天天看點

YII 存放登入資訊的類

在登陸驗證時候增加額外項:

在useridentity.php中

YII 存放登入資訊的類

class useridentity extends cuseridentity{  

    $this->setstate('last_login_time',$user->last_login_time);  

}  

如此,在應用程式的任何地方,這個屬性可以通過如下擷取:yii::app()->user->getstate('last_login_time')

再重新登入看看,

YII 存放登入資訊的類

public function setstate($key, $value, $defaultvalue = null) {  

    $key = $this->getstatekeyprefix() . $key;  

    if ($value === $defaultvalue)  

        unset($_session[$key]);  

    else  

        $_session[$key] = $value;  

其實他将資訊放到session中了

其中的user是yii的一個components.需要在protected/config/main.php中定義

YII 存放登入資訊的類

'user'=>array(  

        // enable cookie-based authentication  

        'allowautologin'=>true,  

        'loginurl' => array('site/login'),  

),  

步驟:1、添加$user屬性到useridentity類。 添加getuser()方法-getter上面這個屬性。加setuser($user)方法-setter上面這個屬性,它可以指派給user的資訊通過$user這個屬性。

使用者資訊存到資料庫表裡

我的useridentity類例子:

YII 存放登入資訊的類

<?php  

class useridentity extends cuseridentity {  

    /** 

     * user's attributes 

     * @var array 

     */  

    public $user;  

    public function authenticate() {  

        $this->errorcode = self::error_password_invalid;  

        $user = user::model()->findbyattributes(array('email' => chtml::encode($this->username)));  

        if ($user) {  

            if ($user->password === md5($user->salt . $this->password)) {  

                $this->errorcode = self::error_none;  

                $this->setuser($user);  

            }  

        }  

        unset($user);  

        return !$this->errorcode;  

    }  

    public function getuser() {  

        return $this->user;  

    public function setuser(cactiverecord $user) {  

        $this->user = $user->attributes;  

?>   

現在使用者的屬性已經設定,建立webuser類并把它放在/protected/components

YII 存放登入資訊的類

class webuser extends cwebuser {  

    public function __get($name) {  

        if ($this->hasstate('__userinfo')) {  

            $user = $this->getstate('__userinfo', array());  

            if (isset($user[$name])) {  

                return $user[$name];  

       //this method can user yii::app()->user->{$user的attribute}  

        return parent::__get($name);  

    public function login($identity, $duration) {  

        $this->setstate('__userinfo', $identity->getuser());  

        parent::login($identity, $duration);  

    public function getisguest() {  

        $customer = yii::app()->session->get('customer');  

        return $customer === null || $customer['id'] === null;  

?>  

 記得設定一下這個類yii::app()->user

YII 存放登入資訊的類

'components'=>array(  

    'user'=>array(  

        'class'=>'webuser',  

    )  

)  

調用方法

yii::app()->user->getisguest()

yii::app()->user->__userinfo;

2使用者資訊存到單獨的檔案

YII 存放登入資訊的類

class webuser extends cwebuser  

{  

    public function getreturnurl($defaulturl=null)  

    {     

        $userinfo = $this->getuserinfo();  

        if(isset($userinfo['url'])){  

            return $userinfo['url'];  

        return parent::getreturnurl($defaulturl);  

    protected function afterlogin($fromcookie)  

    {  

        parent::afterlogin($fromcookie);  

        $users = require(dirname(__file__) . '/../config/password.php');  

        $this->setstate('userinfo',$users[$this->getname()]);  

    public function getuserinfo()  

        return $this->getstate('userinfo',array());  

//accessrules  roles  

    public function checkaccess($operation,$params=array(),$allowcaching=true)  

        if($userinfo['group'] == $operation){  

            return true;  

        return parent::checkaccess($operation,$params,$allowcaching);  

 password.php

YII 存放登入資訊的類

return array(  

    'dianyin'           => array(  

        'pwd'           => 'dianyinxx',  

        'url'           => array('dianyin/order/index'),  

        'merchant_id'   => 1,  

        'group'         => 'dianyin',  

     ),  

    'boer' => array(  

        'pwd'           => 'boerxx',  

        'url'           =>  array('third_jifen/default/index'),  

        'group'         => 'jifen',  

    ),  

);  

 權限checkaccess結合roles

YII 存放登入資訊的類

public function accessrules()  

    return array(  

        array('allow', // allow authenticated users to access all actions  

            'roles'=>array('jifen'),  

        ),  

        array('allow',  // deny all users  

            'actions'=>array('login','logout'),  

            'users'=>array('*'),  

        ),    

        array('deny',  // deny all users  

    );  

}