Zend_Acl-Advanced.xml 4.8 KB

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