multiuser-authorization.xml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.multiuser.authorization">
  4. <title>Building an Authorization System in Zend Framework</title>
  5. <sect2 id="learning.multiuser.authorization.intro">
  6. <title>Introduction to Authorization</title>
  7. <para>
  8. After a user has been identified as being authentic, an application can go about its
  9. business of providing some useful and desirable resources to a consumer. In many cases,
  10. applications might contain different resource types, with some resources having stricter
  11. rules regarding access. This process of determining who has access to which resources is
  12. the process of "authorization". Authorization in its simplest form is the composition of
  13. these elements:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>
  18. the identity whom wishes to be granted access
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. the resource the identity is asking permission to consume
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. and optionally, what the identity is privileged to do with the resource
  29. </para>
  30. </listitem>
  31. </itemizedlist>
  32. <para>
  33. In Zend Framework, the <classname>Zend_Acl</classname> component handles the task of
  34. building a tree of roles, resources and privileges to manage and query authorization
  35. requests against.
  36. </para>
  37. </sect2>
  38. <sect2 id="learning.multiuser.authorization.basic-usage">
  39. <title>Basic Usage of Zend_Acl</title>
  40. <!-- explain the interaction with a User object, how -->
  41. <para>
  42. When using <classname>Zend_Acl</classname>, any models can serve as roles or resources
  43. by simply implementing the proper interface. To be used in a role capacity, the class
  44. must implement the <classname>Zend_Acl_Role_Interface</classname>, which requires only
  45. <methodname>getRoleId()</methodname>. To be used in a resource capacity, a class must
  46. implement the <classname>Zend_Acl_Resource_Interface</classname> which similarly
  47. requires the class implement the <methodname>getResourceId()</methodname> method.
  48. </para>
  49. <para>
  50. Demonstrated below is a simple user model. This model can take part in our
  51. <acronym>ACL</acronym> system simply by implementing the
  52. <classname>Zend_Acl_Role_Interface</classname>. The method
  53. <methodname>getRoleId()</methodname> will return the id "guest" when an ID is not known,
  54. or it will return the role ID that was assigned to this actual user object. This value
  55. can effectively come from anywhere, a static definition or perhaps dynamically from the
  56. users database role itself.
  57. </para>
  58. <programlisting language="php"><![CDATA[
  59. class Default_Model_User implements Zend_Acl_Role_Interface
  60. {
  61. protected $_aclRoleId = null;
  62. public function getRoleId()
  63. {
  64. if ($this->_aclRoleId == null) {
  65. return 'guest';
  66. }
  67. return $this->_aclRoleId;
  68. }
  69. }
  70. ]]></programlisting>
  71. <para>
  72. While the concept of a user as a role is pretty straight forward, your application
  73. might choose to have any other models in your system as a potential "resource" to be
  74. consumed in this <acronym>ACL</acronym> system. For simplicity, we'll use the example
  75. of a blog post. Since the type of the resource is tied to the type of the object,
  76. this class will only return 'blogPost' as the resource ID in this system. Naturally,
  77. this value can be dynamic if your system requires it to be so.
  78. </para>
  79. <programlisting language="php"><![CDATA[
  80. class Default_Model_BlogPost implements Zend_Acl_Resource_Interface
  81. {
  82. public function getResourceId()
  83. {
  84. return 'blogPost';
  85. }
  86. }
  87. ]]></programlisting>
  88. <para>
  89. Now that we have at least a role and a resource, we can go about defining the rules
  90. of the <acronym>ACL</acronym> system. These rules will be consulted when the system
  91. receives a query about what is possible given a certain role, resources, and optionally
  92. a privilege.
  93. </para>
  94. <para>
  95. Lets assume the following rules:
  96. </para>
  97. <programlisting language="php"><![CDATA[
  98. $acl = new Zend_Acl();
  99. // setup the various roles in our system
  100. $acl->addRole('guest');
  101. // owner inherits all of the rules of guest
  102. $acl->addRole('owner', 'guest');
  103. // add the resources
  104. $acl->addResource('blogPost');
  105. // add privileges to roles and resource combinations
  106. $acl->allow('guest', 'blogPost', 'view');
  107. $acl->allow('owner', 'blogPost', 'post');
  108. $acl->allow('owner', 'blogPost', 'publish');
  109. ]]></programlisting>
  110. <para>
  111. The above rules are quite simple: a guest role and an owner role exist; as does a
  112. blogPost type resource. Guests are allowed to view blog posts, and owners are
  113. allowed to post and publish blog posts. To query this system one might do any of
  114. the following:
  115. </para>
  116. <programlisting language="php"><![CDATA[
  117. // assume the user model is of type guest resource
  118. $guestUser = new Default_Model_User();
  119. $ownerUser = new Default_Model_Owner('OwnersUsername');
  120. $post = new Default_Model_BlogPost();
  121. $acl->isAllowed($guestUser, $post, 'view'); // true
  122. $acl->isAllowed($ownerUser, $post, 'view'); // true
  123. $acl->isAllowed($guestUser, $post, 'post'); // false
  124. $acl->isAllowed($ownerUser, $post, 'post'); // true
  125. ]]></programlisting>
  126. <para>
  127. As you can see, the above rules exercise whether owners and guests can view posts,
  128. which they can, or post new posts, which owners can and guests cannot. But as you
  129. might expect this type of system might not be as dynamic as we wish it to be.
  130. What if we want to ensure a specific owner actual owns a very specific blog post
  131. before allowing him to publish it? In other words, we want to ensure that only post
  132. owners have the ability to publish their own posts.
  133. </para>
  134. <para>
  135. This is where assertions come in. Assertions are methods that will be called out to
  136. when the static rule checking is simply not enough. When registering an assertion
  137. object this object will be consulted to determine, typically dynamically, if some
  138. roles has access to some resource, with some optional privlidge that can only be
  139. answered by the logic within the assertion. For this example, we'll use the following
  140. assertion:
  141. </para>
  142. <programlisting language="php"><![CDATA[
  143. class OwnerCanPublishBlogPostAssertion implements Zend_Acl_Assert_Interface
  144. {
  145. /**
  146. * This assertion should receive the actual User and BlogPost objects.
  147. *
  148. * @param Zend_Acl $acl
  149. * @param Zend_Acl_Role_Interface $user
  150. * @param Zend_Acl_Resource_Interface $blogPost
  151. * @param $privilege
  152. * @return bool
  153. */
  154. public function assert(Zend_Acl $acl,
  155. Zend_Acl_Role_Interface $user = null,
  156. Zend_Acl_Resource_Interface $blogPost = null,
  157. $privilege = null)
  158. {
  159. if (!$user instanceof Default_Model_User) {
  160. throw new Exception(__CLASS__
  161. . '::'
  162. . __METHOD__
  163. . ' expects the role to be'
  164. . ' an instance of User');
  165. }
  166. if (!$blogPost instanceof Default_Model_BlogPost) {
  167. throw new Exception(__CLASS__
  168. . '::'
  169. . __METHOD__
  170. . ' expects the resource to be'
  171. . ' an instance of BlogPost');
  172. }
  173. // if role is publisher, he can always modify a post
  174. if ($user->getRoleId() == 'publisher') {
  175. return true;
  176. }
  177. // check to ensure that everyone else is only modifying their own post
  178. if ($user->id != null && $blogPost->ownerUserId == $user->id) {
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. }
  185. ]]></programlisting>
  186. <para>
  187. To hook this into our <acronym>ACL</acronym> system, we would do the following:
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. // replace this:
  191. // $acl->allow('owner', 'blogPost', 'publish');
  192. // with this:
  193. $acl->allow('owner',
  194. 'blogPost',
  195. 'publish',
  196. new OwnerCanPublishBlogPostAssertion());
  197. // lets also add the role of a "publisher" who has access to everything
  198. $acl->allow('publisher', 'blogPost', 'publish');
  199. ]]></programlisting>
  200. <para>
  201. Now, anytime the <acronym>ACL</acronym> is consulted about whether or not an owner
  202. can publish a specific blog post, this assertion will be run. This assertion will
  203. ensure that unless the role type is 'publisher' the owner role must be logically
  204. tied to the blog post in question. In this example, we check to see that the
  205. <property>ownerUserId</property> property of the blog post matches the id of the
  206. owner passed in.
  207. </para>
  208. </sect2>
  209. </sect1>