首页 > 其他 > 详细

YII-用户身份管理

时间:2014-02-11 21:01:00      阅读:370      评论:0      收藏:0      [点我收藏+]

一,基础类

   CUserIdentity:是一个基于用户名验证的身份和密码的基类

           CWebUser:代表一个Web应用程序的持久状态

二,身份验证基本过程

       一个典型的使用CWebUser的身份验证过程如下:

    1. 用户提供所需的信息进行身份验证。

    2. 一个是由用户提供的信息创建的身份标识实例

    3. 调用IUserIdentity::authenticate来检查身份标识是否有效

    4. 如果有效,调用CWebUser::login登陆用户, 然后用户浏览器重定向到returnUrl

    5. 如果无效,从身份标识中检索错误代码或错误信息 然后显示。

三,具体流程

   1.使用CFormModel的子类接收并验证数据,若通过启动登陆流程(cotroller)

   

$model = new LoginForm;
$model->attributes=$_POST[‘LoginForm‘];
if($model->validate() && $model->login()){
    $this->redirect(Yii::app()->user->returnUrl);
}

   2.创建用户身份对象并进行登陆验证(LoginForm,UserIdentity)

    /**
 * 使用给定的账号和密码登录
 * @return boolean whether login is successful
 */
public function login(){
    //用户信息验证
    if($this->_identity===null){
        $this->_identity = new UserIdentity($this->username,$this->password);  //用户检验接口
        $this->_identity->authenticate(); //执行验证
    }
                                                                                                                                                           
    //若无错误保存用户信息
    if($this->_identity->errorCode === UserIdentity::ERROR_NONE){
        $duration = $this->rememberMe ? 3600*24*30 : 0;      // 30天
        // CWebUser::login($identity,$duration=0);记录用户信息
        Yii::app()->user->login($this->_identity,$duration);   // 保存用户信息并设定过期时间
        return true;
    }else{
        return false;
    }
}


    public function authenticate(){
//演示用户
$users=array(
    // username => password
    ‘demo‘=>‘demo‘,
    ‘admin‘=>‘admin‘,
);
$user_info = User::model()->findByAttributes(array(‘username‘=>$this->username));
if(empty($user_info))                               //用户不存在
    $this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($user_info[‘password‘]!==$this->password) //密码不正确
    $this->errorCode=self::ERROR_PASSWORD_INVALID;
else
    $this->errorCode=self::ERROR_NONE;
        return !$this->errorCode;


   3 验证通过记录用户信息
   

// CWebUser::login($identity,$duration=0);记录用户信息
            Yii::app()->user->login($this->_identity,$duration);


   4 state的处理

   identity中的state数据都是在调用CWebUser::login后存入了session

    public function setState($key,$value,$defaultValue=null)
{
    $key=$this->getStateKeyPrefix().$key;
    if($value===$defaultValue)
        unset($_SESSION[$key]);
    else
        $_SESSION[$key]=$value;
}
    public function getState($key,$defaultValue=null)
{
    $key=$this->getStateKeyPrefix().$key;
    return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
}


   4 销毁

   CwebUser::logout

    public function logout($destroySession=true)
{
    if($this->beforeLogout())
    {
        if($this->allowAutoLogin)
        {
            Yii::app()->getRequest()->getCookies()->remove($this->getStateKeyPrefix()); //清楚cookie
            if($this->identityCookie!==null)
            {
                $cookie=$this->createIdentityCookie($this->getStateKeyPrefix());
                $cookie->value=null;
                $cookie->expire=0;
                Yii::app()->getRequest()->getCookies()->add($cookie->name,$cookie);
            }
        }
        if($destroySession)
            Yii::app()->getSession()->destroy();  //销毁会话
        else
            $this->clearStates();
        $this->_access=array();
        $this->afterLogout();
    }
}

   


   CHttpSession::destroy()

public function destroy()
    {
        if(session_id()!==‘‘)
        {
            @session_unset();
            @session_destroy();
        }
    }





YII-用户身份管理

原文:http://liyongjiang.blog.51cto.com/5800344/1358019

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!