AclComponent.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @package Cake.Controller.Component
  12. * @since CakePHP(tm) v 0.10.0.1076
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('Component', 'Controller');
  16. App::uses('AclInterface', 'Controller/Component/Acl');
  17. /**
  18. * Access Control List factory class.
  19. *
  20. * Uses a strategy pattern to allow custom ACL implementations to be used with the same component interface.
  21. * You can define by changing `Configure::write('Acl.classname', 'DbAcl');` in your core.php. Concrete ACL
  22. * implementations should extend `AclBase` and implement the methods it defines.
  23. *
  24. * @package Cake.Controller.Component
  25. * @link http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html
  26. */
  27. class AclComponent extends Component {
  28. /**
  29. * Instance of an ACL class
  30. *
  31. * @var AclInterface
  32. */
  33. protected $_Instance = null;
  34. /**
  35. * Aro object.
  36. *
  37. * @var string
  38. */
  39. public $Aro;
  40. /**
  41. * Aco object
  42. *
  43. * @var string
  44. */
  45. public $Aco;
  46. /**
  47. * Constructor. Will return an instance of the correct ACL class as defined in `Configure::read('Acl.classname')`
  48. *
  49. * @param ComponentCollection $collection
  50. * @param array $settings
  51. * @throws CakeException when Acl.classname could not be loaded.
  52. */
  53. public function __construct(ComponentCollection $collection, $settings = array()) {
  54. parent::__construct($collection, $settings);
  55. $name = Configure::read('Acl.classname');
  56. if (!class_exists($name)) {
  57. list($plugin, $name) = pluginSplit($name, true);
  58. App::uses($name, $plugin . 'Controller/Component/Acl');
  59. if (!class_exists($name)) {
  60. throw new CakeException(__d('cake_dev', 'Could not find %s.', $name));
  61. }
  62. }
  63. $this->adapter($name);
  64. }
  65. /**
  66. * Sets or gets the Adapter object currently in the AclComponent.
  67. *
  68. * `$this->Acl->adapter();` will get the current adapter class while
  69. * `$this->Acl->adapter($obj);` will set the adapter class
  70. *
  71. * Will call the initialize method on the adapter if setting a new one.
  72. *
  73. * @param AclInterface|string $adapter Instance of AclInterface or a string name of the class to use. (optional)
  74. * @return AclInterface|void either null, or the adapter implementation.
  75. * @throws CakeException when the given class is not an instance of AclInterface
  76. */
  77. public function adapter($adapter = null) {
  78. if ($adapter) {
  79. if (is_string($adapter)) {
  80. $adapter = new $adapter();
  81. }
  82. if (!$adapter instanceof AclInterface) {
  83. throw new CakeException(__d('cake_dev', 'AclComponent adapters must implement AclInterface'));
  84. }
  85. $this->_Instance = $adapter;
  86. $this->_Instance->initialize($this);
  87. return;
  88. }
  89. return $this->_Instance;
  90. }
  91. /**
  92. * Pass-thru function for ACL check instance. Check methods
  93. * are used to check whether or not an ARO can access an ACO
  94. *
  95. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  96. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  97. * @param string $action Action (defaults to *)
  98. * @return boolean Success
  99. */
  100. public function check($aro, $aco, $action = "*") {
  101. return $this->_Instance->check($aro, $aco, $action);
  102. }
  103. /**
  104. * Pass-thru function for ACL allow instance. Allow methods
  105. * are used to grant an ARO access to an ACO.
  106. *
  107. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  108. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  109. * @param string $action Action (defaults to *)
  110. * @return boolean Success
  111. */
  112. public function allow($aro, $aco, $action = "*") {
  113. return $this->_Instance->allow($aro, $aco, $action);
  114. }
  115. /**
  116. * Pass-thru function for ACL deny instance. Deny methods
  117. * are used to remove permission from an ARO to access an ACO.
  118. *
  119. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  120. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  121. * @param string $action Action (defaults to *)
  122. * @return boolean Success
  123. */
  124. public function deny($aro, $aco, $action = "*") {
  125. return $this->_Instance->deny($aro, $aco, $action);
  126. }
  127. /**
  128. * Pass-thru function for ACL inherit instance. Inherit methods
  129. * modify the permission for an ARO to be that of its parent object.
  130. *
  131. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  132. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  133. * @param string $action Action (defaults to *)
  134. * @return boolean Success
  135. */
  136. public function inherit($aro, $aco, $action = "*") {
  137. return $this->_Instance->inherit($aro, $aco, $action);
  138. }
  139. /**
  140. * Pass-thru function for ACL grant instance. An alias for AclComponent::allow()
  141. *
  142. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  143. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  144. * @param string $action Action (defaults to *)
  145. * @return boolean Success
  146. * @deprecated
  147. */
  148. public function grant($aro, $aco, $action = "*") {
  149. trigger_error(__d('cake_dev', 'AclComponent::grant() is deprecated, use allow() instead'), E_USER_WARNING);
  150. return $this->_Instance->allow($aro, $aco, $action);
  151. }
  152. /**
  153. * Pass-thru function for ACL grant instance. An alias for AclComponent::deny()
  154. *
  155. * @param array|string|Model $aro ARO The requesting object identifier. See `AclNode::node()` for possible formats
  156. * @param array|string|Model $aco ACO The controlled object identifier. See `AclNode::node()` for possible formats
  157. * @param string $action Action (defaults to *)
  158. * @return boolean Success
  159. * @deprecated
  160. */
  161. public function revoke($aro, $aco, $action = "*") {
  162. trigger_error(__d('cake_dev', 'AclComponent::revoke() is deprecated, use deny() instead'), E_USER_WARNING);
  163. return $this->_Instance->deny($aro, $aco, $action);
  164. }
  165. }