AclComponent.php 5.3 KB

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