Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法分析
本文實例講述了Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法。分享給大家供大家參考,具體如下:
1. 設(shè)置權(quán)限規(guī)則表:可放在module模塊配置文件里面
public function init() {
//操作權(quán)限表,必須存在以下字段:
//itemname角色名/ID,
//type授權(quán)項目類型/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. 實現(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)文章
php 使用curl模擬登錄人人(校內(nèi))網(wǎng)的簡單實例
下面小編就為大家?guī)硪黄猵hp 使用curl模擬登錄人人(校內(nèi))網(wǎng)的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
yii2框架中使用下拉菜單的自動搜索yii-widget-select2實例分析
這篇文章主要介紹了yii2框架中使用下拉菜單的自動搜索yii-widget-select2的方法,介紹了yii-widget-select2的下載,安裝及具體使用技巧,需要的朋友可以參考下2016-01-01
php 實現(xiàn)銀聯(lián)商務(wù)H5支付的示例代碼
這篇文章主要介紹了php 實現(xiàn)銀聯(lián)商務(wù)H5支付的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Laravel框架運(yùn)行出錯提示RuntimeException No application encryption ke
這篇文章主要介紹了Laravel框架運(yùn)行出錯提示RuntimeException No application encryption key has been specified.解決方法,涉及Laravel框架相關(guān)配置、設(shè)置與運(yùn)行操作技巧,需要的朋友可以參考下2019-04-04
PHP使用openssl擴(kuò)展實現(xiàn)加解密方法示例
這篇文章主要介紹了PHP使用openssl擴(kuò)展實現(xiàn)加解密方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Thinkphp連表查詢及數(shù)據(jù)導(dǎo)出方法示例
這篇文章主要介紹了Thinkphp連表查詢及數(shù)據(jù)導(dǎo)出方法,結(jié)合實例形式分析了thinkPHP連表查詢的操作技巧與相關(guān)注意事項,需要的朋友可以參考下2016-10-10

