Zend_Acl-Refining.xml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 ACL as defined in the
  9. <link linkend="zend.acl.introduction">previous section</link> shows how various
  10. privileges may be allowed upon the entire ACL (all resources). In practice, however,
  11. access controls tend to have exceptions and varying degrees of complexity.
  12. <classname>Zend_Acl</classname> allows to you accomplish these refinements in a
  13. straightforward and flexible manner.
  14. </para>
  15. <para>
  16. For the example CMS, it has been determined that whilst the 'staff' group covers the
  17. needs of the vast majority of users, there is a need for a new 'marketing' group that
  18. requires access to the newsletter and latest news in the CMS. The group is fairly
  19. self-sufficient and will have the ability to publish and archive both newsletters and
  20. 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 role="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 role="php"><![CDATA[
  42. // Create Resources for the rules
  43. // newsletter
  44. $acl->add(new Zend_Acl_Resource('newsletter'));
  45. // news
  46. $acl->add(new Zend_Acl_Resource('news'));
  47. // latest news
  48. $acl->add(new Zend_Acl_Resource('latest'), 'news');
  49. // announcement news
  50. $acl->add(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 ACL:
  55. </para>
  56. <programlisting role="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 ACL with respect to the latest changes:
  71. </para>
  72. <programlisting role="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 ACL, simply use the available
  103. <code>removeAllow()</code> or <code>removeDeny()</code> methods. As with
  104. <code>allow()</code> and <code>deny()</code>, you may provide a <code>null</code>
  105. value to indicate application to all roles, resources, and/or privileges:
  106. </para>
  107. <programlisting role="php"><![CDATA[
  108. // Remove the denial of revising latest news to staff (and marketing,
  109. // by inheritance)
  110. $acl->removeDeny('staff', 'latest', 'revise');
  111. echo $acl->isAllowed('marketing', 'latest', 'revise') ?
  112. "allowed" : "denied";
  113. // allowed
  114. // Remove the allowance of publishing and archiving newsletters to
  115. // marketing
  116. $acl->removeAllow('marketing',
  117. 'newsletter',
  118. array('publish', 'archive'));
  119. echo $acl->isAllowed('marketing', 'newsletter', 'publish') ?
  120. "allowed" : "denied";
  121. // denied
  122. echo $acl->isAllowed('marketing', 'newsletter', 'archive') ?
  123. "allowed" : "denied";
  124. // denied
  125. ]]></programlisting>
  126. <para>
  127. Privileges may be modified incrementally as indicated above, but a <code>null</code>
  128. value for the privileges overrides such incremental changes:
  129. </para>
  130. <programlisting role="php"><![CDATA[
  131. // Allow marketing all permissions upon the latest news
  132. $acl->allow('marketing', 'latest');
  133. echo $acl->isAllowed('marketing', 'latest', 'publish') ?
  134. "allowed" : "denied";
  135. // allowed
  136. echo $acl->isAllowed('marketing', 'latest', 'archive') ?
  137. "allowed" : "denied";
  138. // allowed
  139. echo $acl->isAllowed('marketing', 'latest', 'anything') ?
  140. "allowed" : "denied";
  141. // allowed
  142. ]]></programlisting>
  143. </sect2>
  144. </sect1>
  145. <!--
  146. vim:se ts=4 sw=4 et:
  147. -->