-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathIdentityDecorator.php
More file actions
191 lines (172 loc) · 5.14 KB
/
IdentityDecorator.php
File metadata and controls
191 lines (172 loc) · 5.14 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?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 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Authorization;
use ArrayAccess;
use Authorization\Policy\ResultInterface;
use BadMethodCallException;
/**
* An decorator implementing the IdentityInterface.
*
* This decorator is intended to wrap the application defined identity
* object and proxy attributes/methods to and 'mixin' the can() method.
*
* The decorated identity must implement ArrayAccess or be an array.
*/
class IdentityDecorator implements IdentityInterface
{
/**
* Identity data
*
* @var \ArrayAccess<string, mixed>|array<string, mixed>
*/
protected ArrayAccess|array $identity;
/**
* Authorization Service
*/
protected AuthorizationServiceInterface $authorization;
/**
* Constructor
*
* @param \Authorization\AuthorizationServiceInterface $service The authorization service.
* @param \ArrayAccess<string, mixed>|array<string, mixed> $identity Identity data
*/
public function __construct(AuthorizationServiceInterface $service, ArrayAccess|array $identity)
{
$this->authorization = $service;
$this->identity = $identity;
}
/**
* @inheritDoc
*/
public function can(string $action, mixed $resource): bool
{
return $this->authorization->can($this, $action, $resource);
}
/**
* @inheritDoc
*/
public function canResult(string $action, mixed $resource): ResultInterface
{
return $this->authorization->canResult($this, $action, $resource);
}
/**
* @inheritDoc
*/
public function applyScope(string $action, mixed $resource, mixed ...$optionalArgs): mixed
{
return $this->authorization->applyScope($this, $action, $resource, ...$optionalArgs);
}
/**
* @inheritDoc
*/
public function getOriginalData(): ArrayAccess|array
{
if (
is_object($this->identity)
&& method_exists($this->identity, 'getOriginalData')
) {
return $this->identity->getOriginalData();
}
return $this->identity;
}
/**
* Delegate unknown methods to decorated identity.
*
* @param string $method The method being invoked.
* @param array<mixed> $args The arguments for the method.
* @return mixed
*/
public function __call(string $method, array $args): mixed
{
if (!is_object($this->identity)) {
throw new BadMethodCallException(sprintf('Cannot call `%s`. Identity data is not an object.', $method));
}
if (!method_exists($this->identity, $method)) {
throw new BadMethodCallException(sprintf(
'Method `%s` does not exist on `%s`.',
$method,
$this->identity::class,
));
}
/** @phpstan-ignore callable.nonCallable */
return [$this->identity, $method](...$args);
}
/**
* Delegate property access to decorated identity.
*
* @param string $property The property to read.
* @return mixed
*/
public function __get(string $property): mixed
{
return $this->identity->{$property};
}
/**
* Delegate property isset to decorated identity.
*
* @param string $property The property to read.
* @return bool
*/
public function __isset(string $property): bool
{
return isset($this->identity->{$property});
}
/**
* Whether a offset exists
*
* @link https://secure.php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset Offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->identity[$offset]);
}
/**
* Offset to retrieve
*
* @link https://secure.php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset Offset
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->identity[$offset] ?? null;
}
/**
* Offset to set
*
* @link https://secure.php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset The offset to assign the value to.
* @param mixed $value Value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->identity[$offset] = $value;
}
/**
* Offset to unset
*
* @link https://secure.php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset Offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
unset($this->identity[$offset]);
}
}