Introduction

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. '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. 'newsletter'));
  5.  
  6. // news
  7. 'news'));
  8.  
  9. // latest news
  10. 'latest'), 'news');
  11.  
  12. // announcement news
  13. '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''newsletter', 'latest''publish', 'archive'));
  4.  
  5. // Staff (and marketing, by inheritance), are denied permission to
  6. // revise the latest news
  7. $acl->deny('staff', 'latest', 'revise');
  8.  
  9. // Everyone (including administrators) are denied permission to
  10. // archive news announcements
  11. 'announcement', 'archive');

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

  1. span style="color: #ff0000;">'staff', 'newsletter', 'publish') ?
  2.      "allowed" : "denied";
  3. // denied
  4. 'marketing', 'newsletter', 'publish') ?
  5.      "allowed" : "denied";
  6. // allowed
  7. 'staff', 'latest', 'publish') ?
  8.      "allowed" : "denied";
  9. // denied
  10. 'marketing', 'latest', 'publish') ?
  11.      "allowed" : "denied";
  12. // allowed
  13. 'marketing', 'latest', 'archive') ?
  14.      "allowed" : "denied";
  15. // allowed
  16. 'marketing', 'latest', 'revise') ?
  17.      "allowed" : "denied";
  18. // denied
  19. 'editor', 'announcement', 'archive') ?
  20.      "allowed" : "denied";
  21. // denied
  22. 'administrator', 'announcement', 'archive') ?
  23.      "allowed" : "denied";
  24. // 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''marketing', 'latest', 'revise') ?
  4.      "allowed" : "denied";
  5. // allowed
  6.  
  7. // Remove the allowance of publishing and archiving newsletters to
  8. // marketing
  9. $acl->removeAllow('marketing',
  10.                   'newsletter''publish', 'archive''marketing', 'newsletter', 'publish') ?
  11.      "allowed" : "denied";
  12. // denied
  13. 'marketing', 'newsletter', 'archive') ?
  14.      "allowed" : "denied";
  15. // 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''marketing', 'latest', 'publish') ?
  3.      "allowed" : "denied";
  4. // allowed
  5. 'marketing', 'latest', 'archive') ?
  6.      "allowed" : "denied";
  7. // allowed
  8. 'marketing', 'latest', 'anything') ?
  9.      "allowed" : "denied";
  10. // allowed

Introduction