• Yii 自定义属性


    如果在用户登录后想额外调用除 user,id之外的数据库变量,可以这样设置:

    在登陆验证时候增加额外项:Yii::app()->user->last_login_time

    在UserIdentity.php中

    Java代码 复制代码 收藏代码
    1. class UserIdentity extends CUserIdentity   
    2. {   
    3.     $this->setState('last_login_time',$user->last_login_time);   
    4. }  
    class UserIdentity extends CUserIdentity
    {
    	$this->setState('last_login_time',$user->last_login_time);
    }

     如此,在应用程序的任何地方,这个属性可以通过如下获取:Yii::app()->user->last_login_time

    再重新登录看看,

    Java代码 复制代码 收藏代码
    1. public function setState($key, $value, $defaultValue = null) {   
    2.     $key = $this->getStateKeyPrefix() . $key;   
    3.     if ($value === $defaultValue)   
    4.         unset($_SESSION[$key]);   
    5.     else  
    6.         $_SESSION[$key] = $value;   
    7. }  
    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中定义

    Java代码 复制代码 收藏代码
    1. 'user'=>array(   
    2.         // enable cookie-based authentication   
    3.         'allowAutoLogin'=>true,   
    4.         'loginUrl' => array('site/login'),   
    5. ),  
    'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            'loginUrl' => array('site/login'),
    ),

    通过扩展CWebUser添加信息到Yii:app()->user

    步骤:1、添加$user属性到UserIdentity类。 添加getUser()方法-getter上面这个属性。加setUser($user)方法-setter上面这个属性,它可以赋值给user的信息通过$user这个属性。

    用户信息存到数据库表里
    我的UserIdentity类例子:

    Java代码 复制代码 收藏代码
    1. <?php   
    2. class UserIdentity extends CUserIdentity {   
    3.     /**  
    4.      * User's attributes  
    5.      * @var array  
    6.      */  
    7.     public $user;   
    8.   
    9.     public function authenticate() {   
    10.         $this->errorCode = self::ERROR_PASSWORD_INVALID;   
    11.         $user = User::model()->findByAttributes(array('email' => CHtml::encode($this->username)));   
    12.         if ($user) {   
    13.             if ($user->password === md5($user->salt . $this->password)) {   
    14.                 $this->errorCode = self::ERROR_NONE;   
    15.                 $this->setUser($user);   
    16.             }   
    17.         }   
    18.         unset($user);   
    19.         return !$this->errorCode;   
    20.     }   
    21.   
    22.     public function getUser() {   
    23.         return $this->user;   
    24.     }   
    25.   
    26.     public function setUser(CActiveRecord $user) {   
    27.         $this->user = $user->attributes;   
    28.     }   
    29. }   
    30.   
    31. ?>   
    <?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

    Java代码 复制代码 收藏代码
    1. <?php   
    2. class WebUser extends CWebUser {   
    3.     public function __get($name) {   
    4.         if ($this->hasState('__userInfo')) {   
    5.             $user = $this->getState('__userInfo', array());   
    6.             if (isset($user[$name])) {   
    7.                 return $user[$name];   
    8.             }   
    9.         }   
    10.   
    11.         return parent::__get($name);   
    12.     }   
    13.   
    14.     public function login($identity, $duration) {   
    15.         $this->setState('__userInfo', $identity->getUser());   
    16.         parent::login($identity, $duration);   
    17.     }   
    18.   
    19.     public function getIsGuest() {   
    20.         $customer = Yii::app()->session->get('customer');   
    21.         return $customer === null || $customer['id'] === null;   
    22.     }   
    23. }   
    24. ?>  
    <?php
    class WebUser extends CWebUser {
        public function __get($name) {
            if ($this->hasState('__userInfo')) {
                $user = $this->getState('__userInfo', array());
                if (isset($user[$name])) {
                    return $user[$name];
                }
            }
    
            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

    Java代码 复制代码 收藏代码
    1. <?php   
    2. 'components'=>array(   
    3.     'user'=>array(   
    4.         'class'=>'WebUser',   
    5.     )   
    6. )   
    7. ?>   
    <?php
    'components'=>array(
        'user'=>array(
            'class'=>'WebUser',
        )
    )
    ?> 

    调用方法

    Yii::app()->user->getIsGuest()

    2用户信息存到单独的文件

    Java代码 复制代码 收藏代码
    1. <?php   
    2. class WebUser extends CWebUser   
    3. {   
    4.     public function getReturnUrl($defaultUrl=null)   
    5.     {      
    6.         $userInfo = $this->getUserInfo();   
    7.         if(isset($userInfo['url'])){   
    8.             return $userInfo['url'];   
    9.         }   
    10.         return parent::getReturnUrl($defaultUrl);   
    11.     }   
    12.   
    13.     protected function afterLogin($fromCookie)   
    14.     {   
    15.         parent::afterLogin($fromCookie);   
    16.         $users = require(dirname(__FILE__) . '/../config/password.php');   
    17.   
    18.         $this->setState('userInfo',$users[$this->getName()]);   
    19.     }   
    20.   
    21.     public function getUserInfo()   
    22.     {   
    23.         return $this->getState('userInfo',array());   
    24.     }   
    25. //accessRules  roles   
    26.     public function checkAccess($operation,$params=array(),$allowCaching=true)   
    27.     {   
    28.         $userInfo = $this->getUserInfo();   
    29.         if($userInfo['group'] == $operation){   
    30.             return true;   
    31.         }   
    32.         return parent::checkAccess($operation,$params,$allowCaching);   
    33.     }   
    34. }  
    <?php
    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)
        {
            $userInfo = $this->getUserInfo();
            if($userInfo['group'] == $operation){
                return true;
            }
            return parent::checkAccess($operation,$params,$allowCaching);
        }
    }

     password.php

    Java代码 复制代码 收藏代码
    1. <?php   
    2.   
    3. return array(   
    4.     'dianyin'           => array(   
    5.         'pwd'           => 'dianyinXX',   
    6.         'url'           => array('dianyin/order/index'),   
    7.         'merchant_id'   => 1,   
    8.         'group'         => 'dianyin',   
    9.      ),   
    10.     'boer' => array(   
    11.         'pwd'           => 'boerXX',   
    12.         'url'           =>  array('third_jifen/default/index'),   
    13.         'merchant_id'   => 1,   
    14.         'group'         => 'jifen',   
    15.     ),   
    16. );  
    <?php
    
    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'),
            'merchant_id'   => 1,
            'group'         => 'jifen',
        ),
    );

     权限checkAccess结合roles

    Java代码 复制代码 收藏代码
    1. public function accessRules()   
    2. {   
    3.     return array(   
    4.         array('allow'// allow authenticated users to access all actions   
    5.             'roles'=>array('jifen'),   
    6.         ),   
    7.         array('allow',  // deny all users   
    8.             'actions'=>array('login','logout'),   
    9.             'users'=>array('*'),   
    10.         ),     
    11.         array('deny',  // deny all users   
    12.             'users'=>array('*'),   
    13.         ),   
    14.        
    15.     );   
    16. }  
  • 相关阅读:
    SELinux安全方式
    PHP jpgraph的一点小提示和方法
    PHP之文件的锁定、上传与下载的方法
    MySQL与Oracle差异函数对比
    Dictionary 初始化数据
    IIS7的集成模式下如何让自定义的HttpModule不处理静态文件(.html .css .js .jpeg等)请求
    iis 负载均衡
    iis 反向代理 组件 Application Request Route
    语法糖
    vs git 推送远程会失败.
  • 原文地址:https://www.cnblogs.com/xiongsd/p/3068950.html
Copyright © 2020-2023  润新知