BaseAuthorize.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  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 = Set::merge($this->settings, $settings);
  68. }
  69. /**
  70. * Checks user authorization.
  71. *
  72. * @param array $user Active user data
  73. * @param CakeRequest $request
  74. * @return boolean
  75. */
  76. abstract public function authorize($user, CakeRequest $request);
  77. /**
  78. * Accessor to the controller object.
  79. *
  80. * @param mixed $controller null to get, a controller to set.
  81. * @return mixed
  82. * @throws CakeException
  83. */
  84. public function 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
  100. * @return string the action path for the given request.
  101. */
  102. public function action($request, $path = '/:plugin/:controller/:action') {
  103. $plugin = empty($request['plugin']) ? null : Inflector::camelize($request['plugin']) . '/';
  104. return str_replace(
  105. array(':controller', ':action', ':plugin/'),
  106. array(Inflector::camelize($request['controller']), $request['action'], $plugin),
  107. $this->settings['actionPath'] . $path
  108. );
  109. }
  110. /**
  111. * Maps crud actions to actual controller names. Used to modify or get the current mapped actions.
  112. *
  113. * @param mixed $map Either an array of mappings, or undefined to get current values.
  114. * @return mixed Either the current mappings or null when setting.
  115. */
  116. public function mapActions($map = array()) {
  117. if (empty($map)) {
  118. return $this->settings['actionMap'];
  119. }
  120. $crud = array('create', 'read', 'update', 'delete');
  121. foreach ($map as $action => $type) {
  122. if (in_array($action, $crud) && is_array($type)) {
  123. foreach ($type as $typedAction) {
  124. $this->settings['actionMap'][$typedAction] = $action;
  125. }
  126. } else {
  127. $this->settings['actionMap'][$action] = $type;
  128. }
  129. }
  130. }
  131. }