日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Yii框架權(quán)限控制

 朱啟發(fā) 2014-11-18

需求:公司擁有一套用戶權(quán)限系統(tǒng)。我們?cè)谛掳婵蚣苤?,我們需要兼容這套用戶權(quán)限系統(tǒng)。

 

問(wèn)題:YII單表方式已經(jīng)滿足不了我們的需求,急切需要對(duì)YII進(jìn)行擴(kuò)展設(shè)計(jì),支持?jǐn)?shù)據(jù)庫(kù)分表設(shè)計(jì)

 

解決方法:1、新建protected/sinashowExt/JController.php文件

  1. /** 
  2.  * Controller is the customized base controller class. 
  3.  * All controller classes for this application should extend from this base class. 
  4.  */  
  5. class JController extends CController  
  6. {  
  7.     /** 
  8.      * @var string the default layout for the controller view. Defaults to '//layouts/column1', 
  9.      * meaning using a single column layout. See 'protected/views/layouts/column1.php'. 
  10.      */  
  11.     public $layout='//layouts/column1';  
  12.     /** 
  13.      * @var 菜單 {@link CMenu::items}. 
  14.      */  
  15.     public $menu=array();  
  16.     /** 
  17.      * @var 路徑設(shè)置 
  18.      * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links} 
  19.      * for more details on how to specify this property. 
  20.      */  
  21.     public $breadcrumbs = array();  
  22.     //視圖數(shù)據(jù)  
  23.     public $view        = array();  
  24.     //是否自動(dòng)輸出  
  25.     public $autoView    = false;  
  26.     //輸出頁(yè)面  
  27.     public $renderPage  = '';  
  28.     //頁(yè)面提示文字  
  29.     public $notice  = '';  
  30.     //搜索標(biāo)簽  
  31.     public $searchTag   = array();  
  32.     //其他代碼  
  33.     public $otherHtml   = '';  
  34.     //按鈕標(biāo)簽  
  35.     public $buttonTag   = array();  
  36.     //單位標(biāo)簽  
  37.     public $unitTag     = '';  
  38.     //輸出信息  
  39.     public $alertText   = '';  
  40.     //是否顯示外框  
  41.     public $haveBorder   = true;  
  42.       
  43.     public function init()  
  44.     {  
  45.         $cookie = Yii::app()->request->getCookies();  
  46.         Yii::app()->user->id  = $cookie->itemAt('SSD_user_id')->value;  
  47.         Yii::app()->user->name    = $cookie->itemAt('SSD_user_nick')->value;  
  48.     }  
  49.   
  50.     /** 
  51.      * 判斷是否有指定操作的權(quán)限 
  52.      *  
  53.      * @param string $action 
  54.      */  
  55.     public function checkPower($action)  
  56.     {  
  57.         return "purviewPcc::model()->checkPower('{$this->getModule()->getId()}', '{$this->getId()}', '{$action}')";   
  58.     }  
  59.       
  60.     /** 
  61.      * 檢查權(quán)限擴(kuò)展 
  62.      *  
  63.      * @param string $action 
  64.      * @param string $contrl 
  65.      * @param string $module 
  66.      */  
  67.     public function checkPowerEx($action$contrl=null, $module=null)  
  68.     {  
  69.         if ($contrl === null)  
  70.         {  
  71.             $contrl = $this->getId();  
  72.         }  
  73.           
  74.         if ($module === null)  
  75.         {  
  76.             $module = $this->getModule()->getId();  
  77.         }  
  78.           
  79.         return purviewPcc::model()->checkPower($module$contrl$action);  
  80.     }  
  81.       
  82.     /** 
  83.      * 權(quán)限判斷 
  84.      * 
  85.      */  
  86.      public function purview($module$control$action)  
  87.      {  
  88.         if (!purviewPcc::model()->checkPurview($module,$control,$action))  
  89.         {  
  90.             echo '沒(méi)有訪問(wèn)權(quán)限!';  
  91.             Yii::app()->end();  
  92.         }  
  93.      }  
  94.   
  95.     /** 
  96.      * Action操作前動(dòng)作 
  97.      * 
  98.      * @param unknown_type $action 
  99.      * @return unknown 
  100.      */   
  101.     public function beforeAction($action)  
  102.     {  
  103.         if($action && $this->getModule())  
  104.             $this->purview($this->getModule()->getId(), $this->getId(), $action->getId());  
  105.         return true;  
  106.     }  
  107.       
  108.       
  109.     /** 
  110.      * Action操作后動(dòng)作 
  111.      * 
  112.      * @param string $action 
  113.      */  
  114.     public function afterAction($action)  
  115.     {  
  116.         /** 是否自動(dòng)輸出 */  
  117.         if ($this->autoView)  
  118.         {  
  119.             //默認(rèn)輸入頁(yè)面  
  120.             if (empty($this->renderPage))  
  121.                 $this->renderPage    = $action->getId();            
  122.             $this->render($this->renderPage, $this->view);  
  123.         }  
  124.     }  
  125.       
  126.     /** 
  127.      * 頁(yè)面提示窗口 
  128.      *  
  129.      * @param string $view 
  130.      * @param array $data 
  131.      * @param bool $exit 
  132.      */  
  133.     public function alert($msg$href = 'javascript:history.go(-1);'$time = 0, $exit = true, $view = '//system/alert'$data = array())  
  134.     {  
  135.         $this->autoView  = false;  
  136.         $data['msg']    = $msg;  
  137.         $data['href']   = $href;  
  138.         $data['time']   = $time;  
  139.         $this->render($view$data);  
  140.         if ($exit)  
  141.         {  
  142.             Yii::app()->end();  
  143.         }  
  144.     }  
  145. }  


使用方法:

例子:新做了菜單http://localhost/index.php?r=default/site/index菜單。操作有delete、create、update

步驟:

1、向綜合后臺(tái)管理員申請(qǐng)菜單權(quán)限和菜單操作權(quán)限(110101、11010101[刪除]、11010102[新建]、11010103[修改])

2、在protected/config/purview.php 文件中為對(duì)應(yīng)的action配置權(quán)限ID

  1. return array(  
  2.     'default'=>array(  
  3.         'site'=>array(  
  4.             'index'=>110101,  
  5.             'delete'=>11010101,  
  6.             'create'=>11010102,  
  7.             'update'=>11010103  
  8.         )  
  9.     )  
  10. );  

3、完成以上功能,基本已經(jīng)完成了權(quán)限的配置,但是假如在用戶沒(méi)有某操作權(quán)限的時(shí)候,需要隱藏操作鏈接的時(shí)候,我們可以做一下操作

  1. //表格內(nèi)容  
  2. $this->widget('zii.widgets.grid.CGridView'array(  
  3.     'dataProvider'=>$model->search(),  
  4.     'columns'=>array(  
  5.         'id',  
  6.         'start_dt',  
  7.         'end_dt',  
  8.         array(  
  9.             'class'=>'CButtonColumn',  
  10.             'template'=>'{update} {delete}',  
  11.             'updateButtonOptions'=>array(  
  12.                 'onclick'=>'$.fn.sinaShow.openWindow("節(jié)目修改", this.href); return false;',  
  13.             ),  
  14.             'buttons'=>array(  
  15.                 'update'=>array(  
  16.                     'visible'=>$this->checkPower('update')  
  17.                 ),  
  18.                 'delete'=>array(  
  19.                     'visible'=>$this->checkPower('delete')  
  20.                 ),  
  21.             )  
  22.         ),  
  23.     )  
  24. ));  


在這里的visible表達(dá)式中設(shè)置調(diào)用$this->checkPower('操作名');就可以隱藏沒(méi)有權(quán)限訪問(wèn)的菜單了


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多