/
DoctrineObject.php
393 lines (350 loc) · 12.2 KB
/
DoctrineObject.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace DoctrineModule\Authentication\Adapter;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Adapter\Exception;
use Zend\Authentication\Result as AuthenticationResult;
/**
* Abstract authentication adapter that uses a Doctrine object for verification.
*
* @deprecated please use DoctrineModule\Authentication\Adapter\DoctrineObjectRepository
* @license MIT
* @link http://www.doctrine-project.org/
* @since 0.2.0
* @author Tim Roediger <superdweebie@gmail.com>
*/
class DoctrineObject implements AdapterInterface
{
/**
* Doctrine ObjectManager instance
*
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $om;
/**
* Doctrine object class that holds the identity.
*
* @var string
*/
protected $identityClassName;
/**
* Identity property to check credential against.
*
* @var string
*/
protected $identityProperty;
/**
* Credential property to check credential against.
*
* @var string
*/
protected $credentialProperty;
/**
* User supplied identity.
*
* @var string
*/
protected $identityValue;
/**
* User supplied credential.
*
* @var string
*/
protected $credentialValue;
/**
* User supplied credential.
*
* @var mixed
*/
protected $credentialCallable;
/**
* User supplied identity
*
* @var mixed
*/
protected $identityCallable;
/**
* Contains the authentication results.
*
* @var array
*/
protected $authenticationResultInfo = null;
/**
* __construct() - Sets configuration options
*
* @param \Doctrine\Common\Persistence\ObjectManager $om
* @param string $identityClassName
* @param string $identityProperty
* @param string $credentialProperty
* @param null|array|Closure $credentialCallable
* @param null|array|Closure $identityCallable
* @return void
*/
public function __construct(
ObjectManager $objectManager,
$identityClassName,
$identityProperty = 'username',
$credentialProperty = 'password',
$credentialCallable = null,
$identityCallable = null
) {
$this->setObjectManager($objectManager);
$this->setIdentityClassName($identityClassName);
$this->setIdentityProperty($identityProperty);
$this->setCredentialProperty($credentialProperty);
if (null !== $credentialCallable) {
$this->setCredentialCallable($credentialCallable);
}
if (null !== $identityCallable) {
$this->setIdentityCallable($identityCallable);
}
}
/**
* {@inheritDoc}
*
* @return \Zend\Authentication\Result
*/
public function authenticate()
{
$this->authenticateSetup();
$repository = $this->om->getRepository($this->identityClassName);
$identity = $repository->findOneBy(array($this->identityProperty => $this->identityValue));
if (!$identity) {
$this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
$this->authenticationResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
return $this->authenticateCreateAuthResult();
}
$authResult = $this->authenticateValidateIdentity($identity);
return $authResult;
}
/**
* Sets the object manager to use.
*
* @param \Doctrine\Common\Persistence\ObjectManager $om
* @return self
*/
public function setObjectManager(ObjectManager $om)
{
$this->om = $om;
return $this;
}
/**
* Sets the identity class to use for authentication.
*
* @param string $identityClassName
* @return self
*/
public function setIdentityClassName($identityClassName)
{
$this->identityClassName = (string) $identityClassName;
return $this;
}
/**
* Set the value to be used as the identity
*
* @param mixed $value
* @return self
*/
public function setIdentityValue($identityValue)
{
$this->identityValue = $identityValue;
return $this;
}
/**
* Set the credential value to be used.
*
* @param mixed $credentialValue
* @return self
*/
public function setCredentialValue($credentialValue)
{
$this->credentialValue = $credentialValue;
return $this;
}
/**
* Set the credential callable to be used to transform the password
* before checking.
*
* @param string $callable
* @throws \InvalidArgumentException if argument is not a callable function
* @return self
*/
public function setCredentialCallable($callable)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException(sprintf(
'"%s" is not a callable fuction',
is_string($callable) ? $callable : gettype($callable)
));
}
$this->credentialCallable = $callable;
return $this;
}
/**
* Set the identity callable to be used when saving the identity property to the
* authentication result info.
*
* @param string $callable
* @throws \InvalidArgumentException if argument is not a callable function
* @return self
*/
public function setIdentityCallable($callable)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException(sprintf(
'"%s" is not a callable fuction',
is_string($callable) ? $callable : gettype($callable)
));
}
$this->identityCallable = $callable;
return $this;
}
/**
* Set the property name to be used as the identity property
*
* @param string $identityProperty
* @return self
*/
public function setIdentityProperty($identityProperty)
{
$this->identityProperty = (string) $identityProperty;
return $this;
}
/**
* Set the property name to be used as the credential property
*
* @param string $credentialField
* @return self
*/
public function setCredentialProperty($credentialProperty)
{
$this->credentialProperty = (string) $credentialProperty;
return $this;
}
/**
* This method attempts to validate that the record in the resultset is indeed a
* record that matched the identity provided to this adapter.
*
* @param object $identity
* @throws \UnexpectedValueException - if the identity is not the class expected
* @throws \BadMethodCallException - if the credentialProperty cannot be accessed on identity
* @return \Zend\Authentication\Result
*/
protected function authenticateValidateIdentity($identity)
{
if (!$identity instanceof $this->identityClassName) {
throw new \UnexpectedValueException(sprintf(
'Identity class type expected was %s, but got %s',
$this->identityClassName,
get_class($identity)
));
}
$getter = 'get' . ucfirst($this->credentialProperty);
$vars = get_object_vars($identity);
$documentCredential = null;
if (method_exists($identity, $getter)) {
$documentCredential = $identity->$getter();
} else if (isset($identity->{$this->credentialProperty}) || isset($vars[$this->credentialProperty])) {
$documentCredential = $identity->{$this->credentialProperty};
} else {
throw new \BadMethodCallException(sprintf(
'Property (%s) in (%s) is not accessible. You should implement %s::%s()',
$this->credentialProperty,
get_class($identity),
get_class($identity),
$getter
));
}
$credentialValue = $this->credentialValue;
$callable = $this->credentialCallable;
if ($callable) {
$credentialValue = call_user_func($callable, $identity, $credentialValue);
}
if ($credentialValue !== true && $credentialValue != $documentCredential) {
$this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
$this->authenticationResultInfo['messages'][] = 'Supplied credential is invalid.';
return $this->authenticateCreateAuthResult();
}
$identityCallable = $this->identityCallable;
if ($identityCallable) {
$identity = call_user_func($identityCallable, $identity);
}
$this->authenticationResultInfo['code'] = AuthenticationResult::SUCCESS;
$this->authenticationResultInfo['identity'] = $identity;
$this->authenticationResultInfo['messages'][] = 'Authentication successful.';
return $this->authenticateCreateAuthResult();
}
/**
* This method abstracts the steps involved with making sure that this adapter was
* indeed setup properly with all required pieces of information.
*
* @throws \Zend\Authentication\Adapter\Exception\RuntimeException - in the event that setup was not done properly
* @return bool
*/
protected function authenticateSetup()
{
if (!$this->identityClassName) {
throw new Exception\RuntimeException(
'An identityClassName must be supplied for the DoctrineObject authentication adapter'
);
}
if (!$this->identityProperty) {
throw new Exception\RuntimeException(
'An identity property must be supplied for the DoctrineObject authentication adapter'
);
}
if (!$this->credentialProperty) {
throw new Exception\RuntimeException(
'A credential property must be supplied for the DoctrineObject authentication adapter'
);
}
if (null === $this->identityValue) {
throw new Exception\RuntimeException(
'A value for the identity was not provided prior to authentication with DoctrineObject authentication '
. 'adapter'
);
}
if (null === $this->credentialValue) {
throw new Exception\RuntimeException(
'A credential value was not provided prior to authentication with DoctrineObject authentication adapter'
);
}
$this->authenticationResultInfo = array(
'code' => AuthenticationResult::FAILURE,
'identity' => $this->identityValue,
'messages' => array()
);
return true;
}
/**
* Creates a Zend_Auth_Result object from the information that has been collected
* during the authenticate() attempt.
*
* @return \Zend\Authentication\Result
*/
protected function authenticateCreateAuthResult()
{
return new AuthenticationResult(
$this->authenticationResultInfo['code'],
$this->authenticationResultInfo['identity'],
$this->authenticationResultInfo['messages']
);
}
}