BaseAuthorize.php 4.9 KB

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