yii权限控制的方法(三种方法)


Posted in PHP onDecember 28, 2015

本文实例讲述了yii权限控制的方法。分享给大家供大家参考,具体如下:

这里摘录以下3种:

1. 通过accessControl:

public function filters()
{
  return array(
    'accessControl', // perform access control for CRUD operations
  );
}
/**
 * Specifies the access control rules.
 * This method is used by the 'accessControl' filter.
 * @return array access control rules
 */
public function accessRules()
{
  return array(
    array('allow', // allow authenticated users to access all actions
      'users'=>array('@'),
    ),
    array('deny', // deny all users
      'users'=>array('*'),
    ),
  );
}

2. 通过插件(如:right)

public function filters()
{
  return array(
    'rights',
  );
}

3. 混合模式:

/**
 * @return array action filters
 */
public function filters()
{
  return array(
    'updateOwn + update', // Apply this filter only for the update action.
    'rights',
  );
}
/**
 * Filter method for checking whether the currently logged in user
 * is the author of the post being accessed.
 */
public function filterUpdateOwn($filterChain)
{
  $post=$this->loadModel();
  // Remove the 'rights' filter if the user is updating an own post
  // and has the permission to do so.
  if(Yii::app()->user->checkAccess('PostUpdateOwn', array('userid'=>$post->author_id)))
    $filterChain->removeAt(1);
  $filterChain->run();
}

如果有权限的基础上,开放某些动作的权限,可以通过allowedActions:

public function allowedActions()
{
  return 'autocomplate,autocomplate2';
}

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

PHP 相关文章推荐
php之Memcache学习笔记
Jun 17 PHP
深入解析PHP中逗号与点号的区别
Aug 05 PHP
PHP取余函数介绍MOD(x,y)与x%y
May 15 PHP
PHP采用XML-RPC构造Web Service实例教程
Jul 16 PHP
PHP实现的QQ空间g_tk加密算法
Jul 09 PHP
再推荐十款免费的php开发工具
Nov 09 PHP
PHP微信开发之模板消息回复
Jun 24 PHP
php及codeigniter使用session-cookie的方法(详解)
Apr 06 PHP
php中时间函数date及常用的时间计算
May 12 PHP
PHP异常处理定义与使用方法分析
Jul 25 PHP
分享5个非常有用的Laravel Blade指令
May 30 PHP
用Laravel轻松处理千万级数据的方法实现
Dec 25 PHP
Yii使用Captcha验证码的方法
Dec 28 #PHP
yii使用activeFileField控件实现上传文件与图片的方法
Dec 28 #PHP
yii实现使用CUploadedFile上传文件的方法
Dec 28 #PHP
Yii中Model(模型)的创建及使用方法
Dec 28 #PHP
yii数据库的查询方法
Dec 28 #PHP
yii分页组件用法实例分析
Dec 28 #PHP
PHP读取文件内容的五种方式
Dec 28 #PHP
You might like
PHP中spl_autoload_register()和__autoload()区别分析
2014/05/10 PHP
Yii2简单实现给表单添加验证码的方法
2016/07/18 PHP
php flush无效,IIS7下php实时输出的方法
2016/08/25 PHP
在Firefox下js select标签点击无法弹出
2014/03/06 Javascript
让html页面不缓存js的实现方法
2014/10/31 Javascript
轻松创建nodejs服务器(10):处理上传图片
2014/12/18 NodeJs
跟我学习javascript的隐式强制转换
2015/11/16 Javascript
JavaScript中的继承之类继承
2016/05/01 Javascript
基于CSS3和jQuery实现跟随鼠标方位的Hover特效
2016/07/25 Javascript
jQuery获取file控件中图片的宽高与大小
2016/08/04 Javascript
jquery自动补齐功能插件flexselect用法示例
2016/08/06 Javascript
Vue 2.X的状态管理vuex记录详解
2017/03/23 Javascript
详解AngularJS 路由 resolve用法
2017/04/24 Javascript
VUE重点问题总结
2018/03/19 Javascript
用ES6写全屏滚动插件的示例代码
2018/05/02 Javascript
详解Puppeteer 入门教程
2018/05/09 Javascript
20个最常见的jQuery面试问题及答案
2018/05/23 jQuery
React-router4路由监听的实现
2018/08/07 Javascript
js实现简单模态框实例
2018/11/16 Javascript
Vue computed 计算属性代码实例
2020/04/22 Javascript
Python自动发邮件脚本
2017/03/31 Python
在CMD命令行中运行python脚本的方法
2018/05/12 Python
python 统计数组中元素出现次数并进行排序的实例
2018/07/02 Python
对Pyhon实现静态变量全局变量的方法详解
2019/01/11 Python
基于wxPython的GUI实现输入对话框(1)
2019/02/27 Python
使用Python第三方库pygame写个贪吃蛇小游戏
2020/03/06 Python
使用Pycharm在运行过程中,查看每个变量的操作(show variables)
2020/06/08 Python
python通过函数名调用函数的几种场景
2020/09/23 Python
护理专业自我鉴定
2014/01/30 职场文书
舞蹈专业大学生职业规划范文
2014/03/12 职场文书
年终工作总结范文2014
2014/11/27 职场文书
电影建党伟业观后感
2015/06/01 职场文书
住房公积金贷款工资证明
2015/06/12 职场文书
2016春季幼儿园大班开学寄语
2015/12/03 职场文书
springboot如何初始化执行sql语句
2021/06/22 Java/Android
浅析Python中的随机采样和概率分布
2021/12/06 Python