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