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

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 67 lines | 15 code | 5 blank | 47 comment | 2 complexity | 500383cb4d30d6bf7589f299e1a8a21d 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. /**
  17. * An authorization adapter for AuthComponent. Provides the ability to authorize using a controller callback.
  18. * Your controller's isAuthorized() method should return a boolean to indicate whether or not the user is authorized.
  19. *
  20. * {{{
  21. * public function isAuthorized($user) {
  22. * if (!empty($this->request->params['admin'])) {
  23. * return $user['role'] == 'admin';
  24. * }
  25. * return !empty($user);
  26. * }
  27. * }}}
  28. *
  29. * the above is simple implementation that would only authorize users of the 'admin' role to access
  30. * admin routing.
  31. *
  32. * @package Cake.Controller.Component.Auth
  33. * @since 2.0
  34. * @see AuthComponent::$authenticate
  35. */
  36. class ControllerAuthorize extends BaseAuthorize {
  37. /**
  38. * Get/set the controller this authorize object will be working with. Also checks that isAuthorized is implemented.
  39. *
  40. * @param mixed $controller null to get, a controller to set.
  41. * @return mixed
  42. * @throws CakeException
  43. */
  44. public function controller($controller = null) {
  45. if ($controller) {
  46. if (!method_exists($controller, 'isAuthorized')) {
  47. throw new CakeException(__d('cake_dev', '$controller does not implement an isAuthorized() method.'));
  48. }
  49. }
  50. return parent::controller($controller);
  51. }
  52. /**
  53. * Checks user authorization using a controller callback.
  54. *
  55. * @param array $user Active user data
  56. * @param CakeRequest $request
  57. * @return boolean
  58. */
  59. public function authorize($user, CakeRequest $request) {
  60. return (bool) $this->_Controller->isAuthorized($user);
  61. }
  62. }