1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineModule\Authentication\Adapter; |
21
|
|
|
|
22
|
|
|
use DoctrineModule\Options\Authentication as AuthenticationOptions; |
23
|
|
|
use Zend\Authentication\Adapter\AbstractAdapter; |
24
|
|
|
use Zend\Authentication\Adapter\Exception; |
25
|
|
|
use Zend\Authentication\Result as AuthenticationResult; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Authentication adapter that uses a Doctrine object for verification. |
29
|
|
|
* |
30
|
|
|
* @license MIT |
31
|
|
|
* @link http://www.doctrine-project.org/ |
32
|
|
|
* @since 0.5.0 |
33
|
|
|
* @author Tim Roediger <[email protected]> |
34
|
|
|
* @author Michaël Gallego <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class ObjectRepository extends AbstractAdapter |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var AuthenticationOptions |
40
|
|
|
*/ |
41
|
|
|
protected $options; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Contains the authentication results. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $authenticationResultInfo = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor |
52
|
|
|
* |
53
|
|
|
* @param array|AuthenticationOptions $options |
54
|
|
|
*/ |
55
|
13 |
|
public function __construct($options = array()) |
56
|
|
|
{ |
57
|
13 |
|
$this->setOptions($options); |
58
|
11 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array|AuthenticationOptions $options |
62
|
|
|
*/ |
63
|
13 |
|
public function setOptions($options) |
64
|
|
|
{ |
65
|
13 |
|
if (!$options instanceof AuthenticationOptions) { |
66
|
11 |
|
$options = new AuthenticationOptions($options); |
67
|
9 |
|
} |
68
|
|
|
|
69
|
11 |
|
$this->options = $options; |
70
|
11 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return AuthenticationOptions |
75
|
|
|
*/ |
76
|
|
|
public function getOptions() |
77
|
|
|
{ |
78
|
|
|
return $this->options; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
8 |
|
public function authenticate() |
85
|
|
|
{ |
86
|
8 |
|
$this->setup(); |
87
|
6 |
|
$options = $this->options; |
88
|
|
|
$identity = $options |
89
|
6 |
|
->getObjectRepository() |
90
|
6 |
|
->findOneBy(array($options->getIdentityProperty() => $this->identity)); |
91
|
|
|
|
92
|
6 |
|
if (!$identity) { |
93
|
2 |
|
$this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND; |
94
|
2 |
|
$this->authenticationResultInfo['messages'][] = 'A record with the supplied identity could not be found.'; |
95
|
|
|
|
96
|
2 |
|
return $this->createAuthenticationResult(); |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
$authResult = $this->validateIdentity($identity); |
100
|
|
|
|
101
|
4 |
|
return $authResult; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* This method attempts to validate that the record in the resultset is indeed a |
106
|
|
|
* record that matched the identity provided to this adapter. |
107
|
|
|
* |
108
|
|
|
* @param object $identity |
109
|
|
|
* @throws Exception\UnexpectedValueException |
110
|
|
|
* @return AuthenticationResult |
111
|
|
|
*/ |
112
|
6 |
|
protected function validateIdentity($identity) |
113
|
|
|
{ |
114
|
6 |
|
$credentialProperty = $this->options->getCredentialProperty(); |
115
|
6 |
|
$getter = 'get' . ucfirst($credentialProperty); |
116
|
6 |
|
$documentCredential = null; |
|
|
|
|
117
|
|
|
|
118
|
6 |
|
if (method_exists($identity, $getter)) { |
119
|
3 |
|
$documentCredential = $identity->$getter(); |
120
|
6 |
|
} elseif (property_exists($identity, $credentialProperty)) { |
121
|
1 |
|
$documentCredential = $identity->{$credentialProperty}; |
122
|
1 |
|
} else { |
123
|
2 |
|
throw new Exception\UnexpectedValueException( |
124
|
2 |
|
sprintf( |
125
|
2 |
|
'Property (%s) in (%s) is not accessible. You should implement %s::%s()', |
126
|
2 |
|
$credentialProperty, |
127
|
2 |
|
get_class($identity), |
128
|
2 |
|
get_class($identity), |
129
|
|
|
$getter |
130
|
2 |
|
) |
131
|
2 |
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
4 |
|
$credentialValue = $this->credential; |
135
|
4 |
|
$callable = $this->options->getCredentialCallable(); |
136
|
|
|
|
137
|
4 |
|
if ($callable) { |
138
|
1 |
|
$credentialValue = call_user_func($callable, $identity, $credentialValue); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
4 |
|
if ($credentialValue !== true && $credentialValue !== $documentCredential) { |
142
|
2 |
|
$this->authenticationResultInfo['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID; |
143
|
2 |
|
$this->authenticationResultInfo['messages'][] = 'Supplied credential is invalid.'; |
144
|
|
|
|
145
|
2 |
|
return $this->createAuthenticationResult(); |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
$this->authenticationResultInfo['code'] = AuthenticationResult::SUCCESS; |
149
|
3 |
|
$this->authenticationResultInfo['identity'] = $identity; |
150
|
3 |
|
$this->authenticationResultInfo['messages'][] = 'Authentication successful.'; |
151
|
|
|
|
152
|
3 |
|
return $this->createAuthenticationResult(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* This method abstracts the steps involved with making sure that this adapter was |
157
|
|
|
* indeed setup properly with all required pieces of information. |
158
|
|
|
* |
159
|
|
|
* @throws Exception\RuntimeException - in the event that setup was not done properly |
160
|
|
|
*/ |
161
|
8 |
|
protected function setup() |
162
|
|
|
{ |
163
|
8 |
|
if (null === $this->identity) { |
164
|
1 |
|
throw new Exception\RuntimeException( |
165
|
|
|
'A value for the identity was not provided prior to authentication with ObjectRepository ' |
166
|
|
|
. 'authentication adapter' |
167
|
1 |
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
7 |
|
if (null === $this->credential) { |
171
|
1 |
|
throw new Exception\RuntimeException( |
172
|
|
|
'A credential value was not provided prior to authentication with ObjectRepository' |
173
|
|
|
. ' authentication adapter' |
174
|
1 |
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
6 |
|
$this->authenticationResultInfo = array( |
178
|
6 |
|
'code' => AuthenticationResult::FAILURE, |
179
|
6 |
|
'identity' => $this->identity, |
180
|
6 |
|
'messages' => array() |
181
|
6 |
|
); |
182
|
6 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Creates a Zend\Authentication\Result object from the information that has been collected |
186
|
|
|
* during the authenticate() attempt. |
187
|
|
|
* |
188
|
|
|
* @return \Zend\Authentication\Result |
189
|
|
|
*/ |
190
|
4 |
|
protected function createAuthenticationResult() |
191
|
|
|
{ |
192
|
4 |
|
return new AuthenticationResult( |
193
|
4 |
|
$this->authenticationResultInfo['code'], |
194
|
4 |
|
$this->authenticationResultInfo['identity'], |
195
|
4 |
|
$this->authenticationResultInfo['messages'] |
196
|
4 |
|
); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.