需求:公司拥有一套用户权限系统。我们在新版框架中,我们需要兼容这套用户权限系统。
问题:YII单表方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持数据库分表设计
解决方法:1、新建protected/sinashowExt/JController.php文件
- /**
- * 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 路径设置
- * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
- * for more details on how to specify this property.
- */
- 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;
- }
- /**
- * 判断是否有指定操作的权限
- *
- * @param string $action
- */
- public function checkPower($action)
- {
- return "purviewPcc::model()->checkPower('{$this->getModule()->getId()}', '{$this->getId()}', '{$action}')";
- }
- /**
- * 检查权限扩展
- *
- * @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);
- }
- /**
- * 权限判断
- *
- */
- public function purview($module, $control, $action)
- {
- if (!purviewPcc::model()->checkPurview($module,$control,$action))
- {
- echo '没有访问权限!';
- Yii::app()->end();
- }
- }
- /**
- * Action操作前动作
- *
- * @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操作后动作
- *
- * @param string $action
- */
- public function afterAction($action)
- {
- /** 是否自动输出 */
- if ($this->autoView)
- {
- //默认输入页面
- if (empty($this->renderPage))
- $this->renderPage = $action->getId();
- $this->render($this->renderPage, $this->view);
- }
- }
- /**
- * 页面提示窗口
- *
- * @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、向综合后台管理员申请菜单权限和菜单操作权限(110101、11010101[删除]、11010102[新建]、11010103[修改])
2、在protected/config/purview.php 文件中为对应的action配置权限ID
- return array(
- 'default'=>array(
- 'site'=>array(
- 'index'=>110101,
- 'delete'=>11010101,
- 'create'=>11010102,
- 'update'=>11010103
- )
- )
- );
3、完成以上功能,基本已经完成了权限的配置,但是假如在用户没有某操作权限的时候,需要隐藏操作链接的时候,我们可以做一下操作
- //表格内容
- $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("节目修改", this.href); return false;',
- ),
- 'buttons'=>array(
- 'update'=>array(
- 'visible'=>$this->checkPower('update')
- ),
- 'delete'=>array(
- 'visible'=>$this->checkPower('delete')
- ),
- )
- ),
- )
- ));
在这里的visible表达式中设置调用$this->checkPower('操作名');就可以隐藏没有权限访问的菜单了