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计划任务之关闭浏览器后仍然继续执行的函数
Jul 22 PHP
延长phpmyadmin登录时间的方法
Feb 06 PHP
PHP 计算代码执行耗时的代码修正网上普遍错误
May 14 PHP
深入理解PHP中的Streams工具
Jul 03 PHP
使用PHP和JavaScript判断请求是否来自微信内浏览器
Aug 18 PHP
PHP+jQuery翻板抽奖功能实现
Oct 19 PHP
php实现生成验证码实例分享
Apr 10 PHP
PHP最常用的正则表达式
Feb 13 PHP
PHP读取Excel类文件
May 15 PHP
PHP根据key删除数组中指定的元素
Feb 28 PHP
laravel框架模型、视图与控制器简单操作示例
Oct 10 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
Feb 27 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
图书管理程序(二)
2006/10/09 PHP
基于mysql的论坛(2)
2006/10/09 PHP
浅析php中jsonp的跨域实例
2013/06/21 PHP
浅析php与数据库代码开发规范
2013/08/08 PHP
php获取指定日期之间的各个周和月的起止时间
2014/11/24 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
zen cart实现订单中增加paypal中预留电话的方法
2016/07/12 PHP
jquery 1.4.2发布!主要是性能与API
2010/02/25 Javascript
JS Date函数整理方便使用
2013/10/23 Javascript
原生javascript实现图片弹窗交互效果
2015/01/12 Javascript
JS组件Bootstrap Table使用方法详解
2016/02/02 Javascript
基于JavaScript实现树形下拉框
2016/08/10 Javascript
JavaScript学习笔记整理_简单实现枚举类型,扑克牌应用
2016/09/19 Javascript
最常用的jQuery表单验证(简单)
2017/05/23 jQuery
彻底搞懂JavaScript中的apply和call方法(必看)
2017/09/18 Javascript
javaScript实现滚动条事件详解
2020/03/24 Javascript
angular2中Http请求原理与用法详解
2018/01/11 Javascript
JavaScript函数式编程(Functional Programming)纯函数用法分析
2019/05/22 Javascript
微信小程序使用Vant Weapp组件库的方法步骤
2019/08/01 Javascript
基于layui框架响应式布局的一些使用详解
2019/09/16 Javascript
JS随机密码生成算法
2019/09/23 Javascript
python-docx修改已存在的Word文档的表格的字体格式方法
2018/05/08 Python
python机器学习之KNN分类算法
2018/08/29 Python
Python 窗体(tkinter)按钮 位置实例
2019/06/13 Python
Django之路由层的实现
2019/09/09 Python
python的mysql数据库建立表与插入数据操作示例
2019/09/30 Python
css 省略号 css3让多余的字符串消失并附加省略号的实现代码
2013/02/07 HTML / CSS
美国最大的旗帜经销商:Carrot-Top
2018/02/26 全球购物
解释DataSet(ds) 和 ds as DataSet 的含义
2014/07/27 面试题
银行委托书范本
2014/04/04 职场文书
青年文明号口号
2014/06/17 职场文书
党员批评与自我批评
2014/10/15 职场文书
项目安全员岗位职责
2015/02/15 职场文书
车间质检员岗位职责
2015/04/08 职场文书
2019送给家人们的中秋节祝福语
2019/08/15 职场文书
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB