YII添加rights扩展实现RBAC

下载rights的扩展http://www.yiiframework.com/extension/rights/,解压到protected下的modules目录

接下来按照如下顺序进行操作:

一.用gii生成User模型

安装rights之前,需要先打开gii,对应自己的用户表,生成User模型,修改相应modules里面的配置字段(配置文件main.php的更改见第三步);也就是说,你需要自己先有用户表;我测试的用户表是id,usrname,password


二.修改UserIdentify.php认证

需要修改protected,components下面的UserIdentify.php里面的认证过程,如下:

  1. <?php  
  2.   
  3. class UserIdentity extends CUserIdentity  
  4. {  
  5.     private $_id;  
  6.   
  7.     public function authenticate()  
  8.     {  
  9.         $userFromDB = User::model()->find('usrname=?',array(strtolower($this->username)));  
  10.   
  11.         if( !isset($this->username) || null === $userFromDB )  
  12.         {  
  13.             $this->errorCode=self::ERROR_USERNAME_INVALID;  
  14.         }  
  15.         elseif( !isset($this->password) || null === $userFromDB )  
  16.         {  
  17.             $this->errorCode=self::ERROR_PASSWORD_INVALID;  
  18.         }  
  19.         elseif$userFromDB->password === md5($this->password) )  
  20.         {  
  21.             $this->username = $userFromDB->usrname;  
  22.             $this->_id = $userFromDB->id;  
  23.   
  24.             $this->errorCode=self::ERROR_NONE;  
  25.         }  
  26.   
  27.         return !$this->errorCode;  
  28.     }  
  29.   
  30.     //必须返回id,不能返回usrName  
  31.     public function getId()  
  32.     {  
  33.         return $this->_id;  
  34.     }  
  35. }  
  36. ?>  

三.更改main.php配置文件

