• Yii登录验证和全局访问用户ID


    注意:Yii默认的代码,Yii::app()->user->id返回的不是我们想要的用户ID,而是用户名。因此在useridentity类中要用一个变量来存储登录用户的ID,然后重载getID()方法,返回正确的用户ID。

    Yii 有一个内置的验证/授权(auth)框架,用起来很方便,还能对其进行自定义,使其符合特殊的需求。

    Yii auth 框架的核心是一个预定义的 用户(user)应用组件 它是一个实现了 IWebUser 接口的对象。此用户组件代表当前用户的持久性认证信息。我们可以通过Yii::app()->user在任何地方访问它。

    使用此用户组件,我们可以通过 CWebUser::isGuest 检查检查一个用户是否登陆; 可以 登录(login) 或 注销(logout) 一个用户;我们可以通过CWebUser::checkAccess检查此用户是否可以执行特定的操作;还可以获取此用户的唯一标识(unique identifier)及其他持久性身份信息。

    为了验证一个用户,我们定义一个有验证逻辑的身份类。这个身份类实现IUserIdentity 接口。

    不同的类可能实现不同的验证方式(例如:OpenID,LDAP)。最好是继承 CUserIdentity,此类是居于用户名和密码的验证方式。

    定义身份类的主要工作是实现IUserIdentity::authenticate方法。在用户会话中根据需要,身份类可能需要定义别的身份信息。

    下面是个例子:

    class UserIdentity extends CUserIdentity
    {
        private $_id;
        public function authenticate()
        {
            $record=User::model()->findByAttributes(array('username'=>$this->username));
            if($record===null)
                $this->errorCode=self::ERROR_USERNAME_INVALID;
            else if($record->password!==md5($this->password))
                $this->errorCode=self::ERROR_PASSWORD_INVALID;
            else
            {
                $this->_id=$record->id;
                $this->setState('title', $record->title);
                $this->errorCode=self::ERROR_NONE;
            }
            return !$this->errorCode;
        }
     
        public function getId()
        {
            return $this->_id;
        }
    }

    二、自己的操作

    路径 

    <?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
    {
        private $_id;
        /**
         * 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()
        {
            $userInfo = Admin::model()->find('username=:name', array(':name'=>$this->username));
    
            if($userInfo == NULL){
                $this->errorCode=self::ERROR_USERNAME_INVALID;
                return false;
            }
    
            if($userInfo->password !== md5($this->password)){
                $this->errorCode=self::ERROR_PASSWORD_INVALID;
                return false;
            }
            $this->_id = $userInfo->admin_id;
            $this->errorCode=self::ERROR_NONE;
            return true;
    
    
    /*        $users=array(
                // username => password
                'demo'=>'demo',
                'admin'=>'admin',
            );
            if(!isset($users[$this->username]))
                $this->errorCode=self::ERROR_USERNAME_INVALID;
            elseif($users[$this->username]!==$this->password)
                $this->errorCode=self::ERROR_PASSWORD_INVALID;
            else
                $this->errorCode=self::ERROR_NONE;
            return !$this->errorCode;*/
        }
    
        public function getId()
        {
            return $this->_id;
        }
    }
  • 相关阅读:
    Oracle EBS订单的流程(Order>AR)
    ORA12547: TNS:lost contact
    Shipping lock fix –> WSH_DELIVERY_DETAILS Column has leading or trailing spaces
    PLSQL 操作 ORACLE 服务器上的文件
    ORACLE EBS 11i常见问题
    solr 查询解析流程
    spring管理servlet的利器SimpleServletHandlerAdapter
    Solr Cache使用介绍及分析
    solr 自定义 dismax查询方式
    spring2中jpa的配置和使用
  • 原文地址:https://www.cnblogs.com/rainblack/p/5622729.html
Copyright © 2020-2023  润新知