Zend Framework – Creating Access Control for your Applications – Zend_ACL

I plan on extending more on this topic as well, but wanted to make a note.

It may be obvious.. but it took me awhile to figure out how I could specify access to a particular action inside of a controller.

Hopefully, I was the only one – but just in case ;) here ya go!

        const ROLE_GUEST = 'guest';
        const ROLE_USER  = 'user';
        const ROLE_ADMIN = 'admin';

        $this->_acl = new Zend_Acl();

        // define our possible user groups
	$this->_acl->addRole(new Zend_Acl_Role(self::ROLE_GUEST));

	// members have at least same access as guest
	$this->_acl->addRole(new Zend_Acl_Role(self::ROLE_USER), array(self::ROLE_GUEST));

        // admins have at least same access as users
        $this->_acl->addRole(new Zend_Acl_Role(self::ROLE_ADMIN), array(self::ROLE_USER));

	// define our restricted controllers as resources.
        $this->_acl->add(new Zend_Acl_Resource('modulename'));
        $this->_acl->add(new Zend_Acl_Resource('modulename/controllername'));
        $this->_acl->allow(self::ROLE_USER,  'modulename');
        $this->_acl->allow(self::ROLE_USER,  'modulename/controllername', 'actionname');
        $this->_acl->allow(self::ROLE_ADMIN, null, array('actionname'));
Friday, August 21st, 2009 Zend Framework

Leave a Reply

You must be logged in to post a comment.

[ad code=1 align=center]