ControllerAuthorize.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Auth;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Exception\Exception;
  19. use Cake\Network\Request;
  20. /**
  21. * An authorization adapter for AuthComponent. Provides the ability to authorize
  22. * using a controller callback. Your controller's isAuthorized() method should
  23. * return a boolean to indicate whether or not the user is authorized.
  24. *
  25. * ```
  26. * public function isAuthorized($user)
  27. * {
  28. * if ($this->request->param('admin')) {
  29. * return $user['role'] === 'admin';
  30. * }
  31. * return !empty($user);
  32. * }
  33. * ```
  34. *
  35. * The above is simple implementation that would only authorize users of the
  36. * 'admin' role to access admin routing.
  37. *
  38. * @see AuthComponent::$authenticate
  39. */
  40. class ControllerAuthorize extends BaseAuthorize
  41. {
  42. /**
  43. * Controller for the request.
  44. *
  45. * @var \Cake\Controller\Controller
  46. */
  47. protected $_Controller = null;
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function __construct(ComponentRegistry $registry, array $config = [])
  52. {
  53. parent::__construct($registry, $config);
  54. $this->controller($registry->getController());
  55. }
  56. /**
  57. * Get/set the controller this authorize object will be working with. Also
  58. * checks that isAuthorized is implemented.
  59. *
  60. * @param Controller|null $controller null to get, a controller to set.
  61. * @return \Cake\Controller\Controller
  62. * @throws \Cake\Core\Exception\Exception If controller does not have method `isAuthorized()`.
  63. */
  64. public function controller(Controller $controller = null)
  65. {
  66. if ($controller) {
  67. if (!method_exists($controller, 'isAuthorized')) {
  68. throw new Exception(sprintf(
  69. '%s does not implement an isAuthorized() method.',
  70. get_class($controller)
  71. ));
  72. }
  73. $this->_Controller = $controller;
  74. }
  75. return $this->_Controller;
  76. }
  77. /**
  78. * Checks user authorization using a controller callback.
  79. *
  80. * @param array $user Active user data
  81. * @param \Cake\Network\Request $request Request instance.
  82. * @return bool
  83. */
  84. public function authorize($user, Request $request)
  85. {
  86. return (bool)$this->_Controller->isAuthorized($user);
  87. }
  88. }