BaseAuthorize.php 4.8 KB

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