1
|
|
|
<?php namespace Limoncello\Application\Authorization; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyInformation\ContextInterface; |
20
|
|
|
use Limoncello\Contracts\Authentication\AccountInterface; |
21
|
|
|
use Limoncello\Contracts\Authentication\AccountManagerInterface; |
22
|
|
|
use Psr\Container\ContainerExceptionInterface; |
23
|
|
|
use Psr\Container\ContainerInterface; |
24
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Application |
28
|
|
|
*/ |
29
|
|
|
trait AuthorizationRulesTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param ContextInterface $context |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
protected static function reqHasAction(ContextInterface $context): bool |
37
|
|
|
{ |
38
|
|
|
return $context->getRequest()->has(RequestProperties::REQ_ACTION); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ContextInterface $context |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
protected static function reqGetAction(ContextInterface $context): string |
47
|
|
|
{ |
48
|
|
|
assert(static::reqHasAction($context)); |
49
|
|
|
|
50
|
|
|
$value = $context->getRequest()->get(RequestProperties::REQ_ACTION); |
51
|
|
|
|
52
|
|
|
return $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ContextInterface $context |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected static function reqHasResourceType(ContextInterface $context): bool |
61
|
|
|
{ |
62
|
|
|
return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_TYPE); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ContextInterface $context |
67
|
|
|
* |
68
|
|
|
* @return string|null |
69
|
|
|
*/ |
70
|
|
|
protected static function reqGetResourceType(ContextInterface $context): ?string |
71
|
|
|
{ |
72
|
|
|
assert(static::reqHasResourceType($context)); |
73
|
|
|
|
74
|
|
|
$value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_TYPE); |
75
|
|
|
|
76
|
|
|
assert($value === null || is_string($value)); |
77
|
|
|
|
78
|
|
|
return $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ContextInterface $context |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
protected static function reqHasResourceIdentity(ContextInterface $context): bool |
87
|
|
|
{ |
88
|
|
|
return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_IDENTITY); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ContextInterface $context |
93
|
|
|
* |
94
|
|
|
* @return string|int|array|null |
95
|
|
|
*/ |
96
|
|
|
protected static function reqGetResourceIdentity(ContextInterface $context) |
97
|
|
|
{ |
98
|
|
|
assert(static::reqHasResourceIdentity($context)); |
99
|
|
|
|
100
|
|
|
$value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_IDENTITY); |
101
|
|
|
|
102
|
|
|
assert($value === null || is_string($value) || is_array($value) || is_int($value)); |
103
|
|
|
|
104
|
|
|
return $value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param ContextInterface $context |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected static function reqHasResourceAttributes(ContextInterface $context): bool |
113
|
|
|
{ |
114
|
|
|
return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_ATTRIBUTES); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param ContextInterface $context |
119
|
|
|
* |
120
|
|
|
* @return string|int|array|null |
121
|
|
|
*/ |
122
|
|
|
protected static function reqGetResourceAttributes(ContextInterface $context): array |
123
|
|
|
{ |
124
|
|
|
assert(static::reqHasResourceAttributes($context)); |
125
|
|
|
|
126
|
|
|
$value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_ATTRIBUTES); |
127
|
|
|
|
128
|
|
|
assert(is_array($value)); |
129
|
|
|
|
130
|
|
|
return $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param ContextInterface $context |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
protected static function reqHasResourceRelationships(ContextInterface $context): bool |
139
|
|
|
{ |
140
|
|
|
return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_RELATIONSHIPS); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param ContextInterface $context |
145
|
|
|
* |
146
|
|
|
* @return string|int|array|null |
147
|
|
|
*/ |
148
|
|
|
protected static function reqGetResourceRelationships(ContextInterface $context): array |
149
|
|
|
{ |
150
|
|
|
assert(static::reqHasResourceRelationships($context)); |
151
|
|
|
|
152
|
|
|
$value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_RELATIONSHIPS); |
153
|
|
|
|
154
|
|
|
assert(is_array($value)); |
155
|
|
|
|
156
|
|
|
return $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param ContextInterface $context |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
* |
164
|
|
|
* @throws ContainerExceptionInterface |
165
|
|
|
* @throws NotFoundExceptionInterface |
166
|
|
|
*/ |
167
|
|
|
protected static function ctxHasCurrentAccount(ContextInterface $context): bool |
168
|
|
|
{ |
169
|
|
|
/** @var AccountManagerInterface $manager */ |
170
|
|
|
$container = static::ctxGetContainer($context); |
171
|
|
|
$manager = $container->get(AccountManagerInterface::class); |
172
|
|
|
$account = $manager->getAccount(); |
173
|
|
|
|
174
|
|
|
return $account !== null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param ContextInterface $context |
179
|
|
|
* |
180
|
|
|
* @return AccountInterface |
181
|
|
|
* |
182
|
|
|
* @throws ContainerExceptionInterface |
183
|
|
|
* @throws NotFoundExceptionInterface |
184
|
|
|
*/ |
185
|
|
|
protected static function ctxGetCurrentAccount(ContextInterface $context): AccountInterface |
186
|
|
|
{ |
187
|
|
|
assert(static::ctxHasCurrentAccount($context)); |
188
|
|
|
|
189
|
|
|
/** @var AccountManagerInterface $manager */ |
190
|
|
|
$container = static::ctxGetContainer($context); |
191
|
|
|
$manager = $container->get(AccountManagerInterface::class); |
192
|
|
|
$account = $manager->getAccount(); |
193
|
|
|
|
194
|
|
|
return $account; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param ContextInterface $context |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
protected static function ctxHasContainer(ContextInterface $context): bool |
203
|
|
|
{ |
204
|
|
|
return $context->has(ContextProperties::CTX_CONTAINER); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param ContextInterface $context |
209
|
|
|
* |
210
|
|
|
* @return ContainerInterface |
211
|
|
|
*/ |
212
|
|
|
protected static function ctxGetContainer(ContextInterface $context): ContainerInterface |
213
|
|
|
{ |
214
|
|
|
assert(static::ctxHasContainer($context)); |
215
|
|
|
|
216
|
|
|
return $context->get(ContextProperties::CTX_CONTAINER); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|