Yii框架权限控制

45 篇文章 1 订阅
9 篇文章 1 订阅

需求:公司拥有一套用户权限系统。我们在新版框架中,我们需要兼容这套用户权限系统。

 

问题:YII单表方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持数据库分表设计

 

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

 

[php]  view plain copy
 
  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 路径设置 
  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.     //视图数据  
  23.     public $view        = array();  
  24.     //是否自动输出  
  25.     public $autoView    = false;  
  26.     //输出页面  
  27.     public $renderPage  = '';  
  28.     //页面提示文字  
  29.     public $notice  = '';  
  30.     //搜索标签  
  31.     public $searchTag   = array();  
  32.     //其他代码  
  33.     public $otherHtml   = '';  
  34.     //按钮标签  
  35.     public $buttonTag   = array();  
  36.     //单位标签  
  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.      * 判断是否有指定操作的权限 
  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.      * 检查权限扩展 
  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.      * 权限判断 
  84.      * 
  85.      */  
  86.      public function purview($module$control$action)  
  87.      {  
  88.         if (!purviewPcc::model()->checkPurview($module,$control,$action))  
  89.         {  
  90.             echo '没有访问权限!';  
  91.             Yii::app()->end();  
  92.         }  
  93.      }  
  94.   
  95.     /** 
  96.      * Action操作前动作 
  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操作后动作 
  111.      * 
  112.      * @param string $action 
  113.      */  
  114.     public function afterAction($action)  
  115.     {  
  116.         /** 是否自动输出 */  
  117.         if ($this->autoView)  
  118.         {  
  119.             //默认输入页面  
  120.             if (empty($this->renderPage))  
  121.                 $this->renderPage    = $action->getId();            
  122.             $this->render($this->renderPage, $this->view);  
  123.         }  
  124.     }  
  125.       
  126.     /** 
  127.      * 页面提示窗口 
  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、向综合后台管理员申请菜单权限和菜单操作权限(110101、11010101[删除]、11010102[新建]、11010103[修改])

2、在protected/config/purview.php 文件中为对应的action配置权限ID

 

[php]  view plain copy
 
  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、完成以上功能,基本已经完成了权限的配置,但是假如在用户没有某操作权限的时候,需要隐藏操作链接的时候,我们可以做一下操作

 

 

[php]  view plain copy
 
  1. //表格内容  
  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("节目修改", 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表达式中设置调用$this->checkPower('操作名');就可以隐藏没有权限访问的菜单了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值