Yii中srbac權(quán)限擴展模塊工作原理與用法分析
本文實例講述了Yii中srbac權(quán)限擴展模塊工作原理與用法。分享給大家供大家參考,具體如下:
1. 設(shè)置權(quán)限規(guī)則表:可放在module模塊配置文件里面
public function init() { //操作權(quán)限表,必須存在以下字段: //itemname角色名/ID, //type授權(quán)項目類型/1(任務(wù))或者2(角色), //bizrule權(quán)限/邏輯運算表達(dá)式為false是有權(quán)限操作, //data數(shù)據(jù)/YII暫無利用 Yii::app()->authManager->itemTable = 'AuthItem'; //會員組-權(quán)限對應(yīng)表,必須存在以下字段: //child子角色/ID, //parent父角色/ID,此表可循環(huán)執(zhí)行,可多級繼承 Yii::app()->authManager->itemChildTable = 'uthItemChild'; //會員-會員組對應(yīng)表,會員組可直接為操作名稱,必須存在以下字段: //itemname角色名/ID, //userid用戶名/ID, //bizrule權(quán)限/邏輯運算表達(dá)式為false是有權(quán)限操作, //data數(shù)據(jù)/YII暫無利用 Yii::app()->authManager->assignmentTable = 'zd_mem_glog';
2. 實現(xiàn)規(guī)則,所在控制器繼承基類SBaseController,原來為Controller
class ProductController extends SBaseController { ........ } class SBaseController extends Controller { ........ }
3. SBaseController繼承基類Controller,前填加beforeAction,實現(xiàn)權(quán)限驗證。
protected function beforeAction($action) { //載入模塊分隔符 $del = Helper::findModule('srbac')->delimeter; //取得前模塊名稱 $mod = $this->module !== null ? $this->module->id . $del : ""; $contrArr = explode("/", $this->id); $contrArr[sizeof($contrArr) - 1] = ucfirst($contrArr[sizeof($contrArr) - 1]); $controller = implode(".", $contrArr); $controller = str_replace("/", ".", $this->id); // 生成靜態(tài)頁面 模塊+分隔符+控制器(首字母大寫)+方法(首字母大寫)例: model-ControllerAction if(sizeof($contrArr)==1){ $controller = ucfirst($controller); } $access = $mod . $controller . ucfirst($this->action->id); //驗證訪問頁面地址是否在總是允許列表里面,是返回有權(quán)限 if (in_array($access, $this->allowedAccess())) { return true; } //驗證SRBAC有無安裝,沒在安裝,返回的權(quán)限訪問 if (!Yii::app()->getModule('srbac')->isInstalled()) { return true; } //驗證SRBAC有無開啟,沒在開啟,返回的權(quán)限訪問 if (Yii::app()->getModule('srbac')->debug) { return true; } // 權(quán)限驗證 if (!Yii::app()->user->checkAccess($access) || Yii::app()->user->isGuest) { $this->onUnauthorizedAccess(); } else { return true; } }
4. CDbAuthManager讀取當(dāng)前用戶角色
public function getAuthAssignments($userId) { $rows=$this->db->createCommand() ->select() ->from($this->assignmentTable) ->where('userid=:userid', array(':userid'=>$userId)) ->queryAll(); $assignments=array(); foreach($rows as $row) { if(($data=@unserialize($row['data']))===false) $data=null; $assignments[$row['itemname']]=new CAuthAssignment($this,$row['itemname'],$row['userid'],$row['bizrule'],$data); } return $assignments; }
5. CDbAuthManager讀取角色對應(yīng)權(quán)限
public function getAuthItem($name) { $row=$this->db->createCommand() ->select() ->from($this->itemTable) ->where('name=:name', array(':name'=>$name)) ->queryRow(); if($row!==false) { if(($data=@unserialize($row['data']))===false) $data=null; return new CAuthItem($this,$row['name'],$row['type'],$row['description'],$row['bizrule'],$data); } else return null; }
6. CDbAuthManager讀取權(quán)限對應(yīng)操作
protected function checkAccessRecursive($itemName,$userId,$params,$assignments) { if(($item=$this->getAuthItem($itemName))===null) return false; Yii::trace('Checking permission "'.$item->getName().'"','system.web.auth.CDbAuthManager'); if(!isset($params['userId'])) $params['userId'] = $userId; if($this->executeBizRule($item->getBizRule(),$params,$item->getData())) { if(in_array($itemName,$this->defaultRoles)) return true; if(isset($assignments[$itemName])) { $assignment=$assignments[$itemName]; if($this->executeBizRule($assignment->getBizRule(),$params,$assignment->getData())) return true; } $parents=$this->db->createCommand() ->select('parent') ->from($this->itemChildTable) ->where('child=:name', array(':name'=>$itemName)) ->queryColumn(); foreach($parents as $parent) { if($this->checkAccessRecursive($parent,$userId,$params,$assignments)) return true; } } return false; }
7. CAuthManager驗證權(quán)限
public function executeBizRule($bizRule,$params,$data) { return $bizRule==='' || $bizRule===null || ($this->showErrors ? eval($bizRule)!=0 : @eval($bizRule)!=0); }
8. 總是充許訪問規(guī)則設(shè)置
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
ThinkPHP3.2.3數(shù)據(jù)庫設(shè)置新特性
前篇文章,我們總結(jié)了下ThinkPHP3.2中所產(chǎn)生的新變化,本文我們來詳細(xì)看下關(guān)于數(shù)據(jù)庫這塊有哪些新特性,非常細(xì)致,有需要的小伙伴參考下。2015-03-03destoon實現(xiàn)調(diào)用自增數(shù)字從1開始的方法
這篇文章主要介紹了destoon實現(xiàn)調(diào)用自增數(shù)字從1開始的方法,很有實用價值的一個技巧,需要的朋友可以參考下2014-08-08