Zend_Acl.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.acl.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Acl</classname> provides a lightweight and flexible access control list
  7. (ACL) implementation for privileges management. In general, an application may utilize such
  8. ACL's to control access to certain protected objects by other requesting objects.
  9. </para>
  10. <para>
  11. For the purposes of this documentation:
  12. </para>
  13. <itemizedlist>
  14. <listitem>
  15. <para>
  16. a <emphasis>resource</emphasis> is an object
  17. to which access is controlled.
  18. </para>
  19. </listitem>
  20. <listitem>
  21. <para>
  22. a <emphasis>role</emphasis> is an object
  23. that may request access to a Resource.
  24. </para>
  25. </listitem>
  26. </itemizedlist>
  27. <para>
  28. Put simply, <emphasis>roles request access to resources</emphasis>. For
  29. example, if a parking attendant requests access to a car, then the parking attendant is the
  30. requesting role, and the car is the resource, since access to the car may not be granted to
  31. everyone.
  32. </para>
  33. <para>
  34. Through the specification and use of an ACL, an application may control how roles are
  35. granted access to resources.
  36. </para>
  37. <sect2 id="zend.acl.introduction.resources">
  38. <title>Resources</title>
  39. <para>
  40. Creating a resource in <classname>Zend_Acl</classname> is very simple.
  41. <classname>Zend_Acl</classname> provides the resource,
  42. <classname>Zend_Acl_Resource_Interface</classname>, to facilitate creating resources in
  43. an application. A class need only implement this interface, which consists of a single
  44. method, <methodname>getResourceId()</methodname>, so <classname>Zend_Acl</classname> to
  45. recognize the object as a resource. Additionally,
  46. <classname>Zend_Acl_Resource</classname> is provided by <classname>Zend_Acl</classname>
  47. as a basic resource implementation for developers to extend as needed.
  48. </para>
  49. <para>
  50. <classname>Zend_Acl</classname> provides a tree structure to which multiple resources
  51. can be added. Since resources are stored in such a tree structure, they can be
  52. organized from the general (toward the tree root) to the specific (toward the tree
  53. leaves). Queries on a specific resource will automatically search the resource's
  54. hierarchy for rules assigned to ancestor resources, allowing for simple inheritance of
  55. rules. For example, if a default rule is to be applied to each building in a city, one
  56. would simply assign the rule to the city, instead of assigning the same rule to each
  57. building. Some buildings may require exceptions to such a rule, however, and this can
  58. be achieved in <classname>Zend_Acl</classname> by assigning such exception rules to
  59. each building that requires such an exception. A resource may inherit from only one
  60. parent resource, though this parent resource can have its own parent resource, etc.
  61. </para>
  62. <para>
  63. <classname>Zend_Acl</classname> also supports privileges on resources (e.g., "create",
  64. "read", "update", "delete"), so the developer can assign rules that affect all
  65. privileges or specific privileges on one or more resources.
  66. </para>
  67. </sect2>
  68. <sect2 id="zend.acl.introduction.roles">
  69. <title>Roles</title>
  70. <para>
  71. As with resources, creating a role is also very simple. All roles must implement
  72. <classname>Zend_Acl_Role_Interface</classname>. This interface consists of a single
  73. method, <methodname>getRoleId()</methodname>, Additionally,
  74. <classname>Zend_Acl_Role</classname> is provided by <classname>Zend_Acl</classname> as
  75. a basic role implementation for developers to extend as needed.
  76. </para>
  77. <para>
  78. In <classname>Zend_Acl</classname>, a role may inherit from one or more roles. This is
  79. to support inheritance of rules among roles. For example, a user role, such as "sally",
  80. may belong to one or more parent roles, such as "editor" and "administrator". The
  81. developer can assign rules to "editor" and "administrator" separately, and "sally"
  82. would inherit such rules from both, without having to assign rules directly to "sally".
  83. </para>
  84. <para>
  85. Though the ability to inherit from multiple roles is very useful, multiple inheritance
  86. also introduces some degree of complexity. The following example illustrates the
  87. ambiguity condition and how <classname>Zend_Acl</classname> solves it.
  88. </para>
  89. <example id="zend.acl.introduction.roles.example.multiple_inheritance">
  90. <title>Multiple Inheritance among Roles</title>
  91. <para>
  92. The following code defines three base roles - "guest",
  93. "member", and "admin" - from which other roles may
  94. inherit. Then, a role identified by "someUser" is established and
  95. inherits from the three other roles. The order in which these roles appear in the
  96. <varname>$parents</varname> array is important. When necessary,
  97. <classname>Zend_Acl</classname> searches for access rules defined not only for the
  98. queried role (herein, "someUser"), but also upon the roles from which
  99. the queried role inherits (herein, "guest", "member", and
  100. "admin"):
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. $acl = new Zend_Acl();
  104. $acl->addRole(new Zend_Acl_Role('guest'))
  105. ->addRole(new Zend_Acl_Role('member'))
  106. ->addRole(new Zend_Acl_Role('admin'));
  107. $parents = array('guest', 'member', 'admin');
  108. $acl->addRole(new Zend_Acl_Role('someUser'), $parents);
  109. $acl->add(new Zend_Acl_Resource('someResource'));
  110. $acl->deny('guest', 'someResource');
  111. $acl->allow('member', 'someResource');
  112. echo $acl->isAllowed('someUser', 'someResource') ? 'allowed' : 'denied';
  113. ]]></programlisting>
  114. <para>
  115. Since there is no rule specifically defined for the "someUser" role and
  116. "someResource", <classname>Zend_Acl</classname> must search for rules that may be
  117. defined for roles that "someUser" inherits. First, the "admin" role is visited, and
  118. there is no access rule defined for it. Next, the "member" role is visited, and
  119. <classname>Zend_Acl</classname> finds that there is a rule specifying that "member"
  120. is allowed access to "someResource".
  121. </para>
  122. <para>
  123. If <classname>Zend_Acl</classname> were to continue examining the rules defined for
  124. other parent roles, however, it would find that "guest" is denied access to
  125. "someResource". This fact introduces an ambiguity because now
  126. "someUser" is both denied and allowed access to "someResource", by reason of having
  127. inherited conflicting rules from different parent roles.
  128. </para>
  129. <para>
  130. <classname>Zend_Acl</classname> resolves this ambiguity by completing a query when
  131. it finds the first rule that is directly applicable to the query. In this case,
  132. since the "member" role is examined before the "guest" role, the example code would
  133. print "allowed".
  134. </para>
  135. </example>
  136. <note>
  137. <para>
  138. When specifying multiple parents for a role, keep in mind that the last parent
  139. listed is the first one searched for rules applicable to an authorization query.
  140. </para>
  141. </note>
  142. </sect2>
  143. <sect2 id="zend.acl.introduction.creating">
  144. <title>Creating the Access Control List</title>
  145. <para>
  146. An Access Control List (ACL) can represent any set of physical or virtual objects that
  147. you wish. For the purposes of demonstration, however, we will create a basic Content
  148. Management System (CMS) ACL that maintains several tiers of groups over a wide variety
  149. of areas. To create a new ACL object, we instantiate the ACL with no parameters:
  150. </para>
  151. <programlisting language="php"><![CDATA[
  152. $acl = new Zend_Acl();
  153. ]]></programlisting>
  154. <note>
  155. <para>
  156. Until a developer specifies an "allow" rule, <classname>Zend_Acl</classname> denies
  157. access to every privilege upon every resource by every role.
  158. </para>
  159. </note>
  160. </sect2>
  161. <sect2 id="zend.acl.introduction.role_registry">
  162. <title>Registering Roles</title>
  163. <para>
  164. CMS's will nearly always require a hierarchy of permissions to determine the authoring
  165. capabilities of its users. There may be a 'Guest' group to allow limited access for
  166. demonstrations, a 'Staff' group for the majority of CMS users who perform most of the
  167. day-to-day operations, an 'Editor' group for those responsible for publishing,
  168. reviewing, archiving and deleting content, and finally an 'Administrator' group whose
  169. tasks may include all of those of the other groups as well as maintenance of sensitive
  170. information, user management, back-end configuration data, backup and export. This set
  171. of permissions can be represented in a role registry, allowing each group to inherit
  172. privileges from 'parent' groups, as well as providing distinct privileges for their
  173. unique group only. The permissions may be expressed as follows:
  174. </para>
  175. <table id="zend.acl.introduction.role_registry.table.example_cms_access_controls">
  176. <title>Access Controls for an Example CMS</title>
  177. <tgroup cols="3">
  178. <thead>
  179. <row>
  180. <entry>Name</entry>
  181. <entry>Unique Permissions</entry>
  182. <entry>Inherit Permissions From</entry>
  183. </row>
  184. </thead>
  185. <tbody>
  186. <row>
  187. <entry>Guest</entry>
  188. <entry>View</entry>
  189. <entry>N/A</entry>
  190. </row>
  191. <row>
  192. <entry>Staff</entry>
  193. <entry>Edit, Submit, Revise</entry>
  194. <entry>Guest</entry>
  195. </row>
  196. <row>
  197. <entry>Editor</entry>
  198. <entry>Publish, Archive, Delete</entry>
  199. <entry>Staff</entry>
  200. </row>
  201. <row>
  202. <entry>Administrator</entry>
  203. <entry>(Granted all access)</entry>
  204. <entry>N/A</entry>
  205. </row>
  206. </tbody>
  207. </tgroup>
  208. </table>
  209. <para>
  210. For this example, <classname>Zend_Acl_Role</classname> is used, but any object that
  211. implements <classname>Zend_Acl_Role_Interface</classname> is acceptable. These groups
  212. can be added to the role registry as follows:
  213. </para>
  214. <programlisting language="php"><![CDATA[
  215. $acl = new Zend_Acl();
  216. // Add groups to the Role registry using Zend_Acl_Role
  217. // Guest does not inherit access controls
  218. $roleGuest = new Zend_Acl_Role('guest');
  219. $acl->addRole($roleGuest);
  220. // Staff inherits from guest
  221. $acl->addRole(new Zend_Acl_Role('staff'), $roleGuest);
  222. /*
  223. Alternatively, the above could be written:
  224. $acl->addRole(new Zend_Acl_Role('staff'), 'guest');
  225. */
  226. // Editor inherits from staff
  227. $acl->addRole(new Zend_Acl_Role('editor'), 'staff');
  228. // Administrator does not inherit access controls
  229. $acl->addRole(new Zend_Acl_Role('administrator'));
  230. ]]></programlisting>
  231. </sect2>
  232. <sect2 id="zend.acl.introduction.defining">
  233. <title>Defining Access Controls</title>
  234. <para>
  235. Now that the ACL contains the relevant roles, rules can be established that define how
  236. resources may be accessed by roles. You may have noticed that we have not defined any
  237. particular resources for this example, which is simplified to illustrate that the rules
  238. apply to all resources. <classname>Zend_Acl</classname> provides an implementation
  239. whereby rules need only be assigned from general to specific, minimizing the number of
  240. rules needed, because resources and roles inherit rules that are defined upon their
  241. ancestors.
  242. </para>
  243. <note>
  244. <para>
  245. In general, <classname>Zend_Acl</classname> obeys a given rule if and only if a
  246. more specific rule does not apply.
  247. </para>
  248. </note>
  249. <para>
  250. Consequently, we can define a reasonably complex set of rules with a minimum amount of
  251. code. To apply the base permissions as defined above:
  252. </para>
  253. <programlisting language="php"><![CDATA[
  254. $acl = new Zend_Acl();
  255. $roleGuest = new Zend_Acl_Role('guest');
  256. $acl->addRole($roleGuest);
  257. $acl->addRole(new Zend_Acl_Role('staff'), $roleGuest);
  258. $acl->addRole(new Zend_Acl_Role('editor'), 'staff');
  259. $acl->addRole(new Zend_Acl_Role('administrator'));
  260. // Guest may only view content
  261. $acl->allow($roleGuest, null, 'view');
  262. /*
  263. Alternatively, the above could be written:
  264. $acl->allow('guest', null, 'view');
  265. //*/
  266. // Staff inherits view privilege from guest, but also needs additional
  267. // privileges
  268. $acl->allow('staff', null, array('edit', 'submit', 'revise'));
  269. // Editor inherits view, edit, submit, and revise privileges from
  270. // staff, but also needs additional privileges
  271. $acl->allow('editor', null, array('publish', 'archive', 'delete'));
  272. // Administrator inherits nothing, but is allowed all privileges
  273. $acl->allow('administrator');
  274. ]]></programlisting>
  275. <para>
  276. The <constant>NULL</constant> values in the above <methodname>allow()</methodname> calls
  277. are used to indicate that the allow rules apply to all resources.
  278. </para>
  279. </sect2>
  280. <sect2 id="zend.acl.introduction.querying">
  281. <title>Querying an ACL</title>
  282. <para>
  283. We now have a flexible ACL that can be used to determine whether requesters have
  284. permission to perform functions throughout the web application. Performing queries is
  285. quite simple using the <methodname>isAllowed()</methodname> method:
  286. </para>
  287. <programlisting language="php"><![CDATA[
  288. echo $acl->isAllowed('guest', null, 'view') ?
  289. "allowed" : "denied";
  290. // allowed
  291. echo $acl->isAllowed('staff', null, 'publish') ?
  292. "allowed" : "denied";
  293. // denied
  294. echo $acl->isAllowed('staff', null, 'revise') ?
  295. "allowed" : "denied";
  296. // allowed
  297. echo $acl->isAllowed('editor', null, 'view') ?
  298. "allowed" : "denied";
  299. // allowed because of inheritance from guest
  300. echo $acl->isAllowed('editor', null, 'update') ?
  301. "allowed" : "denied";
  302. // denied because no allow rule for 'update'
  303. echo $acl->isAllowed('administrator', null, 'view') ?
  304. "allowed" : "denied";
  305. // allowed because administrator is allowed all privileges
  306. echo $acl->isAllowed('administrator') ?
  307. "allowed" : "denied";
  308. // allowed because administrator is allowed all privileges
  309. echo $acl->isAllowed('administrator', null, 'update') ?
  310. "allowed" : "denied";
  311. // allowed because administrator is allowed all privileges
  312. ]]></programlisting>
  313. </sect2>
  314. </sect1>
  315. <!--
  316. vim:se ts=4 sw=4 et:
  317. -->