qq277049 2012-10-30 18:02:40 3439次浏览 1条回复 0 0 0

怎么样才可以做到更新用户的信息的时候进行cookie更新

<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{

    public $email;
    public $id;
    public $phone;
    public $record;
    public $userInfo;
    //accounts
    const ERROR_ACCOUNTS=3;

    public function __construct($record,$password)
    {
        $this->record = $record;
        $this->password = $password;
    }
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate($user = null)
	{
        /**
         * 进行验证
         */
        if(empty($user))
        {
            /**
             * 判断用户输入类型
             */
            $result = new CEmailValidator();
            if ( $result->validateValue($this->record) ){
                $this->email = $this->record;
                $factor['email'] = $this->record;
            } elseif (is_numeric($this->record) ) {
                $this->phone = $this->record;
                $factor['phone'] = $this->record;
            } else {
                $this->username = $this->record;
                $factor['username'] = $this->record;
            }
            $user = User::model()->findByAttributes($factor);
            $isset = true;
        }
        if(empty($user))
        {
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        } else if($user->password != $user->encrypt($this->password) && isset($isset)) {
            $temp = $user->encrypt($this->password);
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->id = $user->id;
            $this->email = $user->email;
            $this->username = $user->username;
            $this->phone = $user->phone;
            $this->userInfo = @$user->userInfo->attributes;
            $this->setState('userInfo',$this->userInfo);//用户的相关信息存储到cookie中
            $this->errorCode = self::ERROR_NONE;
        }

        return !$this->errorCode;
	}

    public function getId()
    {
        return $this->id;
    }
}
<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-10-29
 * Time: 下午3:10
 * To change this template use File | Settings | File Templates.
 */
class WebUser extends CWebUser
{
    public function login($identity,$duration=0)
    {
        if(parent::login($identity,$duration))
        {
            $this->updateStates((array)$identity);
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $arr
     * 更新SESSION信息
     */
    public function updateStates($arr)
    {
        $this->loadIdentityStates($arr);
    }

}
您需要登录后才可以回复。登录 | 立即注册