当前位置:首页 > CMS教程 > 其它CMS > 列表

yii权限控制的方法(三种方法)

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-02 11:25:46 浏览: 评论:0 

这篇文章主要介绍了yii权限控制的方法,结合实例形式分析了通过accessControl,插件机混合模式三种方法实现权限控制的实现技巧,需要的朋友可以参考下。

本文实例讲述了yii权限控制的方法,分享给大家供大家参考,具体如下:

这里摘录以下3种:

1. 通过accessControl:

  1. public function filters() 
  2. { 
  3.   return array( 
  4.     'accessControl', // perform access control for CRUD operations 
  5.   ); 
  6. } 
  7. /** 
  8.  * Specifies the access control rules. 
  9.  * This method is used by the 'accessControl' filter. 
  10.  * @return array access control rules 
  11.  */ 
  12. public function accessRules() 
  13. { 
  14.   return array( 
  15.     array('allow', // allow authenticated users to access all actions 
  16.       'users'=>array('@'), 
  17.     ), 
  18.     array('deny', // deny all users 
  19.       'users'=>array('*'), 
  20.     ), 
  21.   ); 
  22. } 

2. 通过插件(如:right)

  1. public function filters() 
  2. { 
  3.   return array( 
  4.     'rights', 
  5.   ); 
  6. } 

3. 混合模式:

  1. /** 
  2.  * @return array action filters 
  3.  */ 
  4. public function filters() 
  5. { 
  6.   return array( 
  7.     'updateOwn + update', // Apply this filter only for the update action. 
  8.     'rights', 
  9.   ); 
  10. } 
  11. /** 
  12.  * Filter method for checking whether the currently logged in user 
  13.  * is the author of the post being accessed. 
  14.  */ 
  15. public function filterUpdateOwn($filterChain) 
  16. { 
  17.   $post=$this->loadModel(); 
  18.   // Remove the 'rights' filter if the user is updating an own post 
  19.   // and has the permission to do so. 
  20.   if(Yii::app()->user->checkAccess('PostUpdateOwn', array('userid'=>$post->author_id))) 
  21.     $filterChain->removeAt(1); 
  22.   $filterChain->run(); 
  23. } 

如果有权限的基础上,开放某些动作的权限,可以通过allowedActions:

  1. public function allowedActions() 
  2. { 
  3.   return 'autocomplate,autocomplate2'; 
  4. } 

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Tags: yii权限控制

分享到: