需求:公司擁有一套用戶權(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文件 -
-
-
-
- class JController extends CController
- {
-
-
-
-
- public $layout='//layouts/column1';
-
-
-
- public $menu=array();
-
-
-
-
-
- public $breadcrumbs = array();
-
- public $view = array();
-
- public $autoView = false;
-
- public $renderPage = '';
-
- public $notice = '';
-
- public $searchTag = array();
-
- public $otherHtml = '';
-
- public $buttonTag = array();
-
- public $unitTag = '';
-
- public $alertText = '';
-
- public $haveBorder = true;
-
- public function init()
- {
- $cookie = Yii::app()->request->getCookies();
- Yii::app()->user->id = $cookie->itemAt('SSD_user_id')->value;
- Yii::app()->user->name = $cookie->itemAt('SSD_user_nick')->value;
- }
-
-
-
-
-
-
- public function checkPower($action)
- {
- return "purviewPcc::model()->checkPower('{$this->getModule()->getId()}', '{$this->getId()}', '{$action}')";
- }
-
-
-
-
-
-
-
-
- public function checkPowerEx($action, $contrl=null, $module=null)
- {
- if ($contrl === null)
- {
- $contrl = $this->getId();
- }
-
- if ($module === null)
- {
- $module = $this->getModule()->getId();
- }
-
- return purviewPcc::model()->checkPower($module, $contrl, $action);
- }
-
-
-
-
-
- public function purview($module, $control, $action)
- {
- if (!purviewPcc::model()->checkPurview($module,$control,$action))
- {
- echo '沒(méi)有訪問(wèn)權(quán)限!';
- Yii::app()->end();
- }
- }
-
-
-
-
-
-
-
- public function beforeAction($action)
- {
- if($action && $this->getModule())
- $this->purview($this->getModule()->getId(), $this->getId(), $action->getId());
- return true;
- }
-
-
-
-
-
-
-
- public function afterAction($action)
- {
-
- if ($this->autoView)
- {
-
- if (empty($this->renderPage))
- $this->renderPage = $action->getId();
- $this->render($this->renderPage, $this->view);
- }
- }
-
-
-
-
-
-
-
-
- public function alert($msg, $href = 'javascript:history.go(-1);', $time = 0, $exit = true, $view = '//system/alert', $data = array())
- {
- $this->autoView = false;
- $data['msg'] = $msg;
- $data['href'] = $href;
- $data['time'] = $time;
- $this->render($view, $data);
- if ($exit)
- {
- Yii::app()->end();
- }
- }
- }
使用方法:例子:新做了菜單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
- return array(
- 'default'=>array(
- 'site'=>array(
- 'index'=>110101,
- 'delete'=>11010101,
- 'create'=>11010102,
- 'update'=>11010103
- )
- )
- );
3、完成以上功能,基本已經(jīng)完成了權(quán)限的配置,但是假如在用戶沒(méi)有某操作權(quán)限的時(shí)候,需要隱藏操作鏈接的時(shí)候,我們可以做一下操作
-
- $this->widget('zii.widgets.grid.CGridView', array(
- 'dataProvider'=>$model->search(),
- 'columns'=>array(
- 'id',
- 'start_dt',
- 'end_dt',
- array(
- 'class'=>'CButtonColumn',
- 'template'=>'{update} {delete}',
- 'updateButtonOptions'=>array(
- 'onclick'=>'$.fn.sinaShow.openWindow("節(jié)目修改", this.href); return false;',
- ),
- 'buttons'=>array(
- 'update'=>array(
- 'visible'=>$this->checkPower('update')
- ),
- 'delete'=>array(
- 'visible'=>$this->checkPower('delete')
- ),
- )
- ),
- )
- ));
在這里的visible表達(dá)式中設(shè)置調(diào)用$this->checkPower('操作名');就可以隱藏沒(méi)有權(quán)限訪問(wèn)的菜單了
|