加载中...

Yii-srbac- 权限扩展模块工作原理


  1. 设置权限规则表:可放在module模块配置文件里面
    1. public function init() {
    2. //操作权限表,必须存在以下字段:
    3. //itemname角色名/ID,
    4. //type授权项目类型/1(任务)或者2(角色),
    5. //bizrule权限/逻辑运算表达式为false是有权限操作,
    6. //data数据/YII暂无利用
    7. Yii::app()->authManager->itemTable = 'AuthItem';
    8. //会员组-权限对应表,必须存在以下字段:
    9. //child子角色/ID,
    10. //parent父角色/ID,此表可循环执行,可多级继承
    11. Yii::app()->authManager->itemChildTable = 'uthItemChild';
    12. //会员-会员组对应表,会员组可直接为操作名称,必须存在以下字段:
    13. //itemname角色名/ID,
    14. //userid用户名/ID,
    15. //bizrule权限/逻辑运算表达式为false是有权限操作,
    16. //data数据/YII暂无利用
    17. Yii::app()->authManager->assignmentTable = 'zd_mem_glog';
    18. }

  2. 实现规则,所在控制器继承基类SBaseController,原来为Controller
    1. class ProductController extends SBaseController
    2. {
    3. ........
    4. }
    5. class SBaseController extends Controller
    6. {
    7. ........
    8. }

  3. SBaseController继承基类Controller,前填加beforeAction,实现权限验证。
    1. protected function beforeAction($action) {
    2. //载入模块分隔符
    3. $del = Helper::findModule('srbac')->delimeter;
    4. //取得前模块名称
    5. $mod = $this->module !== null ? $this->module->id . $del : "";
    6. $contrArr = explode("/", $this->id);
    7. $contrArr[sizeof($contrArr) - 1] = ucfirst($contrArr[sizeof($contrArr) - 1]);
    8. $controller = implode(".", $contrArr);
    9.  
    10. $controller = str_replace("/", ".", $this->id);
    11. // 生成静态页面 模块+分隔符+控制器(首字母大写)+方法(首字母大写)例: model-ControllerAction
    12. if(sizeof($contrArr)==1){
    13. $controller = ucfirst($controller);
    14. }
    15. $access = $mod . $controller . ucfirst($this->action->id);
    16. //验证访问页面地址是否在总是允许列表里面,是返回有权限
    17. if (in_array($access, $this->allowedAccess())) {
    18. return true;
    19. }
    20. //验证SRBAC有无安装,没在安装,返回的权限访问
    21. if (!Yii::app()->getModule('srbac')->isInstalled()) {
    22. return true;
    23. }
    24. //验证SRBAC有无开启,没在开启,返回的权限访问
    25. if (Yii::app()->getModule('srbac')->debug) {
    26. return true;
    27. }
    28. // 权限验证
    29. if (!Yii::app()->user->checkAccess($access) || Yii::app()->user->isGuest) {
    30. $this->onUnauthorizedAccess();
    31. } else {
    32. return true;
    33. }
    34. }

  4. CDbAuthManager读取当前用户角色
    1. public function getAuthAssignments($userId)
    2. {
    3. $rows=$this->db->createCommand()
    4. ->select()
    5. ->from($this->assignmentTable)
    6. ->where('userid=:userid', array(':userid'=>$userId))
    7. ->queryAll();
    8. $assignments=array();
    9. foreach($rows as $row)
    10. {
    11. if(($data=@unserialize($row['data']))===false)
    12. $data=null;
    13. $assignments[$row['itemname']]=new CAuthAssignment($this,$row['itemname'],$row['userid'],$row['bizrule'],$data);
    14. }
    15. return $assignments;
    16. }

  5. CDbAuthManager读取角色对应权限
    1. public function getAuthItem($name)
    2. {
    3. $row=$this->db->createCommand()
    4. ->select()
    5. ->from($this->itemTable)
    6. ->where('name=:name', array(':name'=>$name))
    7. ->queryRow();
    8.  
    9. if($row!==false)
    10. {
    11. if(($data=@unserialize($row['data']))===false)
    12. $data=null;
    13. return new CAuthItem($this,$row['name'],$row['type'],$row['description'],$row['bizrule'],$data);
    14. }
    15. else
    16. return null;
    17. }

  6. CDbAuthManager读取权限对应操作
    1. protected function checkAccessRecursive($itemName,$userId,$params,$assignments)
    2. {
    3. if(($item=$this->getAuthItem($itemName))===null)
    4. return false;
    5. Yii::trace('Checking permission "'.$item->getName().'"','system.web.auth.CDbAuthManager');
    6. if(!isset($params['userId']))
    7. $params['userId'] = $userId;
    8. if($this->executeBizRule($item->getBizRule(),$params,$item->getData()))
    9. {
    10. if(in_array($itemName,$this->defaultRoles))
    11. return true;
    12. if(isset($assignments[$itemName]))
    13. {
    14. $assignment=$assignments[$itemName];
    15. if($this->executeBizRule($assignment->getBizRule(),$params,$assignment->getData()))
    16. return true;
    17. }
    18. $parents=$this->db->createCommand()
    19. ->select('parent')
    20. ->from($this->itemChildTable)
    21. ->where('child=:name', array(':name'=>$itemName))
    22. ->queryColumn();
    23. foreach($parents as $parent)
    24. {
    25. if($this->checkAccessRecursive($parent,$userId,$params,$assignments))
    26. return true;
    27. }
    28. }
    29. return false;
    30. }


  7. CAuthManager验证权限
    1. public function executeBizRule($bizRule,$params,$data)
    2. {
    3. return $bizRule==='' || $bizRule===null || ($this->showErrors ? eval($bizRule)!=0 : @eval($bizRule)!=0);
    4. }

  8. 总是充许访问规则设置

还没有评论.