cakephp2-php8/lib/Cake/Controller/Component/Auth/ControllerAuthorize.php

69 lines
2.1 KiB
PHP
Raw Normal View History

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