2011-01-02 13:35:43 -05:00
|
|
|
<?php
|
2011-01-02 13:39:48 -05:00
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 20:59:49 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-02 13:39:48 -05:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-01-02 13:39:48 -05:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 20:59:49 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-01-02 13:39:48 -05:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-31 00:11:14 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-01-02 13:39:48 -05:00
|
|
|
*/
|
2011-03-05 17:40:42 -04:30
|
|
|
|
|
|
|
App::uses('BaseAuthorize', 'Controller/Component/Auth');
|
2011-01-02 13:35:43 -05:00
|
|
|
|
2011-01-02 13:39:48 -05:00
|
|
|
/**
|
2012-12-22 23:48:15 +01:00
|
|
|
* An authorization adapter for AuthComponent. Provides the ability to authorize using a controller callback.
|
2011-01-02 13:39:48 -05:00
|
|
|
* Your controller's isAuthorized() method should return a boolean to indicate whether or not the user is authorized.
|
|
|
|
*
|
2015-01-09 13:47:25 +01:00
|
|
|
* ```
|
2011-05-30 22:02:32 +02:00
|
|
|
* public function isAuthorized($user) {
|
2011-01-02 13:39:48 -05:00
|
|
|
* if (!empty($this->request->params['admin'])) {
|
2013-02-12 03:38:08 +01:00
|
|
|
* return $user['role'] === 'admin';
|
2011-01-02 13:39:48 -05:00
|
|
|
* }
|
|
|
|
* return !empty($user);
|
|
|
|
* }
|
2015-01-09 13:47:25 +01:00
|
|
|
* ```
|
2011-01-02 13:39:48 -05:00
|
|
|
*
|
|
|
|
* the above is simple implementation that would only authorize users of the 'admin' role to access
|
|
|
|
* admin routing.
|
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Controller.Component.Auth
|
2011-01-02 13:39:48 -05:00
|
|
|
* @since 2.0
|
|
|
|
* @see AuthComponent::$authenticate
|
|
|
|
*/
|
2011-01-02 14:23:43 -05:00
|
|
|
class ControllerAuthorize extends BaseAuthorize {
|
2011-01-02 13:35:43 -05:00
|
|
|
|
|
|
|
/**
|
2012-12-22 23:48:15 +01:00
|
|
|
* Get/set the controller this authorize object will be working with. Also checks that isAuthorized is implemented.
|
2011-01-02 13:35:43 -05:00
|
|
|
*
|
2012-05-13 01:43:31 +01:00
|
|
|
* @param Controller $controller null to get, a controller to set.
|
2011-07-31 16:55:52 -04:00
|
|
|
* @return mixed
|
|
|
|
* @throws CakeException
|
2011-01-02 13:35:43 -05:00
|
|
|
*/
|
2012-02-23 15:06:25 +01:00
|
|
|
public function controller(Controller $controller = null) {
|
2011-01-02 14:23:43 -05:00
|
|
|
if ($controller) {
|
|
|
|
if (!method_exists($controller, 'isAuthorized')) {
|
2013-08-21 00:05:53 +02:00
|
|
|
throw new CakeException(__d('cake_dev', '$controller does not implement an %s method.', 'isAuthorized()'));
|
2011-01-02 14:23:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent::controller($controller);
|
2011-01-02 13:35:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks user authorization using a controller callback.
|
|
|
|
*
|
|
|
|
* @param array $user Active user data
|
2014-06-01 03:06:05 +05:30
|
|
|
* @param CakeRequest $request Request instance.
|
2014-07-03 15:36:42 +02:00
|
|
|
* @return bool
|
2011-01-02 13:35:43 -05:00
|
|
|
*/
|
|
|
|
public function authorize($user, CakeRequest $request) {
|
2012-03-03 19:27:46 -05:00
|
|
|
return (bool)$this->_Controller->isAuthorized($user);
|
2011-01-02 13:35:43 -05:00
|
|
|
}
|
|
|
|
|
2012-03-03 19:27:46 -05:00
|
|
|
}
|