1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.acl.refining">
4 <title>Refining Access Controls</title>
6 <sect2 id="zend.acl.refining.precise">
7 <title>Precise Access Controls</title>
10 The basic <acronym>ACL</acronym> as defined in the
11 <link linkend="zend.acl.introduction">previous section</link> shows how various
12 privileges may be allowed upon the entire <acronym>ACL</acronym> (all resources). In
13 practice, however, access controls tend to have exceptions and varying degrees of
14 complexity. <classname>Zend_Acl</classname> allows to you accomplish these refinements
15 in a straightforward and flexible manner.
19 For the example <acronym>CMS</acronym>, it has been determined that whilst the 'staff'
20 group covers the needs of the vast majority of users, there is a need for a new
21 'marketing' group that requires access to the newsletter and latest news in the
22 <acronym>CMS</acronym>. The group is fairly self-sufficient and will have the ability
23 to publish and archive both newsletters and the latest news.
27 In addition, it has also been requested that the 'staff' group be allowed to view news
28 stories but not to revise the latest news. Finally, it should be impossible for anyone
29 (administrators included) to archive any 'announcement' news stories since they only
30 have a lifespan of 1-2 days.
34 First we revise the role registry to reflect these changes. We have determined that the
35 'marketing' group has the same basic permissions as 'staff', so we define 'marketing'
36 in such a way that it inherits permissions from 'staff':
39 <programlisting language="php"><![CDATA[
40 // The new marketing group inherits permissions from staff
41 $acl->addRole(new Zend_Acl_Role('marketing'), 'staff');
45 Next, note that the above access controls refer to specific resources (e.g.,
46 "newsletter", "latest news", "announcement news"). Now we add these resources:
49 <programlisting language="php"><![CDATA[
50 // Create Resources for the rules
53 $acl->addResource(new Zend_Acl_Resource('newsletter'));
56 $acl->addResource(new Zend_Acl_Resource('news'));
59 $acl->addResource(new Zend_Acl_Resource('latest'), 'news');
62 $acl->addResource(new Zend_Acl_Resource('announcement'), 'news');
66 Then it is simply a matter of defining these more specific rules on the target areas of
67 the <acronym>ACL</acronym>:
70 <programlisting language="php"><![CDATA[
71 // Marketing must be able to publish and archive newsletters and the
73 $acl->allow('marketing',
74 array('newsletter', 'latest'),
75 array('publish', 'archive'));
77 // Staff (and marketing, by inheritance), are denied permission to
78 // revise the latest news
79 $acl->deny('staff', 'latest', 'revise');
81 // Everyone (including administrators) are denied permission to
82 // archive news announcements
83 $acl->deny(null, 'announcement', 'archive');
87 We can now query the <acronym>ACL</acronym> with respect to the latest changes:
90 <programlisting language="php"><![CDATA[
91 echo $acl->isAllowed('staff', 'newsletter', 'publish') ?
95 echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
99 echo $acl->isAllowed('staff', 'latest', 'publish') ?
100 "allowed" : "denied";
103 echo $acl->isAllowed('marketing', 'latest', 'publish') ?
104 "allowed" : "denied";
107 echo $acl->isAllowed('marketing', 'latest', 'archive') ?
108 "allowed" : "denied";
111 echo $acl->isAllowed('marketing', 'latest', 'revise') ?
112 "allowed" : "denied";
115 echo $acl->isAllowed('editor', 'announcement', 'archive') ?
116 "allowed" : "denied";
119 echo $acl->isAllowed('administrator', 'announcement', 'archive') ?
120 "allowed" : "denied";
125 <sect2 id="zend.acl.refining.removing">
126 <title>Removing Access Controls</title>
129 To remove one or more access rules from the <acronym>ACL</acronym>, simply use the
130 available <methodname>removeAllow()</methodname> or
131 <methodname>removeDeny()</methodname> methods. As with <methodname>allow()</methodname>
132 and <methodname>deny()</methodname>, you may provide a <constant>NULL</constant> value
133 to indicate application to all roles, resources, and/or privileges:
136 <programlisting language="php"><![CDATA[
137 // Remove the denial of revising latest news to staff (and marketing,
139 $acl->removeDeny('staff', 'latest', 'revise');
141 echo $acl->isAllowed('marketing', 'latest', 'revise') ?
142 "allowed" : "denied";
145 // Remove the allowance of publishing and archiving newsletters to
147 $acl->removeAllow('marketing',
149 array('publish', 'archive'));
151 echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
152 "allowed" : "denied";
155 echo $acl->isAllowed('marketing', 'newsletter', 'archive') ?
156 "allowed" : "denied";
161 Privileges may be modified incrementally as indicated above, but a
162 <constant>NULL</constant> value for the privileges overrides such incremental changes:
165 <programlisting language="php"><![CDATA[
166 // Allow marketing all permissions upon the latest news
167 $acl->allow('marketing', 'latest');
169 echo $acl->isAllowed('marketing', 'latest', 'publish') ?
170 "allowed" : "denied";
173 echo $acl->isAllowed('marketing', 'latest', 'archive') ?
174 "allowed" : "denied";
177 echo $acl->isAllowed('marketing', 'latest', 'anything') ?
178 "allowed" : "denied";