Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法分析
本文實(shí)例講述了Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法。分享給大家供大家參考,具體如下:
1. 設(shè)置權(quán)限規(guī)則表:可放在module模塊配置文件里面
public function init() { //操作權(quán)限表,必須存在以下字段: //itemname角色名/ID, //type授權(quán)項(xiàng)目類型/1(任務(wù))或者2(角色), //bizrule權(quán)限/邏輯運(yù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)限/邏輯運(yùn)算表達(dá)式為false是有權(quán)限操作, //data數(shù)據(jù)/YII暫無利用 Yii::app()->authManager->assignmentTable = 'zd_mem_glog';
2. 實(shí)現(xiàn)規(guī)則,所在控制器繼承基類SBaseController,原來為Controller
class ProductController extends SBaseController { ........ } class SBaseController extends Controller { ........ }
3. SBaseController繼承基類Controller,前填加beforeAction,實(shí)現(xiàn)權(quán)限驗(yà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); //驗(yàn)證訪問頁面地址是否在總是允許列表里面,是返回有權(quán)限 if (in_array($access, $this->allowedAccess())) { return true; } //驗(yàn)證SRBAC有無安裝,沒在安裝,返回的權(quán)限訪問 if (!Yii::app()->getModule('srbac')->isInstalled()) { return true; } //驗(yàn)證SRBAC有無開啟,沒在開啟,返回的權(quán)限訪問 if (Yii::app()->getModule('srbac')->debug) { return true; } // 權(quán)限驗(yà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驗(yàn)證權(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è)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- YII2框架中使用RBAC對模塊,控制器,方法的權(quán)限控制及規(guī)則的使用示例
- YII框架模塊化處理操作示例
- YII分模塊加載路由的實(shí)現(xiàn)方法
- YII模塊實(shí)現(xiàn)綁定二級域名的方法
- YII2框架中自定義用戶認(rèn)證模型,完成登陸和注冊操作示例
- YII2框架自定義全局函數(shù)的實(shí)現(xiàn)方法小結(jié)
- YII框架實(shí)現(xiàn)自定義第三方擴(kuò)展操作示例
- Yii2框架自定義驗(yàn)證規(guī)則操作示例
- Yii2實(shí)現(xiàn)自定義獨(dú)立驗(yàn)證器的方法
- 深入淺析yii2-gii自定義模板的方法
- YII2框架中添加自定義模塊的方法實(shí)例分析
相關(guān)文章
ThinkPHP3.2.3數(shù)據(jù)庫設(shè)置新特性
前篇文章,我們總結(jié)了下ThinkPHP3.2中所產(chǎn)生的新變化,本文我們來詳細(xì)看下關(guān)于數(shù)據(jù)庫這塊有哪些新特性,非常細(xì)致,有需要的小伙伴參考下。2015-03-03PHP實(shí)現(xiàn)微信公眾平臺音樂點(diǎn)播
首先說一下思路,微信提供了接口,只要數(shù)據(jù)格式滿足它所給的接口的XML格式即可以發(fā)送給關(guān)注者對應(yīng)的音樂2014-03-03laravel配置Redis多個庫的實(shí)現(xiàn)方法
這篇文章主要介紹了laravel配置Redis多個庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法,很有實(shí)用價值的一個技巧,需要的朋友可以參考下2014-08-08laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)
本文主要介紹了laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09