CrudAuthorize.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('BaseAuthorize', 'Controller/Component/Auth');
  17. App::uses('Router', 'Routing');
  18. /**
  19. * An authorization adapter for AuthComponent. Provides the ability to authorize using CRUD mappings.
  20. * CRUD mappings allow you to translate controller actions into *C*reate *R*ead *U*pdate *D*elete actions.
  21. * This is then checked in the AclComponent as specific permissions.
  22. *
  23. * For example, taking `/posts/index` as the current request. The default mapping for `index`, is a `read` permission
  24. * check. The Acl check would then be for the `posts` controller with the `read` permission. This allows you
  25. * to create permission systems that focus more on what is being done to resources, rather than the specific actions
  26. * being visited.
  27. *
  28. * @package Cake.Controller.Component.Auth
  29. * @since 2.0
  30. * @see AuthComponent::$authenticate
  31. * @see AclComponent::check()
  32. */
  33. class CrudAuthorize extends BaseAuthorize {
  34. /**
  35. * Sets up additional actionMap values that match the configured `Routing.prefixes`.
  36. *
  37. * @param ComponentCollection $collection The component collection from the controller.
  38. * @param string $settings An array of settings. This class does not use any settings.
  39. */
  40. public function __construct(ComponentCollection $collection, $settings = array()) {
  41. parent::__construct($collection, $settings);
  42. $this->_setPrefixMappings();
  43. }
  44. /**
  45. * sets the crud mappings for prefix routes.
  46. *
  47. * @return void
  48. */
  49. protected function _setPrefixMappings() {
  50. $crud = array('create', 'read', 'update', 'delete');
  51. $map = array_combine($crud, $crud);
  52. $prefixes = Router::prefixes();
  53. if (!empty($prefixes)) {
  54. foreach ($prefixes as $prefix) {
  55. $map = array_merge($map, array(
  56. $prefix . '_index' => 'read',
  57. $prefix . '_add' => 'create',
  58. $prefix . '_edit' => 'update',
  59. $prefix . '_view' => 'read',
  60. $prefix . '_remove' => 'delete',
  61. $prefix . '_create' => 'create',
  62. $prefix . '_read' => 'read',
  63. $prefix . '_update' => 'update',
  64. $prefix . '_delete' => 'delete'
  65. ));
  66. }
  67. }
  68. $this->mapActions($map);
  69. }
  70. /**
  71. * Authorize a user using the mapped actions and the AclComponent.
  72. *
  73. * @param array $user The user to authorize
  74. * @param CakeRequest $request The request needing authorization.
  75. * @return boolean
  76. */
  77. public function authorize($user, CakeRequest $request) {
  78. if (!isset($this->settings['actionMap'][$request->params['action']])) {
  79. trigger_error(__d('cake_dev',
  80. 'CrudAuthorize::authorize() - Attempted access of un-mapped action "%1$s" in controller "%2$s"',
  81. $request->action,
  82. $request->controller
  83. ),
  84. E_USER_WARNING
  85. );
  86. return false;
  87. }
  88. $user = array($this->settings['userModel'] => $user);
  89. $Acl = $this->_Collection->load('Acl');
  90. return $Acl->check(
  91. $user,
  92. $this->action($request, ':controller'),
  93. $this->settings['actionMap'][$request->params['action']]
  94. );
  95. }
  96. }