需求:公司擁有一套用戶(hù)權(quán)限系統(tǒng)。我們?cè)谛掳婵蚣苤校覀冃枰嫒葸@套用戶(hù)權(quán)限系統(tǒng)。
問(wèn)題:YII單表方式已經(jīng)滿(mǎn)足不了我們的需求,急切需要對(duì)YII進(jìn)行擴(kuò)展設(shè)計(jì),支持?jǐn)?shù)據(jù)庫(kù)分表設(shè)計(jì)
解決方法:1、新建protected/sinashowExt/JController.php文件
[php]
view plaincopy/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class JController extends CController
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column1';
/**
* @var 菜單 {@link CMenu::items}.
*/
public $menu=array();
/**
* @var 路徑設(shè)置
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs = array();
//視圖數(shù)據(jù)
public $view = array();
//是否自動(dòng)輸出
public $autoView = false;
//輸出頁(yè)面
public $renderPage = '';
//頁(yè)面提示文字
public $notice = '';
//搜索標(biāo)簽
public $searchTag = array();
//其他代碼
public $otherHtml = '';
//按鈕標(biāo)簽
public $buttonTag = array();
//單位標(biāo)簽
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;
}
/**
* 判斷是否有指定操作的權(quán)限
*
* @param string $action
*/
public function checkPower($action)
{
return "purviewPcc::model()->checkPower('{$this->getModule()->getId()}', '{$this->getId()}', '{$action}')";
}
/**
* 檢查權(quán)限擴(kuò)展
*
* @param string $action
* @param string $contrl
* @param string $module
*/
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);
}
/**
* 權(quán)限判斷
*
*/
public function purview($module, $control, $action)
{
if (!purviewPcc::model()->checkPurview($module,$control,$action))
{
echo '沒(méi)有訪問(wèn)權(quán)限!';
Yii::app()->end();
}
}
/**
* Action操作前動(dòng)作
*
* @param unknown_type $action
* @return unknown
*/
public function beforeAction($action)
{
if($action && $this->getModule())
$this->purview($this->getModule()->getId(), $this->getId(), $action->getId());
return true;
}
/**
* Action操作后動(dòng)作
*
* @param string $action
*/
public function afterAction($action)
{
/** 是否自動(dòng)輸出 */
if ($this->autoView)
{
//默認(rèn)輸入頁(yè)面
if (empty($this->renderPage))
$this->renderPage = $action->getId();
$this->render($this->renderPage, $this->view);
}
}
/**
* 頁(yè)面提示窗口
*
* @param string $view
* @param array $data
* @param bool $exit
*/
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
[php]
view plaincopyreturn array(
'default'=>array(
'site'=>array(
'index'=>110101,
'delete'=>11010101,
'create'=>11010102,
'update'=>11010103
)
)
);
3、完成以上功能,基本已經(jīng)完成了權(quán)限的配置,但是假如在用戶(hù)沒(méi)有某操作權(quán)限的時(shí)候,需要隱藏操作鏈接的時(shí)候,我們可以做一下操作
[php]
view plaincopy//表格內(nèi)容
$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)的菜單了