验证和授权——官方文档:

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法)——已验证的更多相关文章

  1. Yii 权限分级式访问控制实现(非RBAC法)

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 主要参考资料:yii官网http://www.yiiframework.com/wiki/60/yii framework 提供了2套权限访问系 ...

  2. YIi 权限管理和基于角色的访问控制

    验证和授权(Authentication and Authorization) 定义身份类 (Defining Identity Class) 登录和注销(Login and Logout) 访问控制 ...

  3. Nagios ’status.cgi‘文件权限许可和访问控制漏洞

    漏洞名称: Nagios ’status.cgi‘文件权限许可和访问控制漏洞 CNNVD编号: CNNVD-201307-013 发布时间: 2014-02-21 更新时间: 2014-02-21 危 ...

  4. OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞

    漏洞名称: OpenSSH ‘mm_newkeys_from_blob’函数权限许可和访问控制漏洞 CNNVD编号: CNNVD-201311-117 发布时间: 2013-11-12 更新时间: 2 ...

  5. Spring Security实现基于RBAC的权限表达式动态访问控制

    昨天有个粉丝加了我,问我如何实现类似shiro的资源权限表达式的访问控制.我以前有一个小框架用的就是shiro,权限控制就用了资源权限表达式,所以这个东西对我不陌生,但是在Spring Securit ...

  6. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  7. 权限系统设计(0):权限系统设计基本概念改需-MAC/RBAC引子

    此篇主要对权限系统设计所涉的一些专业术语重点梳理.从我们windows的文件系统 自主访问控制 到基于角色访问控制. 权限设计基本术语 对后面会用到的词汇做一个简要说明 什么是权限(许可) 权限(Pr ...

  8. sshpass-Linux命令之非交互SSH密码验证

    sshpass-Linux命令之非交互SSH密码验证 参考网址:https://www.cnblogs.com/chenlaichao/p/7727554.html ssh登陆不能在命令行中指定密码. ...

  9. 【转】sshpass-Linux命令之非交互SSH密码验证

      sshpass-Linux命令之非交互SSH密码验证 ssh登陆不能在命令行中指定密码.sshpass的出现,解决了这一问题.sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次 ...

随机推荐

  1. 【转】CHAR CHARACTER VARCHAR NCHAR NVARCHAR NVARCHAR2区别

    http://blog.csdn.net/lhl6688/article/details/44156823?ref=myread oracle提供了五种字符数据类型:char.nchar.varcha ...

  2. #Leet Code# Best Time to Buy and Sell Stock

    描述:数组 A,对于 i < j, 找到最大的 A[j] - A[i] 代码: class Solution: # @param prices, a list of integer # @ret ...

  3. zzuli oj 1145 有问题的里程表 2

    Description 某辆汽车有一个里程表,该里程表可以显示一个整数,为该车走过的公里数.然而这个里程表有个毛病:它总是从3变到5,而跳过数字4,里程表所有位(个位. 十位.百位等)上的数字都是如此 ...

  4. after I see Little Dorrit

    也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...

  5. 定位 - CoreLocation - 打印位置信息

    1. 导入框架 <CoreLocation.framework>, 引入头文件 import <CoreLocation/CoreLocation.h>; 2. 创建管理者对象 ...

  6. python下redis的基本操作:

    1. 基本操作: >>> import redis >>> print redis.__file__ /usr/local/lib/python2.7/dist-p ...

  7. 【技术贴】解决vss中提交pdf下载打开空白乱码

    vss客户端需要安装一个Vss2005的补丁程序,而且之前上传的pdf文件重新删掉,再次上传进Vss中,再下载打卡就ok了. 补丁名称vs80-kb943847-x86-intl.exe 别人的csd ...

  8. ECMall模板开发文档

    ECMall 模板开发文档 前 言 欢迎阅读 ECMall 模板制作教程,通过阅读本教程可快速上手 ECMall 模板的使用和制作. ECMall 模板制 作要求用户具备 XML . XHTML 和 ...

  9. 太受不了了,,REST_FRAMEWORK太方便啦~~

    按英文原始的DOCUMENT走一圈,从最手工的输出到高度的集成. 最后真的就几行代码,实现最常用的JSON API..纯RESTFUL风格. 但,其核心是要记住序列化生反序列的过程,都是要以PYTHO ...

  10. CAS单点登录配置[1]:准备工作

    关于CAS是什么这里就不在赘述,网友将它比喻成旅游景点的套票,买了一个套票就可以观看所有景点,不需要一个景点买一次票...我们重点介绍CAS单点登录的配置. 工具/原料 1.配置好JDK环境,否则不方 ...