BaseAuthorize.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('Hash', 'Utility');
  15. /**
  16. * Abstract base authorization adapter for AuthComponent.
  17. *
  18. * @package Cake.Controller.Component.Auth
  19. * @since 2.0
  20. * @see AuthComponent::$authenticate
  21. */
  22. abstract class BaseAuthorize {
  23. /**
  24. * Controller for the request.
  25. *
  26. * @var Controller
  27. */
  28. protected $_Controller = null;
  29. /**
  30. * Component collection instance for getting more components.
  31. *
  32. * @var ComponentCollection
  33. */
  34. protected $_Collection;
  35. /**
  36. * Settings for authorize objects.
  37. *
  38. * - `actionPath` - The path to ACO nodes that contains the nodes for controllers. Used as a prefix
  39. * when calling $this->action();
  40. * - `actionMap` - Action -> crud mappings. Used by authorization objects that want to map actions to CRUD roles.
  41. * - `userModel` - Model name that ARO records can be found under. Defaults to 'User'.
  42. *
  43. * @var array
  44. */
  45. public $settings = array(
  46. 'actionPath' => null,
  47. 'actionMap' => array(
  48. 'index' => 'read',
  49. 'add' => 'create',
  50. 'edit' => 'update',
  51. 'view' => 'read',
  52. 'delete' => 'delete',
  53. 'remove' => 'delete'
  54. ),
  55. 'userModel' => 'User'
  56. );
  57. /**
  58. * Constructor
  59. *
  60. * @param ComponentCollection $collection The controller for this request.
  61. * @param string $settings An array of settings. This class does not use any settings.
  62. */
  63. public function __construct(ComponentCollection $collection, $settings = array()) {
  64. $this->_Collection = $collection;
  65. $controller = $collection->getController();
  66. $this->controller($controller);
  67. $this->settings = Hash::merge($this->settings, $settings);
  68. }
  69. /**
  70. * Checks user authorization.
  71. *
  72. * @param array $user Active user data
  73. * @param CakeRequest $request Request instance.
  74. * @return bool
  75. */
  76. abstract public function authorize($user, CakeRequest $request);
  77. /**
  78. * Accessor to the controller object.
  79. *
  80. * @param Controller $controller null to get, a controller to set.
  81. * @return mixed
  82. * @throws CakeException
  83. */
  84. public function controller(Controller $controller = null) {
  85. if ($controller) {
  86. if (!$controller instanceof Controller) {
  87. throw new CakeException(__d('cake_dev', '$controller needs to be an instance of Controller'));
  88. }
  89. $this->_Controller = $controller;
  90. return true;
  91. }
  92. return $this->_Controller;
  93. }
  94. /**
  95. * Get the action path for a given request. Primarily used by authorize objects
  96. * that need to get information about the plugin, controller, and action being invoked.
  97. *
  98. * @param CakeRequest $request The request a path is needed for.
  99. * @param string $path Path format.
  100. * @return string the action path for the given request.
  101. */
  102. public function action(CakeRequest $request, $path = '/:plugin/:controller/:action') {
  103. $plugin = empty($request['plugin']) ? null : Inflector::camelize($request['plugin']) . '/';
  104. $path = str_replace(
  105. array(':controller', ':action', ':plugin/'),
  106. array(Inflector::camelize($request['controller']), $request['action'], $plugin),
  107. $this->settings['actionPath'] . $path
  108. );
  109. $path = str_replace('//', '/', $path);
  110. return trim($path, '/');
  111. }
  112. /**
  113. * Maps crud actions to actual action names. Used to modify or get the current mapped actions.
  114. *
  115. * Create additional mappings for a standard CRUD operation:
  116. *
  117. * {{{
  118. * $this->Auth->mapActions(array('create' => array('add', 'register'));
  119. * }}}
  120. *
  121. * Or equivalently:
  122. *
  123. * {{{
  124. * $this->Auth->mapActions(array('register' => 'create', 'add' => 'create'));
  125. * }}}
  126. *
  127. * Create mappings for custom CRUD operations:
  128. *
  129. * {{{
  130. * $this->Auth->mapActions(array('range' => 'search'));
  131. * }}}
  132. *
  133. * You can use the custom CRUD operations to create additional generic permissions
  134. * that behave like CRUD operations. Doing this will require additional columns on the
  135. * permissions lookup. For example if one wanted an additional search CRUD operation
  136. * one would create and additional column '_search' in the aros_acos table. One could
  137. * create a custom admin CRUD operation for administration functions similarly if needed.
  138. *
  139. * @param array $map Either an array of mappings, or undefined to get current values.
  140. * @return mixed Either the current mappings or null when setting.
  141. * @see AuthComponent::mapActions()
  142. */
  143. public function mapActions($map = array()) {
  144. if (empty($map)) {
  145. return $this->settings['actionMap'];
  146. }
  147. foreach ($map as $action => $type) {
  148. if (is_array($type)) {
  149. foreach ($type as $typedAction) {
  150. $this->settings['actionMap'][$typedAction] = $action;
  151. }
  152. } else {
  153. $this->settings['actionMap'][$action] = $type;
  154. }
  155. }
  156. }
  157. }