本文實例講述了Yii中srbac權限擴展模塊工作原理與用法。分享給大家供大家參考,具體如下:
1. 設置權限規則表:可放在module模塊配置文件里面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public function init() { //操作權限表,必須存在以下字段: //itemname角色名/ID, //type授權項目類型/1(任務)或者2(角色), //bizrule權限/邏輯運算表達式為false是有權限操作, //data數據/YII暫無利用 Yii::app()->authManager->itemTable = 'AuthItem' ; //會員組-權限對應表,必須存在以下字段: //child子角色/ID, //parent父角色/ID,此表可循環執行,可多級繼承 Yii::app()->authManager->itemChildTable = 'uthItemChild' ; //會員-會員組對應表,會員組可直接為操作名稱,必須存在以下字段: //itemname角色名/ID, //userid用戶名/ID, //bizrule權限/邏輯運算表達式為false是有權限操作, //data數據/YII暫無利用 Yii::app()->authManager->assignmentTable = 'zd_mem_glog' ; |
2. 實現規則,所在控制器繼承基類SBaseController,原來為Controller
1
2
3
4
5
6
7
8
|
class ProductController extends SBaseController { ........ } class SBaseController extends Controller { ........ } |
3. SBaseController繼承基類Controller,前填加beforeAction,實現權限驗證。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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); // 生成靜態頁面 模塊+分隔符+控制器(首字母大寫)+方法(首字母大寫)例: model-ControllerAction if (sizeof( $contrArr )==1){ $controller = ucfirst( $controller ); } $access = $mod . $controller . ucfirst( $this ->action->id); //驗證訪問頁面地址是否在總是允許列表里面,是返回有權限 if (in_array( $access , $this ->allowedAccess())) { return true; } //驗證SRBAC有無安裝,沒在安裝,返回的權限訪問 if (!Yii::app()->getModule( 'srbac' )->isInstalled()) { return true; } //驗證SRBAC有無開啟,沒在開啟,返回的權限訪問 if (Yii::app()->getModule( 'srbac' )->debug) { return true; } // 權限驗證 if (!Yii::app()->user->checkAccess( $access ) || Yii::app()->user->isGuest) { $this ->onUnauthorizedAccess(); } else { return true; } } |
4. CDbAuthManager讀取當前用戶角色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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讀取角色對應權限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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讀取權限對應操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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驗證權限
1
2
3
4
|
public function executeBizRule( $bizRule , $params , $data ) { return $bizRule === '' || $bizRule ===null || ( $this ->showErrors ? eval ( $bizRule )!=0 : @ eval ( $bizRule )!=0); } |
8. 總是充許訪問規則設置
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。