PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Controller/Component/Auth/BaseAuthorize.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 157 lines | 57 code | 8 blank | 92 comment | 6 complexity | 93da9e0c690a4c0d22b4856fec991b3e MD5 | raw file
  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 action names. Used to modify or get the current mapped actions.
  112. *
  113. * Create additional mappings for a standard CRUD operation:
  114. *
  115. * {{{
  116. * $this->Auth->mapActions(array('create' => array('add', 'register'));
  117. * }}}
  118. *
  119. * Create mappings for custom CRUD operations:
  120. *
  121. * {{{
  122. * $this->Auth->mapActions(array('my_action' => 'admin'));
  123. * }}}
  124. *
  125. * You can use the custom CRUD operations to create additional generic permissions
  126. * that behave like CRUD operations. Doing this will require additional columns on the
  127. * permissions lookup. When using with DbAcl, you'll have to add additional _admin type columns
  128. * to the `aros_acos` table.
  129. *
  130. * @param mixed $map Either an array of mappings, or undefined to get current values.
  131. * @return mixed Either the current mappings or null when setting.
  132. * @see AuthComponent::mapActions()
  133. */
  134. public function mapActions($map = array()) {
  135. if (empty($map)) {
  136. return $this->settings['actionMap'];
  137. }
  138. $crud = array('create', 'read', 'update', 'delete');
  139. foreach ($map as $action => $type) {
  140. if (in_array($action, $crud) && is_array($type)) {
  141. foreach ($type as $typedAction) {
  142. $this->settings['actionMap'][$typedAction] = $action;
  143. }
  144. } else {
  145. $this->settings['actionMap'][$action] = $type;
  146. }
  147. }
  148. }
  149. }