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

67 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2011-01-02 18:39:48 +00:00
/**
2017-06-10 21:33:55 +00:00
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
2017-06-10 21:33:55 +00:00
* @link https://cakephp.org CakePHP(tm) Project
* @license https://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);
* }
* ```
2011-01-02 18:39:48 +00:00
*
* 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
*/
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
2014-05-31 21:36:05 +00:00
* @param CakeRequest $request Request instance.
* @return bool
*/
public function authorize($user, CakeRequest $request) {
return (bool)$this->_Controller->isAuthorized($user);
}
}