Yii framework srbac模組根據使用者組許可權的擴充

來源:互聯網
上載者:User

修改Yii的SRBAC模組,使group(你的使用者組表)取代User那一層作為許可權分配的標準。在此基礎上可以修改成去除User層,直接使用Role作為使用者組的方案。

  需要修改的有這幾個地方:

  1)./components/UserIdentity.php

  修改這個檔案是因為Yii預設登入後是將username作為id來儲存在session中的(這點可以在CWebUser.php裡的CWebUser::login裡看到),這裡添加一個屬性:private $_id;然後重寫父類的getId()方法:

  

else{$this->_id = $users->uid;$this->username = $users->username;$this->errorCode=self::ERROR_NONE;}

//重寫介面的方法,返回的唯一標識是uid而不是usernamepublic function getId(){return $this->_id;}

 

 2).在Yii的設定檔中加上組許可權判斷的一些資訊:

  

//配置srbac模組的資料庫結構'authManager'=>array('class'=>'CDbAuthManager','connectionID'=>'db','itemTable'=>'tbl_items','assignmentTable'=>'tbl_assignments','itemChildTable'=>'tbl_itemchildren',//srbac擴充--根據使用者組判斷許可權'groupAuth'=>TRUE,'userGetGroupTable'=>array('tableName'=>'tbl_user','userid'=>'uid','groupid'=>'groupid',),),

  3).修改CWebUser.php,增加一個屬性:public $groupid,這樣以後既可以根據Yii::app()->user->groupid來訪問目前使用者的組id了

  4).從SBaseController找到驗證的動作是Yii::app()->user->checkAccess($access),然後接著往下找,找到最後的執行者是CDbAuthManager::checkAccess

  繼續找到CDbAuthManager::getAuthAssignments這個方法,這個方法做的事情是:根據參數$userId去tbl_assignments表裡找到對應id擁有者的所有許可權返回。(此時這裡的$userId已經是uid而不是username了)

  添加一個方法getAuthAssignmentsByGroup($userId),這個方法主要就是從使用者表中取目前使用者groupid,再用groupid向assignments表中取相應組的所有許可權。

  

/** * Returns the item assignments for the specified user. * @param mixed $userId the user ID (see {@link IWebUser::getId}) * @return array the item assignment information for the user. An empty array will be * returned if there is no item assigned to the user. */public function getAuthAssignments($userId){if($this->groupAuth){return $this->getAuthAssignmentsByGroup($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;}//根據使用者組擷取所有許可權public function getAuthAssignmentsByGroup($userId){$assignments=array();//先擷取使用者的groupidif(!isset(Yii::app()->user->groupid)){$groupID = $this->userGetGroupTable['groupid'];$group = $this->db->createCommand()->select($groupID)->from($this->userGetGroupTable['tableName'])->where($this->userGetGroupTable['userid'].'='.intval($userId))->queryRow();if(empty($group)){return $assignments;}Yii::app()->user->groupid = $group[$groupID];}//然後根據目前使用者的groupid來判斷其身份(User)$rows=$this->db->createCommand()->select()->from($this->assignmentTable)->where('userid=:userid', array(':userid'=>Yii::app()->user->groupid))->queryAll();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;}

  還有,別忘了給CDbAuthManager類添加用到的屬性,否則人家不認識:

  

//使用者組許可權配置資訊public $groupAuth = false;public $userGetGroupTable = null;

  5).開啟SRBAC看下,是不是User變成了你的tbl_group了,Role和Task依然沒變。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.