-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
Authentication.php
231 lines (192 loc) · 6.78 KB
/
Authentication.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace DoctrineModule\Options;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use Laminas\Authentication\Adapter\Exception;
use Laminas\Authentication\Storage\StorageInterface;
use Laminas\Stdlib\AbstractOptions;
use function gettype;
use function is_callable;
use function is_string;
use function sprintf;
/**
* This options class can be consumed by five different classes:
*
* DoctrineModule\Authentication\Adapter\ObjectRepository
* DoctrineModule\Service\Authentication\AdapterFactory
* DoctrineModule\Authentication\Storage\ObjectRepository
* DoctrineModule\Service\Authentication\ServiceFactory
* DoctrineModule\Service\Authentication\AuthenticationServiceFactory
*
* When using with DoctrineModule\Authentication\Adapter\ObjectRepository the following
* options are required:
*
* $identityProperty
* $credentialProperty
*
* In addition either $objectRepository or $objectManager and $identityClass must be set.
* If $objectRepository is set, it takes precedence over $objectManager and $identityClass.
* If $objectManager is used, it must be an instance of ObjectManager.
*
* All remains the same using with DoctrineModule\Service\AuthenticationAdapterFactory,
* however, a string may be passed to $objectManager. This string must be a valid key to
* retrieve an ObjectManager instance from the ServiceManager.
*
* When using with DoctrineModule\Authentication\Service\Object repository the following
* options are required:
*
* Either $objectManager, or $classMetadata and $objectRepository.
*
* All remains the same using with DoctrineModule\Service\AuthenticationStorageFactory,
* however, a string may be passed to $objectManager. This string must be a valid key to
* retrieve an ObjectManager instance from the ServiceManager.
*
* @template-extends AbstractOptions<mixed>
*/
final class Authentication extends AbstractOptions
{
/**
* A valid object implementing ObjectManager interface
*/
protected string|ObjectManager $objectManager;
/**
* A valid object implementing ObjectRepository interface (or ObjectManager/identityClass)
*/
protected ObjectRepository|null $objectRepository = null;
/**
* Entity's class name
*
* @psalm-var class-string
*/
protected string $identityClass;
/**
* Property to use for the identity
*/
protected string $identityProperty;
/**
* Property to use for the credential
*/
protected string $credentialProperty;
/**
* Callable function to check if a credential is valid
*/
protected mixed $credentialCallable;
/**
* If an objectManager is not supplied, this metadata will be used
* by DoctrineModule/Authentication/Storage/ObjectRepository
*/
protected ClassMetadata|null $classMetadata = null;
/**
* When using this options class to create a DoctrineModule/Authentication/Storage/ObjectRepository
* this is the storage instance that the object key will be stored in.
*
* When using this options class to create an AuthenticationService with and
* the option storeOnlyKeys == false, this is the storage instance that the whole
* object will be stored in.
*/
protected StorageInterface|string $storage = 'DoctrineModule\Authentication\Storage\Session';
public function setObjectManager(string|ObjectManager $objectManager): Authentication
{
$this->objectManager = $objectManager;
return $this;
}
/**
* Causes issue with unit test StorageFactoryTest::testCanInstantiateStorageFromServiceLocator
* when return type is specified
* : ObjectManager
*/
public function getObjectManager(): mixed
{
return $this->objectManager;
}
public function setObjectRepository(ObjectRepository $objectRepository): Authentication
{
$this->objectRepository = $objectRepository;
return $this;
}
public function getObjectRepository(): ObjectRepository
{
if ($this->objectRepository) {
return $this->objectRepository;
}
return $this->objectManager->getRepository($this->identityClass);
}
public function setIdentityClass(string $identityClass): Authentication
{
$this->identityClass = $identityClass;
return $this;
}
public function getIdentityClass(): string
{
return $this->identityClass;
}
/** @throws Exception\InvalidArgumentException */
public function setIdentityProperty(string $identityProperty): Authentication
{
if ($identityProperty === '') {
throw new Exception\InvalidArgumentException(
sprintf('Provided $identityProperty is invalid, %s given', gettype($identityProperty)),
);
}
$this->identityProperty = $identityProperty;
return $this;
}
public function getIdentityProperty(): string
{
return $this->identityProperty;
}
/** @throws Exception\InvalidArgumentException */
public function setCredentialProperty(string $credentialProperty): Authentication
{
if ($credentialProperty === '') {
throw new Exception\InvalidArgumentException(
sprintf('Provided $credentialProperty is invalid, %s given', gettype($credentialProperty)),
);
}
$this->credentialProperty = $credentialProperty;
return $this;
}
public function getCredentialProperty(): string
{
return $this->credentialProperty;
}
/** @throws Exception\InvalidArgumentException */
public function setCredentialCallable(mixed $credentialCallable): Authentication
{
if (! is_callable($credentialCallable)) {
throw new Exception\InvalidArgumentException(
sprintf(
'"%s" is not a callable',
is_string($credentialCallable) ? $credentialCallable : gettype($credentialCallable),
),
);
}
$this->credentialCallable = $credentialCallable;
return $this;
}
public function getCredentialCallable(): mixed
{
return $this->credentialCallable;
}
public function getClassMetadata(): ClassMetadata
{
if ($this->classMetadata) {
return $this->classMetadata;
}
return $this->objectManager->getClassMetadata($this->identityClass);
}
public function setClassMetadata(ClassMetadata $classMetadata): void
{
$this->classMetadata = $classMetadata;
}
public function getStorage(): StorageInterface|string
{
return $this->storage;
}
public function setStorage(StorageInterface|string $storage): void
{
$this->storage = $storage;
}
}