1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
5
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
12
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
13
|
|
|
* @since 1.0.0 |
14
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
15
|
|
|
*/ |
16
|
|
|
namespace Phauthentic\Authorization; |
17
|
|
|
|
18
|
|
|
use Phauthentic\Authorization\Policy\BeforePolicyInterface; |
19
|
|
|
use Phauthentic\Authorization\Policy\Exception\MissingMethodException; |
20
|
|
|
use Phauthentic\Authorization\Policy\ResolverInterface; |
21
|
|
|
use Phauthentic\Authorization\Policy\ResultInterface; |
|
|
|
|
22
|
|
|
use RuntimeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Authorization Service |
26
|
|
|
*/ |
27
|
|
|
class AuthorizationService implements AuthorizationServiceInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Authorization policy resolver. |
31
|
|
|
* |
32
|
|
|
* @var \Phauthentic\Authorization\Policy\ResolverInterface |
33
|
|
|
*/ |
34
|
|
|
protected $resolver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Track whether or not authorization was checked. |
38
|
|
|
* |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
protected $authorizationChecked = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor |
45
|
|
|
* |
46
|
|
|
* @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Authorization policy resolver. |
47
|
|
|
*/ |
48
|
12 |
|
public function __construct(ResolverInterface $resolver) |
49
|
|
|
{ |
50
|
12 |
|
$this->resolver = $resolver; |
51
|
12 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
8 |
|
public function can(?IdentityInterface $user, string $action, $resource) |
57
|
|
|
{ |
58
|
8 |
|
$this->authorizationChecked = true; |
59
|
8 |
|
$policy = $this->resolver->getPolicy($resource); |
60
|
|
|
|
61
|
8 |
|
if ($policy instanceof BeforePolicyInterface) { |
62
|
3 |
|
$result = $policy->before($user, $resource, $action); |
63
|
|
|
|
64
|
3 |
|
if ($result instanceof ResultInterface || is_bool($result)) { |
65
|
2 |
|
return $result; |
|
|
|
|
66
|
|
|
} |
67
|
1 |
|
if ($result !== null) { |
|
|
|
|
68
|
|
|
throw new RuntimeException('Pre-authorization check must return `bool` or `null`.'); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
$handler = $this->getCanHandler($policy, $action); |
73
|
5 |
|
$result = $handler($user, $resource); |
74
|
|
|
|
75
|
5 |
|
if ($result instanceof ResultInterface) { |
76
|
|
|
return $result; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return $result === true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
*/ |
85
|
3 |
|
public function applyScope(?IdentityInterface $user, string $action, $resource) |
86
|
|
|
{ |
87
|
3 |
|
$this->authorizationChecked = true; |
88
|
3 |
|
$policy = $this->resolver->getPolicy($resource); |
89
|
3 |
|
$handler = $this->getScopeHandler($policy, $action); |
90
|
|
|
|
91
|
2 |
|
return $handler($user, $resource); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a policy action handler. |
96
|
|
|
* |
97
|
|
|
* @param mixed $policy Policy object. |
98
|
|
|
* @param string $action Action name. |
99
|
|
|
* @return callable |
100
|
|
|
* @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException |
101
|
|
|
*/ |
102
|
6 |
|
protected function getCanHandler($policy, $action) |
103
|
|
|
{ |
104
|
6 |
|
$method = 'can' . ucfirst($action); |
105
|
|
|
|
106
|
6 |
|
if (!method_exists($policy, $method) && !method_exists($policy, '__call')) { |
107
|
1 |
|
throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]); |
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
return [$policy, $method]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns a policy scope action handler. |
115
|
|
|
* |
116
|
|
|
* @param mixed $policy Policy object. |
117
|
|
|
* @param string $action Action name. |
118
|
|
|
* @return callable |
119
|
|
|
* @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException |
120
|
|
|
*/ |
121
|
3 |
|
protected function getScopeHandler($policy, $action): callable |
122
|
|
|
{ |
123
|
3 |
|
$method = 'scope' . ucfirst($action); |
124
|
|
|
|
125
|
3 |
|
if (!method_exists($policy, $method)) { |
126
|
1 |
|
throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]); |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return [$policy, $method]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
3 |
|
public function authorizationChecked(): bool |
136
|
|
|
{ |
137
|
3 |
|
return $this->authorizationChecked; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
1 |
|
public function skipAuthorization(): AuthorizationServiceInterface |
144
|
|
|
{ |
145
|
1 |
|
$this->authorizationChecked = true; |
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths