yii 权限分级式访问控制的实现(非RBAC法)——已验证

验证和授权——官方文档:

http://www.yiichina.com/guide/topics.auth

http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.auth

相关类参考手册:

http://www.yiichina.com/api/CWebUser

http://www.yiichina.com/api/CAccessRule

http://www.yiichina.com/api/CUserIdentity

可参考文章:

http://my.oschina.net/u/873762/blog/98697

http://www.yiiframework.com/wiki/60/

yii 权限分级式访问控制的实现(非RBAC法)

主要参考资料来源:yii官网http://www.yiiframework.com/wiki/60/  我只是做了小小的完善。 

yii framework 提供了2套权限访问系统,一套是简单的filter(过滤器)模式,另一套是复杂全面的RBAC模式,我这里要讲的是第一套(因为我也刚刚学到这里)。如 果你有研究过YII官方的demo blog,一定知道,比如,由gii自动生成的user模块,自动附带了简单的filter权限分配功能,具体细节请参照blog手册的“用户验证”一章 节,以及yii官方指南的“验证和授权”一章节。(注意,我这里所指的模块,只是我个人对与user有关的文件的统称,与yii文件系统的模块 (module)含义不同。) 

关于权限分配的文件大多在controllers里,比如打开UserController.php文件你会看到2个类函数。 

public function filters()
{
     return array(
      'accessControl',               // 实现CRUD操作的访问控制。
      'postOnly + delete',
     );
}

public function accessRules()              //这里就是访问规则的设置。
{
     return array(
        array('allow',              // 允许所有用户执行index,view动作。
          'actions'=>array('index','view'),
          'users'=>array('*'), <span></span>           
          ),                    
        array('allow',             // 只允许经过验证的用户执行create, update动作。
           'actions'=>array('create','update'),
           'users'=>array('@'),       // @号指所有注册的用户
            ),
        array('allow',             // 只允许用户名是admin的用户执行admin,delete动作
            'actions'=>array('admin','delete'),
            'users'=>array('admin'),
            ),                   //admin就是指用户名是admin的用户,以硬编码的形式分配用户权限。
            array('deny',           // 拒绝所有的访问。
            'users'=>array('*'),
            ),
        );
} 

关于更多的访问规则的设定请参照官方文件http://www.yiiframework.com/doc/api/1.1/CAccessControlFilter 


好了,现在要开始按照我们自己的需求设置适合自己的权限分配了。我们希望filter访问控制模式能更完美一点,按照常识,我们希望它能按照数据库里user表里不同级别用户,实行不同的授权,而不是用硬编码的形式控制。 

回到demo blog,我先对数据库的tbl_user表做修改,在原来的基础上加上role一项。对原来的用户信息记录添加role的value为"管理员"或"一般用户"("admin"或"user")。 

然后依次执行以下3个步骤:
 

1. 创建组件WebUser,它是对CWebUser的扩展。
2. 修改config/main.php文件。

3.修改accessRules()。

具体细节如下:

1.WebUser.php 组件代码:

在protectedcomponents 下新建WebUser.php

<?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.
 * 
 * this file must be stored in:
 * protected/components/WebUser.php
 */
class WebUser extends CWebUser
{

    // Store model to not repeat query.
    private $_model;

    /**
     * @return first name.
     * @access it by Yii::app()->user->first_name
     */
    public function getFirst_Name()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        return $user->first_name;
    }

    /**
     * This is a function that checks the field 'role'
     * in the User model to be equal to 1, that means it's admin
     * @return boolean
     * @access it by Yii::app()->user->isAdmin()
     */
    public function isAdmin()
    {
        $user = $this->loadUser(Yii::app()->user->id);
        if ($user == null) {
            return 0;
        } else {
            return $user->role == "admin";
        }

    }

    /**
     * Load user model.
     * Returns the data model based on the primary key given in the GET variable.
     * @param integer $id the ID of the model to be loaded
     * @return User the loaded model
     */
    protected function loadUser($id = null)
    {
        if ($this->_model === null) {
            if ($id !== null) {
                $this->_model = User::model()->findByPk($id);
            }
        }
        return $this->_model;
    }

    /**
     * This method is called after the user is successfully logged in.
     * You may override this method to do some postprocessing (e.g. log the user
     * login IP and time; load the user permission information).
     * @param boolean $fromCookie whether the login is based on cookie.
     */
    public function afterLogin($fromCookie)
    {
        //Yii::app()->request->redirect('/index.php/user/create');
        
        if(!Yii::app()->user->isGuest){
            $uid = Yii::app()->user->id;
            $uip = Yii::app()->request->userHostAddress;  //获取用户IP
            User::model()->updateAll(array('logintime'=>time(), 'loginip'=>$uip), 'id=:id', array(':id'=>$uid));
        }
        
        parent::afterLogin($fromCookie);
    }
    
    
    /**
     * This method is invoked right after a user is logged out.
     * You may override this method to do some extra cleanup work for the user.
     */
    /*
    public function afterLogout()  
    {  
        //Yii::app()->request->redirect('/index.php/user/index');
        
        parent::afterLogout();
    } 
    */
}

2.在config/main.php找到如下代码,添加标红色的代码。

    'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
             'class'=>'WebUser',
        ),
)

3.找到需要更改权限的controller类,对accessRules()函数做修改,比如对前文的accessRules()函数做如下修改:

public function accessRules()  //这里就是访问规则的设置。     
{
    return array(
        array('allow',                     // 允许所有用户执行index,view动作。
            'actions'=>array('index','view'),
            'users'=>array('*'),         //*号标识所有用户包括注册的、没注册的、一般的、管理员级的
        ),
        array('allow',                      // 只允许经过验证的用户执行create, update动作。
            'actions'=>array('create','update'),
            'users'=>array('@'),       // @号指所有注册的用户
        ),
        array('allow',                     
            'actions'=>array('admin','delete'),
            /**
              * expression: 设定一个PHP表达式。它的值用来表明这条规则是否适用。
              * 在表达式,你可以使用一个叫$user的变量,它代表的是Yii::app()->user。
              * 这个选项是在1.0.3版本里引入的。
              * 'expression' => '$user->isAdmin()', //即这样也可以
              * 'expression' => '$user->isAdmin() || $user->isAuthor()', //也可以加多条判断
              */
             'expression' => 'yii::app()->user->isAdmin()',
             //这样只有标识为“管理员”的用户才能访问admin,delete动作
        ),
        array('deny',  // 拒绝所有的访问。
            'users'=>array('*'),
        ),
    );
}

工作完成!

From: http://my.oschina.net/u/873762/blog/98697

附:

官网blog Demo 验证修改:

在protectedcomponents 下新建UserIdentity.php

<?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;

    public function authenticate()
    {
        //$record = User::model()->findByAttributes(array('id' => Yii::app()->user->id));
        $record = User::model()->findByAttributes(array('username' => $this->username));
        if ($record === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } 
        /*elseif ($record->password !== md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }*/
        elseif ($record->password !== $this->password) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
        else {
            $this->_id = $record->id;
            //$this->setState('roles', $record->role); //未生效
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }
}

?>

 在用户登陆时则调用如下代码:

$identity = new UserIdentity($username,$password);
if($identity->authenticate()) {
    Yii::app()->user->login($identity);
} else {
    echo $identity->errorMessage;
}

在用户退出时调用了Yii::app()->user->logout();

原文地址:https://www.cnblogs.com/imxiu/p/3451453.html