phpzend-frameworkpermissionsresourceszend-acl

Zend_Acl I need something like $acl->hasParentResource()


I'm checking in the view the allowed content by role and resource with Zend_Acl to show specific content. This is working fine, but I would like to have something like a hasParentResource() method to check the parents of a resource.

An example:

$acl->add(new Zend_Acl_Resource('default'));
$acl->add(new Zend_Acl_Resource('admin'));
$acl->add(new Zend_Acl_Resource('admin::resource1'), 'admin');
$acl->add(new Zend_Acl_Resource('admin::resource2'), 'admin');
$acl->add(new Zend_Acl_Resource('admin::resource3'), 'admin');

/roles
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('user'), 'guest');
$acl->addRole(new Zend_Acl_Role('admin'), 'user');

//deny/allow
$acl->deny();
$acl->allow('guest', 'default');

$acl->allow('user', array(
    'admin::resource1',
    'admin::resource3'
));

$acl->allow('admin');

In the view:

<h1>Admin</h1>
Lorem ipsum dolor...
<?php if($this->acl->hasParentResource('admin')): ?>
    <h2>Resources</h2>
    <?php if($this->acl->isAllowed('admin::resource1')): ?>
        Ressource 1 stuff
    <?php endif; ?>
    <?php if($this->acl->isAllowed('admin::resource2')): ?>
        Ressource 2 stuff
    <?php endif; ?>
    <?php if($this->acl->isAllowed('admin::resource3')): ?>
        Ressource 3 stuff
    <?php endif; ?>
<?php endif; ?>

The idea is to hide the content including the <h2> tag, if the user has no access to resource 1, 2 or 3.
I know, I could write this:

<?php if($this->acl->isAllowed('admin::resource1') || $this->acl->isAllowed('admin::resource2') || $this->acl->isAllowed('admin::resource3')): ?>
    <h2>Resources</h2>
    ...
<?php endif; ?>

But I do not have only these three resources and I don't want to add new resources to such statements if I change something.


Solution

  • Use the inheritsRole function.

    Such that:

    if($this->acl->inheritsRole('user', 'admin')) {
      /* display content for an admin */
    }