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

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. 关于学习HTML5中自己犯的错误

    7.1写错了 siblings()这个函数写成了sibling,在jQuery中并没有这个函数的定义 在查找错误的过程中,自己也发现了一个学习jQuery的网站http://www.365mini.c ...

  2. demo_07选择器练习

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. HBase安装inAction

    在安装Hbase之前,需要有hadoop的运行环境,关于hadoop的安装过程,请查看我之前的blog:hadoop安装笔记:或者另一个博主的超详细文章http://weixiaolu.iteye.c ...

  4. XMLHttpRequest 使用概括

    ***********************************************XMLHttpRequest对象初始化:********************************* ...

  5. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  6. Word图片显示不完整

    选中图片和上下文字,段落里选择单倍行距,其他行距不行.

  7. Kaggle Competition Past Solutions

    Kaggle Competition Past Solutions We learn more from code, and from great code. Not necessarily alwa ...

  8. LibLinear(SVM包)的MATLAB安装

    LibLinear(SVM包)的MATLAB安装 1 LIBSVM介绍 LIBSVM是众所周知的支持向量机分类工具包(一些支持向量机(SVM)的开源代码库的链接及其简介),运用方便简单,其中的核函数( ...

  9. Google面试题之100层仍两个棋子

    一道Google面试题,题目如下:"有一个100层高的大厦,你手中有两个相同的玻璃围棋子.从这个大厦的某一层扔下围棋子就会碎,用你手中的这两个玻璃围棋子,找出一个最优的策略,来得知那个临界层 ...

  10. Nginx配置性能优化(转)

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...