Zend_Acl-Refining.xml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.acl.refining">
  4. <title>Refining Access Controls</title>
  5. <sect2 id="zend.acl.refining.precise">
  6. <title>Precise Access Controls</title>
  7. <para>
  8. The basic <acronym>ACL</acronym> as defined in the
  9. <link linkend="zend.acl.introduction">previous section</link> shows how various
  10. privileges may be allowed upon the entire <acronym>ACL</acronym> (all resources). In
  11. practice, however, access controls tend to have exceptions and varying degrees of
  12. complexity. <classname>Zend_Acl</classname> allows to you accomplish these refinements
  13. in a straightforward and flexible manner.
  14. </para>
  15. <para>
  16. For the example <acronym>CMS</acronym>, it has been determined that whilst the 'staff'
  17. group covers the needs of the vast majority of users, there is a need for a new
  18. 'marketing' group that requires access to the newsletter and latest news in the
  19. <acronym>CMS</acronym>. The group is fairly self-sufficient and will have the ability
  20. to publish and archive both newsletters and the latest news.
  21. </para>
  22. <para>
  23. In addition, it has also been requested that the 'staff' group be allowed to view news
  24. stories but not to revise the latest news. Finally, it should be impossible for anyone
  25. (administrators included) to archive any 'announcement' news stories since they only
  26. have a lifespan of 1-2 days.
  27. </para>
  28. <para>
  29. First we revise the role registry to reflect these changes. We have determined that the
  30. 'marketing' group has the same basic permissions as 'staff', so we define 'marketing'
  31. in such a way that it inherits permissions from 'staff':
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. // The new marketing group inherits permissions from staff
  35. $acl->addRole(new Zend_Acl_Role('marketing'), 'staff');
  36. ]]></programlisting>
  37. <para>
  38. Next, note that the above access controls refer to specific resources (e.g.,
  39. "newsletter", "latest news", "announcement news"). Now we add these resources:
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. // Create Resources for the rules
  43. // newsletter
  44. $acl->addResource(new Zend_Acl_Resource('newsletter'));
  45. // news
  46. $acl->addResource(new Zend_Acl_Resource('news'));
  47. // latest news
  48. $acl->addResource(new Zend_Acl_Resource('latest'), 'news');
  49. // announcement news
  50. $acl->addResource(new Zend_Acl_Resource('announcement'), 'news');
  51. ]]></programlisting>
  52. <para>
  53. Then it is simply a matter of defining these more specific rules on the target areas of
  54. the <acronym>ACL</acronym>:
  55. </para>
  56. <programlisting language="php"><![CDATA[
  57. // Marketing must be able to publish and archive newsletters and the
  58. // latest news
  59. $acl->allow('marketing',
  60. array('newsletter', 'latest'),
  61. array('publish', 'archive'));
  62. // Staff (and marketing, by inheritance), are denied permission to
  63. // revise the latest news
  64. $acl->deny('staff', 'latest', 'revise');
  65. // Everyone (including administrators) are denied permission to
  66. // archive news announcements
  67. $acl->deny(null, 'announcement', 'archive');
  68. ]]></programlisting>
  69. <para>
  70. We can now query the <acronym>ACL</acronym> with respect to the latest changes:
  71. </para>
  72. <programlisting language="php"><![CDATA[
  73. echo $acl->isAllowed('staff', 'newsletter', 'publish') ?
  74. "allowed" : "denied";
  75. // denied
  76. echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
  77. "allowed" : "denied";
  78. // allowed
  79. echo $acl->isAllowed('staff', 'latest', 'publish') ?
  80. "allowed" : "denied";
  81. // denied
  82. echo $acl->isAllowed('marketing', 'latest', 'publish') ?
  83. "allowed" : "denied";
  84. // allowed
  85. echo $acl->isAllowed('marketing', 'latest', 'archive') ?
  86. "allowed" : "denied";
  87. // allowed
  88. echo $acl->isAllowed('marketing', 'latest', 'revise') ?
  89. "allowed" : "denied";
  90. // denied
  91. echo $acl->isAllowed('editor', 'announcement', 'archive') ?
  92. "allowed" : "denied";
  93. // denied
  94. echo $acl->isAllowed('administrator', 'announcement', 'archive') ?
  95. "allowed" : "denied";
  96. // denied
  97. ]]></programlisting>
  98. </sect2>
  99. <sect2 id="zend.acl.refining.removing">
  100. <title>Removing Access Controls</title>
  101. <para>
  102. To remove one or more access rules from the <acronym>ACL</acronym>, simply use the
  103. available <methodname>removeAllow()</methodname> or
  104. <methodname>removeDeny()</methodname> methods. As with <methodname>allow()</methodname>
  105. and <methodname>deny()</methodname>, you may provide a <constant>NULL</constant> value
  106. to indicate application to all roles, resources, and/or privileges:
  107. </para>
  108. <programlisting language="php"><![CDATA[
  109. // Remove the denial of revising latest news to staff (and marketing,
  110. // by inheritance)
  111. $acl->removeDeny('staff', 'latest', 'revise');
  112. echo $acl->isAllowed('marketing', 'latest', 'revise') ?
  113. "allowed" : "denied";
  114. // allowed
  115. // Remove the allowance of publishing and archiving newsletters to
  116. // marketing
  117. $acl->removeAllow('marketing',
  118. 'newsletter',
  119. array('publish', 'archive'));
  120. echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
  121. "allowed" : "denied";
  122. // denied
  123. echo $acl->isAllowed('marketing', 'newsletter', 'archive') ?
  124. "allowed" : "denied";
  125. // denied
  126. ]]></programlisting>
  127. <para>
  128. Privileges may be modified incrementally as indicated above, but a
  129. <constant>NULL</constant> value for the privileges overrides such incremental changes:
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. // Allow marketing all permissions upon the latest news
  133. $acl->allow('marketing', 'latest');
  134. echo $acl->isAllowed('marketing', 'latest', 'publish') ?
  135. "allowed" : "denied";
  136. // allowed
  137. echo $acl->isAllowed('marketing', 'latest', 'archive') ?
  138. "allowed" : "denied";
  139. // allowed
  140. echo $acl->isAllowed('marketing', 'latest', 'anything') ?
  141. "allowed" : "denied";
  142. // allowed
  143. ]]></programlisting>
  144. </sect2>
  145. </sect1>
  146. <!--
  147. vim:se ts=4 sw=4 et:
  148. -->