Zend_Acl-Advanced.xml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.acl.advanced">
  4. <title>Advanced Usage</title>
  5. <sect2 id="zend.acl.advanced.storing">
  6. <title>Storing ACL Data for Persistence</title>
  7. <para>
  8. <classname>Zend_Acl</classname> was designed in such a way that it does not require any
  9. particular backend technology such as a database or cache server for storage of the ACL
  10. data. Its complete PHP implementation enables customized administration tools to be
  11. built upon <classname>Zend_Acl</classname> with relative ease and flexibility. Many
  12. situations require some form of interactive maintenance of the ACL, and
  13. <classname>Zend_Acl</classname> provides methods for setting up, and querying against,
  14. the access controls of an application.
  15. </para>
  16. <para>
  17. Storage of ACL data is therefore left as a task for the developer, since use cases are
  18. expected to vary widely for various situations. Because <classname>Zend_Acl</classname>
  19. is serializable, ACL objects may be serialized with PHP's
  20. <ulink url="http://php.net/serialize"><code>serialize()</code></ulink> function, and
  21. the results may be stored anywhere the developer should desire, such as a file,
  22. database, or caching mechanism.
  23. </para>
  24. </sect2>
  25. <sect2 id="zend.acl.advanced.assertions">
  26. <title>Writing Conditional ACL Rules with Assertions</title>
  27. <para>
  28. Sometimes a rule for allowing or denying a role access to a resource should not be
  29. absolute but dependent upon various criteria. For example, suppose that certain access
  30. should be allowed, but only between the hours of 8:00am and 5:00pm. Another example
  31. would be denying access because a request comes from an IP address that has been
  32. flagged as a source of abuse. <classname>Zend_Acl</classname> has built-in support for
  33. implementing rules based on whatever conditions the developer needs.
  34. </para>
  35. <para>
  36. <classname>Zend_Acl</classname> provides support for conditional rules with
  37. <classname>Zend_Acl_Assert_Interface</classname>. In order to use the rule assertion
  38. interface, a developer writes a class that implements the <code>assert()</code> method
  39. of the interface:
  40. </para>
  41. <programlisting role="php"><![CDATA[
  42. class CleanIPAssertion implements Zend_Acl_Assert_Interface
  43. {
  44. public function assert(Zend_Acl $acl,
  45. Zend_Acl_Role_Interface $role = null,
  46. Zend_Acl_Resource_Interface $resource = null,
  47. $privilege = null)
  48. {
  49. return $this->_isCleanIP($_SERVER['REMOTE_ADDR']);
  50. }
  51. protected function _isCleanIP($ip)
  52. {
  53. // ...
  54. }
  55. }
  56. ]]></programlisting>
  57. <para>
  58. Once an assertion class is available, the developer must supply an instance of the
  59. assertion class when assigning conditional rules. A rule that is created with an
  60. assertion only applies when the assertion method returns true.
  61. </para>
  62. <programlisting role="php"><![CDATA[
  63. $acl = new Zend_Acl();
  64. $acl->allow(null, null, null, new CleanIPAssertion());
  65. ]]></programlisting>
  66. <para>
  67. The above code creates a conditional allow rule that allows access to all privileges
  68. on everything by everyone, except when the requesting IP is "blacklisted." If a request
  69. comes in from an IP that is not considered "clean," then the allow rule does not apply.
  70. Since the rule applies to all roles, all resources, and all privileges, an "unclean" IP
  71. would result in a denial of access. This is a special case, however, and it should be
  72. understood that in all other cases (i.e., where a specific role, resource, or privilege
  73. is specified for the rule), a failed assertion results in the rule not applying, and
  74. other rules would be used to determine whether access is allowed or denied.
  75. </para>
  76. <para>
  77. The <code>assert()</code> method of an assertion object is passed the ACL, role,
  78. resource, and privilege to which the authorization query (i.e.,
  79. <code>isAllowed()</code>) applies, in order to provide a context for the assertion
  80. class to determine its conditions where needed.
  81. </para>
  82. </sect2>
  83. </sect1>
  84. <!--
  85. vim:se ts=4 sw=4 et:
  86. -->