Refining Access Controls

Precise Access Controls

The basic ACL as defined in the previous section shows how various privileges may be allowed upon the entire ACL (all resources). In practice, however, access controls tend to have exceptions and varying degrees of complexity. Zend_Acl allows to you accomplish these refinements in a straightforward and flexible manner.

For the example CMS, it has been determined that whilst the 'staff' group covers the needs of the vast majority of users, there is a need for a new 'marketing' group that requires access to the newsletter and latest news in the CMS. The group is fairly self-sufficient and will have the ability to publish and archive both newsletters and the latest news.

In addition, it has also been requested that the 'staff' group be allowed to view news stories but not to revise the latest news. Finally, it should be impossible for anyone (administrators included) to archive any 'announcement' news stories since they only have a lifespan of 1-2 days.

First we revise the role registry to reflect these changes. We have determined that the 'marketing' group has the same basic permissions as 'staff', so we define 'marketing' in such a way that it inherits permissions from 'staff':

  1. // The new marketing group inherits permissions from staff
  2. $acl->addRole(new Zend_Acl_Role('marketing'), 'staff');

Next, note that the above access controls refer to specific resources (e.g., "newsletter", "latest news", "announcement news"). Now we add these resources:

  1. // Create Resources for the rules
  2.  
  3. // newsletter
  4. $acl->addResource(new Zend_Acl_Resource('newsletter'));
  5.  
  6. // news
  7. $acl->addResource(new Zend_Acl_Resource('news'));
  8.  
  9. // latest news
  10. $acl->addResource(new Zend_Acl_Resource('latest'), 'news');
  11.  
  12. // announcement news
  13. $acl->addResource(new Zend_Acl_Resource('announcement'), 'news');

Then it is simply a matter of defining these more specific rules on the target areas of the ACL:

  1. // Marketing must be able to publish and archive newsletters and the
  2. // latest news
  3. $acl->allow('marketing',
  4.             array('newsletter', 'latest'),
  5.             array('publish', 'archive'));
  6.  
  7. // Staff (and marketing, by inheritance), are denied permission to
  8. // revise the latest news
  9. $acl->deny('staff', 'latest', 'revise');
  10.  
  11. // Everyone (including administrators) are denied permission to
  12. // archive news announcements
  13. $acl->deny(null, 'announcement', 'archive');

We can now query the ACL with respect to the latest changes:

  1. echo $acl->isAllowed('staff', 'newsletter', 'publish') ?
  2.      "allowed" : "denied";
  3. // denied
  4.  
  5. echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
  6.      "allowed" : "denied";
  7. // allowed
  8.  
  9. echo $acl->isAllowed('staff', 'latest', 'publish') ?
  10.      "allowed" : "denied";
  11. // denied
  12.  
  13. echo $acl->isAllowed('marketing', 'latest', 'publish') ?
  14.      "allowed" : "denied";
  15. // allowed
  16.  
  17. echo $acl->isAllowed('marketing', 'latest', 'archive') ?
  18.      "allowed" : "denied";
  19. // allowed
  20.  
  21. echo $acl->isAllowed('marketing', 'latest', 'revise') ?
  22.      "allowed" : "denied";
  23. // denied
  24.  
  25. echo $acl->isAllowed('editor', 'announcement', 'archive') ?
  26.      "allowed" : "denied";
  27. // denied
  28.  
  29. echo $acl->isAllowed('administrator', 'announcement', 'archive') ?
  30.      "allowed" : "denied";
  31. // denied

Removing Access Controls

To remove one or more access rules from the ACL, simply use the available removeAllow() or removeDeny() methods. As with allow() and deny(), you may provide a NULL value to indicate application to all roles, resources, and/or privileges:

  1. // Remove the denial of revising latest news to staff (and marketing,
  2. // by inheritance)
  3. $acl->removeDeny('staff', 'latest', 'revise');
  4.  
  5. echo $acl->isAllowed('marketing', 'latest', 'revise') ?
  6.      "allowed" : "denied";
  7. // allowed
  8.  
  9. // Remove the allowance of publishing and archiving newsletters to
  10. // marketing
  11. $acl->removeAllow('marketing',
  12.                   'newsletter',
  13.                   array('publish', 'archive'));
  14.  
  15. echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
  16.      "allowed" : "denied";
  17. // denied
  18.  
  19. echo $acl->isAllowed('marketing', 'newsletter', 'archive') ?
  20.      "allowed" : "denied";
  21. // denied

Privileges may be modified incrementally as indicated above, but a NULL value for the privileges overrides such incremental changes:

  1. // Allow marketing all permissions upon the latest news
  2. $acl->allow('marketing', 'latest');
  3.  
  4. echo $acl->isAllowed('marketing', 'latest', 'publish') ?
  5.      "allowed" : "denied";
  6. // allowed
  7.  
  8. echo $acl->isAllowed('marketing', 'latest', 'archive') ?
  9.      "allowed" : "denied";
  10. // allowed
  11.  
  12. echo $acl->isAllowed('marketing', 'latest', 'anything') ?
  13.      "allowed" : "denied";
  14. // allowed
blog comments powered by Disqus