在config/main.php里面配置如下:

  1. 'import'=>array(  
  2.         'application.models.*',  
  3.         'application.components.*',  
  4.         'application.modules.rights.*',  
  5.         'application.modules.rights.components.*',//这一行,在官方文档里面没有,不写的话,会导致RWebUser找不到  
  6.     ),  
  7.   
  8.   
  9. 'modules'=>array(  
  10.   
  11.         'rights'=>array(  
  12.             'superuserName'=>'admin',//自己用户表里面的用户,这个作为超级用户  
  13.             'userClass'=>'User',//自己用户表对应的用户模型类  
  14.             'authenticatedName'=>'Authenticated',//自定义名称  
  15.             'userIdColumn'=>'id',//自己用户表对应的id  
  16.             'userNameColumn'=>'usrname',//自己用户表对应的用户名称  
  17.             'enableBizRule'=>true,  
  18.             'enableBizRuleData'=>false,  
  19.             'displayDescription'=>true,  
  20.             'flashSuccessKey'=>'RightsSuccess',  
  21.             'flashErrorKey'=>'RightsError',  
  22.             'baseUrl'=>'/rights',  
  23.             'layout'=>'rights.views.layouts.main',  
  24.             'appLayout'=>'application.views.layouts.main',  
  25.             'cssFile'=>'rights.css',  
  26.             'install'=>true,//第一次安装需要为true,安装成功以后记得改成false  
  27.             'debug'=>false,  
  28.         ),  
  29.   
  30.   
  31. 'components'=>array(  
  32.         'user'=>array(  
  33.             // enable cookie-based authentication  
  34.             'allowAutoLogin'=>true,  
  35.             'class'=>'RWebUser',  
  36.         ),  
  37.   
  38.         'authManager' => array(  
  39.             'class' => 'RDbAuthManager',  
  40.             'assignmentTable' => 'authassignment',  
  41.             'itemTable' => 'authitem',  
  42.             'itemChildTable' => 'authitemchild',  
  43.             'rightsTable' => 'rights',  
  44.             'defaultRoles'=>array('Guest'),  
  45.         ),  

四.修改controller.php

controller需要继承rights的控制器RController,直接改protected/components/Controller.php,继承自RController即可,如下:

  1. <?php  
  2. /** 
  3.  * Controller is the customized base controller class. 
  4.  * All controller classes for this application should extend from this base class. 
  5.  */  
  6. class Controller extends RController  
  7. {  
  8.     /** 
  9.      * @var string the default layout for the controller view. Defaults to '//layouts/column1', 
  10.      * meaning using a single column layout. See 'protected/views/layouts/column1.php'. 
  11.      */  
  12.     public $layout='//layouts/column1';  
  13.     /** 
  14.      * @var array context menu items. This property will be assigned to {@link CMenu::items}. 
  15.      */  
  16.     public $menu=array();  
  17.     /** 
  18.      * @var array the breadcrumbs of the current page. The value of this property will 
  19.      * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links} 
  20.      * for more details on how to specify this property. 
  21.      */  
  22.     public $breadcrumbs=array();  
  23. }  
  24. ?>  


五.控制层需重写filters并给以rights验证(这个很重要)

每个控制层,都需要重写filters方法,并赋予rights验证,才可以启用rights验证,否则,rights不起作用

  1. <?php  
  2.   
  3. class HomeController extends Controller  
  4. {  
  5.     /** 
  6.      * @var string the default layout for the views. Defaults to '//layouts/column2', meaning 
  7.      * using two-column layout. See 'protected/views/layouts/column2.php'. 
  8.      */  
  9.     public $layout='layout';  
  10.   
  11.     /** 
  12.      * @return array action filters 
  13.      */  
  14.     public function filters()  
  15.     {  
  16.         return array(  
  17.             'postOnly + delete'// 只允许post请求的删除操作,这个是为了避免用户通过url直接请求删除某数据  
  18.             'rights',//采用rights的权限过滤  
  19.         );  
  20.     }  
  21.   
  22.     /** 
  23.      * Displays a particular model. 
  24.      * @param integer $id the ID of the model to be displayed 
  25.      */  
  26.     public function actionView($id)  
  27.     {  
  28.         $this->render('view',array(  
  29.             'model'=>$this->loadModel($id),  
  30.         ));  
  31.     }  
  32.   
  33.     /** 
  34.      * Creates a new model. 
  35.      * If creation is successful, the browser will be redirected to the 'view' page. 
  36.      */  
  37.     public function actionCreate()  
  38.     {  
  39.         $model=new Home;  
  40.   
  41.         // Uncomment the following line if AJAX validation is needed  
  42.         // $this->performAjaxValidation($model);  
  43.   
  44.         if(isset($_POST['Home']))  
  45.         {  
  46.                
  47.             $model->attributes=$_POST['Home'];  
  48.                         $model->Addtime=date("Y-m-d H:i:s");  
  49.             if($model->save())  
  50.                 $this->redirect(array('view','id'=>$model->ID));  
  51.         }  
  52.   
  53.         $this->render('create',array(  
  54.             'model'=>$model,  
  55.         ));  
  56.     }  
  57.   
  58.     /** 
  59.      * Updates a particular model. 
  60.      * If update is successful, the browser will be redirected to the 'view' page. 
  61.      * @param integer $id the ID of the model to be updated 
  62.      */  
  63.     public function actionUpdate($id)  
  64.     {  
  65.         $model=$this->loadModel($id);  
  66.   
  67.         // Uncomment the following line if AJAX validation is needed  
  68.         // $this->performAjaxValidation($model);  
  69.   
  70.         if(isset($_POST['Home']))  
  71.         {  
  72.             $model->attributes=$_POST['Home'];  
  73.                         $model->Addtime=date("Y-m-d H:i:s");  
  74.             if($model->save())  
  75.                 $this->redirect(array('view','id'=>$model->ID));  
  76.         }  
  77.   
  78.         $this->render('update',array(  
  79.             'model'=>$model,  
  80.         ));  
  81.     }  
  82.   
  83.     /** 
  84.      * Deletes a particular model. 
  85.      * If deletion is successful, the browser will be redirected to the 'admin' page. 
  86.      * @param integer $id the ID of the model to be deleted 
  87.      */  
  88.     public function actionDelete($id)  
  89.     {  
  90.         $this->loadModel($id)->delete();  
  91.   
  92.         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser  
  93.         if(!isset($_GET['ajax']))  
  94.             $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));  
  95.     }  
  96.   
  97.     /** 
  98.      * Lists all models. 
  99.      */  
  100.     public function actionIndex()  
  101.     {  
  102.         $dataProvider=new CActiveDataProvider('Home');  
  103.         $this->render('index',array(  
  104.             'dataProvider'=>$dataProvider,  
  105.         ));  
  106.     }  
  107.   
  108.   
  109.     /** 
  110.      * Performs the AJAX validation. 
  111.      * @param Home $model the model to be validated 
  112.      */  
  113.     protected function performAjaxValidation($model)  
  114.     {  
  115.         if(isset($_POST['ajax']) && $_POST['ajax']==='mpos-list-form')  
  116.         {  
  117.             echo CActiveForm::validate($model);  
  118.             Yii::app()->end();  
  119.         }  
  120.     }  
  121. }  
  122. ?>  


六.安装rights

安装rights之前,需要用superUser权限的账号登陆,即配置文件main.php中,superuserName对应的用户,如下:

  1. 'superuserName'=>'admin',  

第一次登陆rights,访问地址为:

http://localhost/testApp/index.php?r=rights/install
安装成功以后,可以访问下面的地址

http://localhost/testApp/index.php?r=rights/authItem


七.注意

1.rights里面的sql不需要手工执行,rights会自己安装;如果提示sql不对,先手工导入,然后刷新页面,然后再删除手工导入的表试试。
2.(如果顺序没错,这个步骤应该不需要)在modules,rights,components,RAuthorizer.php里面303,304行注释掉,如下:

  1. if$superusers===array() )  
  2.             throw new CHttpException(403, Rights::t('core''There must be at least one superuser!'));  

3.rights插件界面中若css丢失,则修改modules/rights/RightsModule.php的154行,如下:

  1. // Make sure we want to register a style sheet.  
  2. if$this->cssFile!==false )  
  3. {  
  4.     //改成这个  
  5.     $this->cssFile = $assetsUrl.'/css/default.css';  
  6.   
  7.     //原始代码  
  8. //            if( $this->cssFile===null )  
  9. //                $this->cssFile = $assetsUrl.'/css/default.css';  
  10. //            else  
  11. //                $this->cssFile = Yii::app()->request->baseUrl. '/' .$this->cssFile;  
  12.   
  13.     // Register the style sheet  
  14.     $cs->registerCssFile($this->cssFile);  
  15. }  

转载:http://blog.csdn.net/haiqiao_2010/article/details/38387529
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值