1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZfcUser\Service; |
4
|
|
|
|
5
|
|
|
use Zend\Authentication\AuthenticationService; |
6
|
|
|
use Zend\Form\Form; |
7
|
|
|
use Zend\ServiceManager\ServiceManager; |
8
|
|
|
use Zend\Crypt\Password\Bcrypt; |
9
|
|
|
use Zend\Stdlib\Hydrator; |
10
|
|
|
use ZfcBase\EventManager\EventProvider; |
11
|
|
|
use ZfcUser\Mapper\UserInterface as UserMapperInterface; |
12
|
|
|
use ZfcUser\Options\UserServiceOptionsInterface; |
13
|
|
|
|
14
|
|
|
class User extends EventProvider |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var UserMapperInterface |
19
|
|
|
*/ |
20
|
|
|
protected $userMapper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AuthenticationService |
24
|
|
|
*/ |
25
|
|
|
protected $authService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Form |
29
|
|
|
*/ |
30
|
|
|
protected $loginForm; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Form |
34
|
|
|
*/ |
35
|
|
|
protected $registerForm; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Form |
39
|
|
|
*/ |
40
|
|
|
protected $changePasswordForm; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ServiceManager |
44
|
|
|
*/ |
45
|
|
|
protected $serviceManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var UserServiceOptionsInterface |
49
|
|
|
*/ |
50
|
|
|
protected $options; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Hydrator\ClassMethods |
54
|
|
|
*/ |
55
|
|
|
protected $formHydrator; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* createFromForm |
59
|
|
|
* |
60
|
|
|
* @param array $data |
61
|
|
|
* @return \ZfcUser\Entity\UserInterface |
62
|
|
|
* @throws Exception\InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function register(array $data) |
65
|
|
|
{ |
66
|
|
|
$class = $this->getOptions()->getUserEntityClass(); |
67
|
|
|
$user = new $class; |
68
|
|
|
$form = $this->getRegisterForm(); |
69
|
|
|
$form->setHydrator($this->getFormHydrator()); |
70
|
|
|
$form->bind($user); |
71
|
|
|
$form->setData($data); |
72
|
|
|
if (!$form->isValid()) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$user = $form->getData(); |
77
|
|
|
/* @var $user \ZfcUser\Entity\UserInterface */ |
78
|
|
|
|
79
|
|
|
$bcrypt = new Bcrypt; |
80
|
|
|
$bcrypt->setCost($this->getOptions()->getPasswordCost()); |
81
|
|
|
$user->setPassword($bcrypt->create($user->getPassword())); |
82
|
|
|
|
83
|
|
|
if ($this->getOptions()->getEnableUsername()) { |
84
|
|
|
$user->setUsername($data['username']); |
85
|
|
|
} |
86
|
|
|
if ($this->getOptions()->getEnableDisplayName()) { |
87
|
|
|
$user->setDisplayName($data['display_name']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// If user state is enabled, set the default state value |
91
|
|
|
if ($this->getOptions()->getEnableUserState()) { |
92
|
|
|
$user->setState($this->getOptions()->getDefaultUserState()); |
93
|
|
|
} |
94
|
|
|
$this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'form' => $form)); |
95
|
|
|
$this->getUserMapper()->insert($user); |
96
|
|
|
$this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $user, 'form' => $form)); |
97
|
|
|
return $user; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* change the current users password |
102
|
|
|
* |
103
|
|
|
* @param array $data |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
public function changePassword(array $data) |
107
|
|
|
{ |
108
|
|
|
$currentUser = $this->getAuthService()->getIdentity(); |
109
|
|
|
|
110
|
|
|
$oldPass = $data['credential']; |
111
|
|
|
$newPass = $data['newCredential']; |
112
|
|
|
|
113
|
|
|
$bcrypt = new Bcrypt; |
114
|
|
|
$bcrypt->setCost($this->getOptions()->getPasswordCost()); |
115
|
|
|
|
116
|
|
|
if (!$bcrypt->verify($oldPass, $currentUser->getPassword())) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$pass = $bcrypt->create($newPass); |
121
|
|
|
$currentUser->setPassword($pass); |
122
|
|
|
|
123
|
|
|
$this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data)); |
124
|
|
|
$this->getUserMapper()->update($currentUser); |
125
|
|
|
$this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data)); |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function changeEmail(array $data) |
131
|
|
|
{ |
132
|
|
|
$currentUser = $this->getAuthService()->getIdentity(); |
133
|
|
|
|
134
|
|
|
$bcrypt = new Bcrypt; |
135
|
|
|
$bcrypt->setCost($this->getOptions()->getPasswordCost()); |
136
|
|
|
|
137
|
|
|
if (!$bcrypt->verify($data['credential'], $currentUser->getPassword())) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$currentUser->setEmail($data['newIdentity']); |
142
|
|
|
|
143
|
|
|
$this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $currentUser, 'data' => $data)); |
144
|
|
|
$this->getUserMapper()->update($currentUser); |
145
|
|
|
$this->getEventManager()->trigger(__FUNCTION__.'.post', $this, array('user' => $currentUser, 'data' => $data)); |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* getUserMapper |
152
|
|
|
* |
153
|
|
|
* @return UserMapperInterface |
154
|
|
|
*/ |
155
|
|
|
public function getUserMapper() |
156
|
|
|
{ |
157
|
|
|
if (null === $this->userMapper) { |
158
|
|
|
$this->userMapper = $this->getServiceManager()->get('zfcuser_user_mapper'); |
159
|
|
|
} |
160
|
|
|
return $this->userMapper; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* setUserMapper |
165
|
|
|
* |
166
|
|
|
* @param UserMapperInterface $userMapper |
167
|
|
|
* @return User |
168
|
|
|
*/ |
169
|
|
|
public function setUserMapper(UserMapperInterface $userMapper) |
170
|
|
|
{ |
171
|
|
|
$this->userMapper = $userMapper; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* getAuthService |
177
|
|
|
* |
178
|
|
|
* @return AuthenticationService |
179
|
|
|
*/ |
180
|
|
|
public function getAuthService() |
181
|
|
|
{ |
182
|
|
|
if (null === $this->authService) { |
183
|
|
|
$this->authService = $this->getServiceManager()->get('zfcuser_auth_service'); |
184
|
|
|
} |
185
|
|
|
return $this->authService; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* setAuthenticationService |
190
|
|
|
* |
191
|
|
|
* @param AuthenticationService $authService |
192
|
|
|
* @return User |
193
|
|
|
*/ |
194
|
|
|
public function setAuthService(AuthenticationService $authService) |
195
|
|
|
{ |
196
|
|
|
$this->authService = $authService; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return Form |
202
|
|
|
*/ |
203
|
|
|
public function getRegisterForm() |
204
|
|
|
{ |
205
|
|
|
if (null === $this->registerForm) { |
206
|
|
|
$this->registerForm = $this->getServiceManager()->get('zfcuser_register_form'); |
207
|
|
|
} |
208
|
|
|
return $this->registerForm; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param Form $registerForm |
213
|
|
|
* @return User |
214
|
|
|
*/ |
215
|
|
|
public function setRegisterForm(Form $registerForm) |
216
|
|
|
{ |
217
|
|
|
$this->registerForm = $registerForm; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return Form |
223
|
|
|
*/ |
224
|
|
|
public function getChangePasswordForm() |
225
|
|
|
{ |
226
|
|
|
if (null === $this->changePasswordForm) { |
227
|
|
|
$this->changePasswordForm = $this->getServiceManager()->get('zfcuser_change_password_form'); |
228
|
|
|
} |
229
|
|
|
return $this->changePasswordForm; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param Form $changePasswordForm |
234
|
|
|
* @return User |
235
|
|
|
*/ |
236
|
|
|
public function setChangePasswordForm(Form $changePasswordForm) |
237
|
|
|
{ |
238
|
|
|
$this->changePasswordForm = $changePasswordForm; |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* get service options |
244
|
|
|
* |
245
|
|
|
* @return UserServiceOptionsInterface |
246
|
|
|
*/ |
247
|
|
|
public function getOptions() |
248
|
|
|
{ |
249
|
|
|
if (!$this->options instanceof UserServiceOptionsInterface) { |
250
|
|
|
$this->setOptions($this->getServiceManager()->get('zfcuser_module_options')); |
251
|
|
|
} |
252
|
|
|
return $this->options; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* set service options |
257
|
|
|
* |
258
|
|
|
* @param UserServiceOptionsInterface $options |
259
|
|
|
*/ |
260
|
|
|
public function setOptions(UserServiceOptionsInterface $options) |
261
|
|
|
{ |
262
|
|
|
$this->options = $options; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Retrieve service manager instance |
267
|
|
|
* |
268
|
|
|
* @return ServiceManager |
269
|
|
|
*/ |
270
|
|
|
public function getServiceManager() |
271
|
|
|
{ |
272
|
|
|
return $this->serviceManager; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set service manager instance |
277
|
|
|
* |
278
|
|
|
* @param ServiceManager $serviceManager |
279
|
|
|
* @return User |
280
|
|
|
*/ |
281
|
|
|
public function setServiceManager(ServiceManager $serviceManager) |
282
|
|
|
{ |
283
|
|
|
$this->serviceManager = $serviceManager; |
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Return the Form Hydrator |
289
|
|
|
* |
290
|
|
|
* @return \Zend\Stdlib\Hydrator\ClassMethods |
291
|
|
|
*/ |
292
|
|
|
public function getFormHydrator() |
293
|
|
|
{ |
294
|
|
|
if (!$this->formHydrator instanceof Hydrator\HydratorInterface) { |
295
|
|
|
$this->setFormHydrator($this->getServiceManager()->get('zfcuser_register_form_hydrator')); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $this->formHydrator; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set the Form Hydrator to use |
303
|
|
|
* |
304
|
|
|
* @param Hydrator\HydratorInterface $formHydrator |
305
|
|
|
* @return User |
306
|
|
|
*/ |
307
|
|
|
public function setFormHydrator(Hydrator\HydratorInterface $formHydrator) |
308
|
|
|
{ |
309
|
|
|
$this->formHydrator = $formHydrator; |
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|