|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Auth\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Authentication\Adapter\AbstractAdapter; |
|
6
|
|
|
use Zend\Authentication\Result; |
|
7
|
|
|
use Auth\Entity\Filter\CredentialFilter; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This class allow to authenticate with a user/password account. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
13
|
|
|
* @author Carsten Bleek <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class User extends AbstractAdapter |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* User entity repository |
|
19
|
|
|
* |
|
20
|
|
|
* @var \Core\Repository\RepositoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $repository; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Initial user. |
|
26
|
|
|
* |
|
27
|
|
|
* @var null|\Auth\Entity\UserInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $defaultUser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a new user authentication adapter |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Core\Repository\RepositoryInterface $repository User entity repository |
|
35
|
|
|
* @param null|string $identity |
|
36
|
|
|
* @param null|string $credential |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($repository, $identity = null, $credential = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->repository = $repository; |
|
41
|
|
|
$this->setIdentity($identity); |
|
42
|
|
|
$this->setCredential($credential); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the user repository |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Core\Repository\RepositoryInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getRepository() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->repository; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sets default user login and password. |
|
57
|
|
|
* |
|
58
|
|
|
* If no password is provided, |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $login |
|
61
|
|
|
* @param string $password |
|
62
|
|
|
* @param string $role (default='recruiter') |
|
63
|
|
|
* |
|
64
|
|
|
* @return self |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setDefaultUser($login, $password, $role = \Auth\Entity\User::ROLE_RECRUITER) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->defaultUser = array($login, $password, $role); |
|
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Performs an authentication attempt |
|
74
|
|
|
* |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
|
|
public function authenticate() |
|
79
|
|
|
{ |
|
80
|
|
|
/* @var $users \Auth\Repository\User */ |
|
81
|
|
|
$identity = $this->getIdentity(); |
|
82
|
|
|
$users = $this->getRepository(); |
|
83
|
|
|
$user = $users->findByLogin($identity, ['allowDeactivated' => true]); |
|
84
|
|
|
$filter = new CredentialFilter(); |
|
85
|
|
|
$credential = $this->getCredential(); |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
if (!$user || $user->getCredential() != $filter->filter($credential)) { |
|
89
|
|
|
return new Result(Result::FAILURE_CREDENTIAL_INVALID, $identity, array('User not known or invalid credential')); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new Result(Result::SUCCESS, $user->getId()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..