PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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