yii 权限分级式访问控制的实现(非RBAC法)——已验证
验证和授权——官方文档:
http://www.yiichina.com/guide/topics.auth
http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.auth
相关类参考手册:
http://www.yiichina.com/api/CWebUser
http://www.yiichina.com/api/CAccessRule
http://www.yiichina.com/api/CUserIdentity
可参考文章:
http://my.oschina.net/u/873762/blog/98697
http://www.yiiframework.com/wiki/60/
yii 权限分级式访问控制的实现(非RBAC法)
主要参考资料来源:yii官网http://www.yiiframework.com/wiki/60/ 我只是做了小小的完善。
yii framework 提供了2套权限访问系统,一套是简单的filter(过滤器)模式,另一套是复杂全面的RBAC模式,我这里要讲的是第一套(因为我也刚刚学到这里)。如 果你有研究过YII官方的demo blog,一定知道,比如,由gii自动生成的user模块,自动附带了简单的filter权限分配功能,具体细节请参照blog手册的“用户验证”一章 节,以及yii官方指南的“验证和授权”一章节。(注意,我这里所指的模块,只是我个人对与user有关的文件的统称,与yii文件系统的模块 (module)含义不同。)
关于权限分配的文件大多在controllers里,比如打开UserController.php文件你会看到2个类函数。
public function filters()
{
return array(
'accessControl', // 实现CRUD操作的访问控制。
'postOnly + delete',
);
} public function accessRules() //这里就是访问规则的设置。
{
return array(
array('allow', // 允许所有用户执行index,view动作。
'actions'=>array('index','view'),
'users'=>array('*'), <span></span>
),
array('allow', // 只允许经过验证的用户执行create, update动作。
'actions'=>array('create','update'),
'users'=>array('@'), // @号指所有注册的用户
),
array('allow', // 只允许用户名是admin的用户执行admin,delete动作
'actions'=>array('admin','delete'),
'users'=>array('admin'),
), //admin就是指用户名是admin的用户,以硬编码的形式分配用户权限。
array('deny', // 拒绝所有的访问。
'users'=>array('*'),
),
);
}
关于更多的访问规则的设定请参照官方文件http://www.yiiframework.com/doc/api/1.1/CAccessControlFilter
好了,现在要开始按照我们自己的需求设置适合自己的权限分配了。我们希望filter访问控制模式能更完美一点,按照常识,我们希望它能按照数据库里user表里不同级别用户,实行不同的授权,而不是用硬编码的形式控制。
回到demo blog,我先对数据库的tbl_user表做修改,在原来的基础上加上role一项。对原来的用户信息记录添加role的value为"管理员"或"一般用户"("admin"或"user")。
然后依次执行以下3个步骤:
1. 创建组件WebUser,它是对CWebUser的扩展。
2. 修改config/main.php文件。
3.修改accessRules()。
具体细节如下:
1.WebUser.php 组件代码:
在protected\components\ 下新建WebUser.php
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*
* this file must be stored in:
* protected/components/WebUser.php
*/
class WebUser extends CWebUser
{ // Store model to not repeat query.
private $_model; /**
* @return first name.
* @access it by Yii::app()->user->first_name
*/
public function getFirst_Name()
{
$user = $this->loadUser(Yii::app()->user->id);
return $user->first_name;
} /**
* This is a function that checks the field 'role'
* in the User model to be equal to 1, that means it's admin
* @return boolean
* @access it by Yii::app()->user->isAdmin()
*/
public function isAdmin()
{
$user = $this->loadUser(Yii::app()->user->id);
if ($user == null) {
return 0;
} else {
return $user->role == "admin";
} } /**
* Load user model.
* Returns the data model based on the primary key given in the GET variable.
* @param integer $id the ID of the model to be loaded
* @return User the loaded model
*/
protected function loadUser($id = null)
{
if ($this->_model === null) {
if ($id !== null) {
$this->_model = User::model()->findByPk($id);
}
}
return $this->_model;
} /**
* This method is called after the user is successfully logged in.
* You may override this method to do some postprocessing (e.g. log the user
* login IP and time; load the user permission information).
* @param boolean $fromCookie whether the login is based on cookie.
*/
public function afterLogin($fromCookie)
{
//Yii::app()->request->redirect('/index.php/user/create'); if(!Yii::app()->user->isGuest){
$uid = Yii::app()->user->id;
$uip = Yii::app()->request->userHostAddress; //获取用户IP
User::model()->updateAll(array('logintime'=>time(), 'loginip'=>$uip), 'id=:id', array(':id'=>$uid));
} parent::afterLogin($fromCookie);
} /**
* This method is invoked right after a user is logged out.
* You may override this method to do some extra cleanup work for the user.
*/
/*
public function afterLogout()
{
//Yii::app()->request->redirect('/index.php/user/index'); parent::afterLogout();
}
*/
}
2.在config/main.php找到如下代码,添加标红色的代码。
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
'class'=>'WebUser',
),
)
3.找到需要更改权限的controller类,对accessRules()函数做修改,比如对前文的accessRules()函数做如下修改:
public function accessRules() //这里就是访问规则的设置。
{
return array(
array('allow', // 允许所有用户执行index,view动作。
'actions'=>array('index','view'),
'users'=>array('*'), //*号标识所有用户包括注册的、没注册的、一般的、管理员级的
),
array('allow', // 只允许经过验证的用户执行create, update动作。
'actions'=>array('create','update'),
'users'=>array('@'), // @号指所有注册的用户
),
array('allow',
'actions'=>array('admin','delete'),
/**
* expression: 设定一个PHP表达式。它的值用来表明这条规则是否适用。
* 在表达式,你可以使用一个叫$user的变量,它代表的是Yii::app()->user。
* 这个选项是在1.0.3版本里引入的。
* 'expression' => '$user->isAdmin()', //即这样也可以
* 'expression' => '$user->isAdmin() || $user->isAuthor()', //也可以加多条判断
*/
'expression' => 'yii::app()->user->isAdmin()',
//这样只有标识为“管理员”的用户才能访问admin,delete动作
),
array('deny', // 拒绝所有的访问。
'users'=>array('*'),
),
);
}
工作完成!
From: http://my.oschina.net/u/873762/blog/98697
附:
官网blog Demo 验证修改:
在protected\components\ 下新建UserIdentity.php
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
private $_id; public function authenticate()
{
//$record = User::model()->findByAttributes(array('id' => Yii::app()->user->id));
$record = User::model()->findByAttributes(array('username' => $this->username));
if ($record === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
/*elseif ($record->password !== md5($this->password)) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}*/
elseif ($record->password !== $this->password) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
else {
$this->_id = $record->id;
//$this->setState('roles', $record->role); //未生效
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
} public function getId()
{
return $this->_id;
}
} ?>
在用户登陆时则调用如下代码:
$identity = new UserIdentity($username,$password);
if($identity->authenticate()) {
Yii::app()->user->login($identity);
} else {
echo $identity->errorMessage;
}
在用户退出时调用了Yii::app()->user->logout();
yii 权限分级式访问控制的实现(非RBAC法)——已验证的更多相关文章
- Yii 权限分级式访问控制实现(非RBAC法)
以下由我们在信易网络公司开发项目的时候终结出的一些经验 主要参考资料:yii官网http://www.yiiframework.com/wiki/60/yii framework 提供了2套权限访问系 ...
- YIi 权限管理和基于角色的访问控制
验证和授权(Authentication and Authorization) 定义身份类 (Defining Identity Class) 登录和注销(Login and Logout) 访问控制 ...
- Nagios ’status.cgi‘文件权限许可和访问控制漏洞
漏洞名称: Nagios ’status.cgi‘文件权限许可和访问控制漏洞 CNNVD编号: CNNVD-201307-013 发布时间: 2014-02-21 更新时间: 2014-02-21 危 ...
- OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞
漏洞名称: OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞 CNNVD编号: CNNVD-201311-117 发布时间: 2013-11-12 更新时间: 2 ...
- Spring Security实现基于RBAC的权限表达式动态访问控制
昨天有个粉丝加了我,问我如何实现类似shiro的资源权限表达式的访问控制.我以前有一个小框架用的就是shiro,权限控制就用了资源权限表达式,所以这个东西对我不陌生,但是在Spring Securit ...
- 算法笔记_013:汉诺塔问题(Java递归法和非递归法)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...
- 权限系统设计(0):权限系统设计基本概念改需-MAC/RBAC引子
此篇主要对权限系统设计所涉的一些专业术语重点梳理.从我们windows的文件系统 自主访问控制 到基于角色访问控制. 权限设计基本术语 对后面会用到的词汇做一个简要说明 什么是权限(许可) 权限(Pr ...
- sshpass-Linux命令之非交互SSH密码验证
sshpass-Linux命令之非交互SSH密码验证 参考网址:https://www.cnblogs.com/chenlaichao/p/7727554.html ssh登陆不能在命令行中指定密码. ...
- 【转】sshpass-Linux命令之非交互SSH密码验证
sshpass-Linux命令之非交互SSH密码验证 ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这一问题.sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次 ...
随机推荐
- 如何在Ubuntu上创建及管理LXC容器?
将LXC安装到Ubuntu上 $ sudo apt-get install lxc 安装完毕之后,运行lxc-checkconifg工具,检查当前Linux内核支持LXC的情况.要是一切都已被启用,内 ...
- c#面向对象小结
特点: 1:将复杂的事情简单化. 2:面向对象将以前的过程中的执行者,变成了指挥者. 3:面向对象这种思想是符合现在人们思考习惯的一种思想. 过程和对象在我们的程序中是如何体现的呢?过程其实就是函数: ...
- SQLServer数据库 导出表和导入sql脚本
1.选择需要导出表的数据库--任务---生成脚本 2.显示:生成和发布脚本窗口--简介(某些可能关闭该页面的,可以省略该步骤),点击下一步 3.显示:生成和发布脚本窗口--选择对象--按照图片操作即可 ...
- mac下Apache + MySql + PHP网站开发
最近接了个小活,做一个使用PHP语言和MySql数据库的动态网站.之前做过类型的网站,是在windows系统下做的,开发环境使用的是 AppServ 的PHP开发套件.现在有了我的大MAC,所以找了M ...
- Xcode 修改工程名称
总会遇到几个项目,在做到一半的时候被要求改项目名,网上找了下相关的资料,大多数是xcode5以前的版本,所以解决好了在这里mark一下,给需要的人. 目标为:将项目名XCD4改成xcd5. 先上结果图 ...
- Dom学习笔记-(一)
一.概述 DOM(文档对象模型)是针对HTML和XML文档的一个API,其脱胎于DHTML. DOM可以将任意HTML和XML文档描绘成一个由多层节点构成的结构. 每一个文档包含一个根节点-文档节点, ...
- HTML5+CSS3鼠标悬停图片特效
点击预览效果 下载该特效: HTML5+CSS3鼠标悬停图片特效.zip 特效说明: 一款HTML5+CSS3鼠标悬停图片事件网页特效,集合了最流行鼠标悬停图片文字.图片动画移动 ...
- 设置UIButton的文字居右显示 去掉点击默认置灰效果
1.设置UIButton的文字居右显示 [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; ...
- matplotlib 显示中文
# --*-- coding: utf-8 --*-- from matplotlib.font_manager import FontProperties import matplotlib.pyp ...
- Hbase热点问题
需求描述:扫描(查询)某个区间--->列用hbase多节点的资源,分布式扫描,加快速度==> 然后拼接到一起 如何打散数据 冠字号逆序,hash 并不一定数据连续就会造成热点,这个是由数据 ...