简单学习一下Zend_ACL

如果不想用rbac那么,简单权限管理ACL,应该是最方便的了。简要说一下原理吧。。
这个文件是放在/application/models/目录下的Acl.php,调用的时候就直接

$identity = Zend_Auth::getInstance()->getIdentity();
$acl = new Application_Model_Acl();
//然后判断
if( $acl->isAllowed( $identity['Role'] , 'xxxxx' ,'yyyy' )); //xxxx是controller名,yyyy是controller里的action名称。不用额外加action。。。
class Application_Model_Acl extends Zend_Acl
{

	public function __construct()
	{
		/*
		 *  第一次添加权限 "guest" ,支持查看所有的内容
		 */
		$this->addRole(new Zend_Acl_Role('guest'));

		/*
		 * 添加 'user' 组权限,权限大于 guest
		 * 用户组能够发表回复
		 */
		$this->addRole(new Zend_Acl_Role('user'), 'guest');

		/*
		 * 添加一个auth组
		 * auth用户权限基于user,并且可以发表文章
		 */
		$this->addRole(new Zend_Acl_Role('auth'), 'user');

		/*
		 * 添加admin组权限,可以做任何事情
		 */
		$this->addRole(new Zend_Acl_Role('admin'), 'blogger');

		//添加允许使用的Resource(也就是controller名啦),这是posts controller
		$this->add(new Zend_Acl_Resource('posts'));

		//添加允许使用的Resource(也就是controller名啦),这是comments controller
		$this->add(new Zend_Acl_Resource('comments'));

		//最后,添加权限,guest用户组可以执行posts controller下的view方法
		$this->allow('guest', 'posts', 'view');

		//User组可以执行 comments controller下的Add 方法
		$this->allow('user', 'comments', 'add');

		// auth 除了上面的功能外还能够支持 posts controller 中的Edit和add方法
		$this->allow('auth', 'posts', 'edit');
		$this->allow('auth', 'posts', 'add');
	}

}

是不是很方便?ACL就是这样的方便 。。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>