auth-booster配置和使用(yii1.5)

auth-booster这个是一个yii框架扩展中的一个模块。是非常好用的(但是里面的说明都是英文的,所以国人用还需要改一点里面的汉化)

1、下载auth-booster这个:http://www.yiiframework.com/extension/authbooster/  这个在附件中已经上传

2、将这个模块拷贝到protected/modules/这个目录下,重新命名auth

3、修改配置文件

'modules'=>array(
               ....
        'auth' => array(
            'userClass' => 'User', // 在模型中使用管理用户表的类
            'userIdColumn' => 'id', // 用户表中用户id
            'userNameColumn' => 'username', //用户名称
            //'defaultLayout' => 'application.modules.admin.views.layouts.column2',//默认的加载的布局,注意:如果这个没有加载就没有这个<meta charset='utf-8'>头信息,就不能插入中文到数据表中
        ),
    ),
'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
            'class' => 'auth.components.AuthWebUser',
            //'loginUrl' => '/site/login',
            // users with full access
            'admins' => array('admin',),
        ),
                'bootstrap' => array(
            'class' => 'ext.bootstrap.components.Bootstrap',
            'responsiveCss' => true,
            'fontAwesomeCss' => true,
            'enableNotifierJS' => false,
            'enableBootboxJS' => false
        ),
                'authManager' => array(
            'class' => 'CDbAuthManager',
            'connectionID' => 'db',
            'itemTable' => '{{authitem}}',
            'itemChildTable' => '{{authitemchild}}',
            'assignmentTable' => '{{authassignment}}',
            'behaviors' => array(
                'auth' => array(
                    'class' => 'auth.components.AuthBehavior',
                ),
            ),
        ),
 ),

上面这些都是最基本的配置

4、导入rbac数据表

CREATE TABLE IF NOT EXISTS `tbl_authassignment` (
  `itemname` varchar(64) NOT NULL comment('分配给用户的节点和authitem表中的name关联'),
  `userid` varchar(64) NOT NULL comment('用户id'),
  `bizrule` text,
  `data` text,
  PRIMARY KEY (`itemname`,`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#这个就是节点表
CREATE TABLE IF NOT EXISTS `tbl_authitem` (
  `name` varchar(64) NOT NULL comment('可以这样分配:admin.category.add/post.add/post.*'),
  `type` int(11) NOT NULL comment('类型,0:操作,1:任务,2:角色'),
  `description` text comment('描述'),
  `bizrule` text,
  `data` text,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `tbl_authitemchild` (
  `parent` varchar(64) NOT NULL comment('父级authitem中类型为1:任务,2:角色的名称,这个值唯一'),
  `child` varchar(64) NOT NULL comment('拥有的子节点,这个表和节点表是一对多关系'),
  PRIMARY KEY (`parent`,`child`),
  KEY `child` (`child`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

5、在需要验证的控制器下面的filters()

public function filters()
    {
        return array(
            array('auth.filters.AuthFilter'),
            //'accessControl', // perform access control for CRUD operations
        );
    }

这三张表是最基本的rbac表,其实还有一张用户表,这个不需要导入,因为既然做权限验证,应该就有自己建立的用户表

三张表的关系

rbac权限验证核心:

因为yii也是单一入口文件,所以在验证权限的时候都是非常的方便。

在访问每一个方法的时候,都可以验证这个方法用户是否有权限。

在控制器里

$name = $this->getModule()->id; // module

$name = $this->getId();  // controller

$name = $this->getAction()->id;  // action

在视图里,除了上述2个方法还可:

$name = $this->module->id; // module

$name = Yii::app()->controller->id;  // controller

$name = $this->getAction()->getId(); // action

这样组合在一起就能验证访问的操作是否有权限

tbl_authassignment 这个是表是分配给用户有哪些权限,就是操作的节点

tbl_authitem  这个表是节点表,type:0节点,1:任务,2:角色

tbl_authitemchild  这个表是任务和角色拥有哪些操作和任务

 

原文地址:https://www.cnblogs.com/shiwenhu/p/4667628.html