欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法分析

 更新時間:2016年07月14日 11:51:56   作者:dreamzml  
這篇文章主要介紹了Yii中srbac權(quán)限擴(kuò)展模塊工作原理與用法,結(jié)合實(shí)例形式分析了srbac模塊的原理及權(quán)限操作的相關(guān)技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • php jsonp單引號轉(zhuǎn)義

    php jsonp單引號轉(zhuǎn)義

    JSONP(JSON with Padding)是一個非官方的協(xié)議,他的實(shí)現(xiàn)方式大致就是:讓客戶端決定要回調(diào)的Javascript函數(shù)名,在第三方服務(wù)端將 JSON 數(shù)據(jù)拼裝到回調(diào)函數(shù)名中,返回的就是參數(shù)為JSON數(shù)據(jù)的函數(shù)調(diào)用腳本,瀏覽器加載腳本并執(zhí)行達(dá)到獲取第三方數(shù)據(jù)的目的。
    2014-11-11
  • ThinkPHP3.2.3數(shù)據(jù)庫設(shè)置新特性

    ThinkPHP3.2.3數(shù)據(jù)庫設(shè)置新特性

    前篇文章,我們總結(jié)了下ThinkPHP3.2中所產(chǎn)生的新變化,本文我們來詳細(xì)看下關(guān)于數(shù)據(jù)庫這塊有哪些新特性,非常細(xì)致,有需要的小伙伴參考下。
    2015-03-03
  • PHP實(shí)現(xiàn)微信公眾平臺音樂點(diǎn)播

    PHP實(shí)現(xiàn)微信公眾平臺音樂點(diǎn)播

    首先說一下思路,微信提供了接口,只要數(shù)據(jù)格式滿足它所給的接口的XML格式即可以發(fā)送給關(guān)注者對應(yīng)的音樂
    2014-03-03
  • laravel配置Redis多個庫的實(shí)現(xiàn)方法

    laravel配置Redis多個庫的實(shí)現(xiàn)方法

    這篇文章主要介紹了laravel配置Redis多個庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • PHP微信開發(fā)之模板消息回復(fù)

    PHP微信開發(fā)之模板消息回復(fù)

    這篇文章主要為大家詳細(xì)介紹了PHP微信開發(fā)之簡單實(shí)現(xiàn)模板消息回復(fù) 的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06
  • YII路徑的用法總結(jié)

    YII路徑的用法總結(jié)

    這篇文章主要介紹了YII路徑的用法總結(jié),需要的朋友可以參考下
    2014-07-07
  • destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法

    destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法

    這篇文章主要介紹了destoon實(shí)現(xiàn)調(diào)用自增數(shù)字從1開始的方法,很有實(shí)用價值的一個技巧,需要的朋友可以參考下
    2014-08-08
  • php 讀取文件夾下所有圖片、文件的實(shí)例

    php 讀取文件夾下所有圖片、文件的實(shí)例

    今天小編就為大家分享一篇php 讀取文件夾下所有圖片、文件的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • thinkPHP5.0框架配置格式、加載解析與讀取方法

    thinkPHP5.0框架配置格式、加載解析與讀取方法

    這篇文章主要介紹了thinkPHP5.0框架配置格式、加載解析與讀取方法,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5.0框架配置的常用格式,加載解析方法,讀取方法等相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)

    laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn)

    本文主要介紹了laravel修改用戶模塊的密碼驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論