`
dcj3sjt126com
  • 浏览: 1855580 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

YII学习第十天,访问控制filter

    博客分类:
  • YII
yii 
阅读更多

在Controller.php 

注:filters是yii的CController.php中定义的方法,而Controller.php是继承CController.php的。此处,相当于覆盖父类中的filters方法。而所有的XXController.php又继承自Controller.php,显然都可以定义authlessActions()方法覆盖父类中相应方法。 

public function filterAccessAuth($filterChain) {
		if(Yii::app()->user->getIsGuest() && !in_array($this->getAction()->getId(), $this->authlessActions())) {
			Yii::app()->user->setFlash("info", "请先登录");
			Yii::app()->user->loginRequired();  //封装了Yii::app()->user->loginUrl
		}
		elseif(!in_array($this->getAction()->getId(), $this->authlessActions()) && $this->current_user && $this->current_user->isPasswordExpired()) {
			$this->user->setFlash('error', "你的密码已经过期,超过: " . Yii::app()->params['user_pwd_max_expire_day'] . "天没有修改,请修改密码");
			$this->redirect($this->createUrl("/account/profile"));
		}
		
		if(!in_array($this->getAction()->getId(), $this->authlessActions()) && $this->current_user && $this->current_user->hi_id == NULL) {
			$target_url = $this->createUrl('account/profile');
			$this->user->setFlash('info', "你还没有设置Hi,请尽快到" . "<a href=\"$target_url\">  账号设置  </a>" . "添加!");	
		}

		$filterChain->run();
	}

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

	public function authlessActions() {
		return array();
	}

 批注1:yii中使用filter规则。 

For method-based filters, a method named 'filterXYZ($filterChain)' in the controller class will be executed, where 'XYZ' stands for the filter name as specified in filters(). Note, inside the filter method, you must call $filterChain->run() if the action should be executed. Otherwise, the filtering process would stop at this filter. 





批注2:另一种用法(隐式用法) 
在1.1.7中,public function filters() { 
return array( 
'accessControl', 
); 

也即accessControl对应下面这个方法 
public void filterAccessControl(CFilterChain $filterChain) 

The filter method for 'accessControl' filter. The filter method for 'accessControl' filter. This filter is a wrapper of CAccessControlFilter. To use this filter, you must override accessRules method. 

class PostController extends CController
{
	......
	public function accessRules()
	{
		return array(
			rray('deny',
				'actions'=>array('create', 'edit'),
				'users'=>array('?'),
				),
			array('allow',
				'actions'=>array('delete'),
				'roles'=>array('admin'),
			),
			array('deny',
				'actions'=>array('delete'),
				'users'=>array('*'),
			),
		);
	}
}

 所以不难看出filter至少有两种类型,一种是accessAuth,另一种是accessControl。 




批注3:当用户访问index.php?r=site/login 

首先,映射到SiteController.php中 
而在SiteController.php中,覆盖了Controller.php的public function authlessActions()方法,不再是空数组, 
而是return array('login', 'logout', 'error', 'page', 'bashJs'); 



批注4:在actionLogin方法里 

		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login()) {
				//初始化前,先初始化父类Controller.php的成员变量和成员函数。
				$this->init();

				// 检查上次密码修改时间
				if($this->current_user->isPasswordExpired()) {
					$this->user->setFlash('error', "密码已经" . Yii::app()->params['user_pwd_max_expire_day'] . "天没有修改过了,请修改密码");	
					$this->redirect($this->createUrl("/account/profile"));
				}
				
				// 检查有没有设置Hi
				if($this->current_user->hi_id == NULL) {
					$target_url = $this->createUrl('account/profile');
					$this->user->setFlash('info', "你还没有设置Hi,请尽快到"."<a href=\"$target_url\">  账号设置  </a>"."添加!");	
				}

				$this->redirect(Yii::app()->user->returnUrl);
			}
		}

 根据最前面的方法in_array($this->getAction()->getId(), $this->authlessActions())可以判断该action是否在不用授权数组中。 


注:查看当前controller和action的ID,是Yii::app()->controller->id;和$this->getAction()->getId(); 
其中,$this->getAction()是一个很大的对象,$this->getAction()->getId()是login。 

 

批注5:关于未登录前,应该走下面这个逻辑 

if(Yii::app()->user->getIsGuest() && !in_array($this->getAction()->getId(), $this->authlessActions())) {
			Yii::app()->user->setFlash("info", "请先登录");
			Yii::app()->user->loginRequired();  //封装了Yii::app()->user->loginUrl
		}

 

根据yii框架实现机制,发现Yii::app()->user->loginRequired()封装了Yii::app()->user->loginUrl。 
即注释掉改行后,project/不会自动跳转到project/index.php?r=site/login页面。 



最后,如果用户登陆成功后,用户跳转到site/index,而其定义为outsource/index,当OutsourceController.php执行时,又一次执行Controller.php,此时的authlessActions数组为空,则in_array()为false,取反为true。执行检查上次修改密码超过30天和是否已添加Hi。 

分享到:
评论

相关推荐

    YII快速入门教程

    - 通过URL访问模块中的控制器和动作。 **9. 路径别名** - **定义**: 用于简化文件路径的引用方式。 - **用途**: 方便维护文件路径,避免硬编码路径带来的问题。 **10. 开发规范** - **URL**: 设计友好的URL。 -...

    yii快速入门教程--一个非常好的php框架

    ### Yii快速入门教程知识点 #### 一、基本概念 **1.... - **功能**: 入口文件是整个Yii应用的第一个脚本,它负责加载Yii框架以及初始化应用。...以上是基于给定部分内容的详细解析,希望对你学习Yii框架有所帮助。

    Yii2的基本应用程序模板 yii-basic-app-2.0.12

    - 授权可通过访问控制过滤器(Access Control Filter, ACF)或角色基础访问控制(Role-Based Access Control, RBAC)进行。 8. **错误处理和日志记录**: - 错误和异常处理机制,便于调试和生产环境的错误报告。 ...

    Yii快速入门手册

    3. 使用第三方库:Yii框架支持整合第三方库,以便利用已有的资源和功能。 在Yii框架中,创建应用实例并运行它时,可以使用Yii::app()方法访问应用实例。这通常在应用的任何位置进行,比如在控制器、视图或其他地方...

    yii2-platform

    - **访问控制**:通过访问规则(Access Control Filter, ACF)或角色基础访问控制(Role-Based Access Control, RBAC)限制不同用户的操作权限。 8. **错误处理与日志记录** - **异常处理**:自定义异常类,统一...

    Yii Framework PHP开发框架 v2.0.16.1

    10. **社区支持**:Yii 拥有活跃的开发者社区,提供了详细的文档、教程、示例以及众多的第三方扩展,便于开发者学习和解决问题。 在yii2-v2.0.16.1这个压缩包中,包含的是Yii 2.0框架的最新稳定版本。下载并解压后...

    FirstYii:第一PHP Yii

    8. **权限和访问控制**:了解Yii的访问控制过滤器(Access Control Filter, ACF)和角色基础访问控制(Role-Based Access Control, RBAC)。 9. **缓存机制**:学习如何使用Yii的缓存策略优化性能。 10. **...

    PHPer 第3期--草根本色

    10. PHP框架应用:如Laravel、Symfony、Yii等,它们提供了一套完整的开发结构和工具,简化了开发流程,提高了开发效率。 11. PHP持续集成和自动化部署:了解Composer依赖管理,Git版本控制,以及Jenkins等工具进行...

    php面试题及答案

    权限管理通常涉及用户角色、访问控制列表(ACL)、认证与授权的概念。面试者需要理解如何实现用户登录、权限分配和安全的权限验证。 4. **面向对象编程**: 面向对象的特性包括封装、继承、多态,何时使用面向...

    PHP实例开发源码—宗师堂软文发稿平台营销系统.zip

    10. **第三方库和框架**:为了简化开发和提高效率,开发者可能会使用Composer来管理依赖,引入如Laravel、Yii、Symfony等PHP框架,或者使用Guzzle等库进行HTTP请求。 通过对宗师堂软文发稿平台营销系统的源码学习,...

    PHP高级开发详细思维导图18张

    - 高阶函数:接受或返回函数的函数,如array_map、array_filter。 - 闭包:匿名函数,可以捕获其所在作用域的变量。 6. **魔术方法(Magic Methods)** - __construct和__destruct:对象生命周期中的构造和析构...

    第4套PHP面试题

    16. **安全性**:包括SQL注入防御(预处理语句,参数绑定),XSS(Cross-site scripting)防护,CSRF(Cross-site request forgery)防范,以及如何使用PHP内置的过滤函数如filter_var()。 17. **PHP版本更新**:...

    Modern PHP中文版_(0601).rar

    10. **PHP框架**:如Laravel、Symfony、Yii等,它们为开发者提供了一套预定义的结构和工具,加速Web应用开发,同时保持良好的编码实践。 11. **单元测试**:PHP有PHPUnit等测试框架,帮助开发者编写单元测试,确保...

    php_api

    - PHP API是PHP核心与开发者之间的一个接口,提供了多种函数和类库,使得开发者能够轻松地访问系统资源、数据库、网络服务等。 - PHP内建了大量的内置函数,如字符串处理、数组操作、文件I/O等,这些都是PHP API的...

Global site tag (gtag.js) - Google Analytics