yii 权限控制

本文深入探讨了权限控制的实现方式,包括通过accessControl、插件(如:right)以及混合模式来管理CRUD操作。同时,阐述了如何在有权限的基础上开放某些动作的权限,以及使用allowedActions方法来指定允许的操作。文章还提供了具体代码示例,并介绍了如何通过filter方法检查当前登录用户是否为访问帖子的作者。

摘录以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';
	}

 From http://blog.youkuaiyun.com/yuhui_fish/article/details/8191653

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值