Zend_Acl-Advanced.xml 4.4 KB

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