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
|
15 |
|
public function __construct(ResolverInterface $resolver) |
49
|
|
|
{ |
50
|
15 |
|
$this->resolver = $resolver; |
51
|
15 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
11 |
|
public function can(?IdentityInterface $user, string $action, $resource): ResultInterface |
57
|
|
|
{ |
58
|
11 |
|
$this->authorizationChecked = true; |
59
|
11 |
|
$policy = $this->resolver->getPolicy($resource); |
60
|
|
|
|
61
|
11 |
|
if ($policy instanceof BeforePolicyInterface) { |
62
|
5 |
|
$result = $policy->before($user, $resource, $action); |
63
|
|
|
|
64
|
5 |
|
if ($result !== null) { |
65
|
4 |
|
return $result; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
$handler = $this->getCanHandler($policy, $action); |
70
|
|
|
|
71
|
6 |
|
return $handler($user, $resource); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
3 |
|
public function applyScope(?IdentityInterface $user, string $action, $resource) |
78
|
|
|
{ |
79
|
3 |
|
$this->authorizationChecked = true; |
80
|
3 |
|
$policy = $this->resolver->getPolicy($resource); |
81
|
3 |
|
$handler = $this->getScopeHandler($policy, $action); |
82
|
|
|
|
83
|
2 |
|
return $handler($user, $resource); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns a policy action handler. |
88
|
|
|
* |
89
|
|
|
* @param mixed $policy Policy object. |
90
|
|
|
* @param string $action Action name. |
91
|
|
|
* @return callable |
92
|
|
|
* @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException |
93
|
|
|
*/ |
94
|
7 |
|
protected function getCanHandler($policy, $action): callable |
95
|
|
|
{ |
96
|
7 |
|
$method = 'can' . ucfirst($action); |
97
|
|
|
|
98
|
7 |
|
if (!method_exists($policy, $method) && !method_exists($policy, '__call')) { |
99
|
1 |
|
throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]); |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
return [$policy, $method]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a policy scope action handler. |
107
|
|
|
* |
108
|
|
|
* @param mixed $policy Policy object. |
109
|
|
|
* @param string $action Action name. |
110
|
|
|
* @return callable |
111
|
|
|
* @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException |
112
|
|
|
*/ |
113
|
3 |
|
protected function getScopeHandler($policy, $action): callable |
114
|
|
|
{ |
115
|
3 |
|
$method = 'scope' . ucfirst($action); |
116
|
|
|
|
117
|
3 |
|
if (!method_exists($policy, $method)) { |
118
|
1 |
|
throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
return [$policy, $method]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
3 |
|
public function authorizationChecked(): bool |
128
|
|
|
{ |
129
|
3 |
|
return $this->authorizationChecked; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
1 |
|
public function skipAuthorization(): AuthorizationServiceInterface |
136
|
|
|
{ |
137
|
1 |
|
$this->authorizationChecked = true; |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|