|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Users\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Modules\Core\BaseClasses\BaseService; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use App\Modules\Users\Repositories\UserRepository; |
|
8
|
|
|
use App\Modules\Permissions\Services\PermissionService; |
|
9
|
|
|
use App\Modules\OauthClients\Services\OauthClientService; |
|
10
|
|
|
use App\Modules\Notifications\Services\NotificationService; |
|
11
|
|
|
use App\Modules\Users\Proxy\LoginProxy; |
|
12
|
|
|
|
|
13
|
|
|
class UserService extends BaseService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var PermissionService |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $permissionService; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var LoginProxy |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $loginProxy; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var NotificationService |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $notificationService; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var OauthClientService |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $oauthClientService; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Init new object. |
|
37
|
|
|
* |
|
38
|
|
|
* @param UserRepository $repo |
|
39
|
|
|
* @param PermissionService $permissionService |
|
40
|
|
|
* @param LoginProxy $loginProxy |
|
41
|
|
|
* @param NotificationService $notificationService |
|
42
|
|
|
* @param OauthClientService $oauthClientService |
|
43
|
|
|
* @return void |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
UserRepository $repo, |
|
47
|
|
|
PermissionService $permissionService, |
|
48
|
|
|
LoginProxy $loginProxy, |
|
49
|
|
|
NotificationService $notificationService, |
|
50
|
|
|
OauthClientService $oauthClientService |
|
51
|
|
|
) { |
|
52
|
|
|
$this->permissionService = $permissionService; |
|
53
|
|
|
$this->loginProxy = $loginProxy; |
|
54
|
|
|
$this->notificationService = $notificationService; |
|
55
|
|
|
$this->oauthClientService = $oauthClientService; |
|
56
|
|
|
parent::__construct($repo); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return the logged in user account. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $relations |
|
63
|
|
|
* @return boolean |
|
64
|
|
|
*/ |
|
65
|
|
|
public function account($relations = ['roles.permissions']) |
|
66
|
|
|
{ |
|
67
|
|
|
$permissions = []; |
|
68
|
|
|
$user = $this->repo->find(\Auth::id(), $relations); |
|
69
|
|
|
foreach ($user->roles as $role) { |
|
70
|
|
|
$role->permissions->each(function ($permission) use (&$permissions) { |
|
71
|
|
|
$permissions[$permission->repo][$permission->id] = $permission->name; |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
$user->permissions = $permissions; |
|
75
|
|
|
|
|
76
|
|
|
return $user; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check if the logged in user or the given user |
|
81
|
|
|
* has the given permissions on the given model. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $permissionName |
|
84
|
|
|
* @param string $model |
|
85
|
|
|
* @param mixed $userId |
|
86
|
|
|
* @return boolean |
|
87
|
|
|
*/ |
|
88
|
|
|
public function can($permissionName, $model, $userId = false) |
|
89
|
|
|
{ |
|
90
|
|
|
$permission = $this->permissionService->first([ |
|
91
|
|
|
'and' => [ |
|
92
|
|
|
'model' => $model, |
|
93
|
|
|
'name' => $permissionName, |
|
94
|
|
|
'roles' => [ |
|
95
|
|
|
'op' => 'has', |
|
96
|
|
|
'val' => [ |
|
97
|
|
|
'users' => [ |
|
98
|
|
|
'op' => 'has', |
|
99
|
|
|
'val' => [ |
|
100
|
|
|
'users.id' => $userId ?: \Auth::id() |
|
101
|
|
|
] |
|
102
|
|
|
] |
|
103
|
|
|
] |
|
104
|
|
|
] |
|
105
|
|
|
] |
|
106
|
|
|
]); |
|
107
|
|
|
|
|
108
|
|
|
return $permission ? true : false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Check if the logged in or the given user has the given role. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string[] $roles |
|
115
|
|
|
* @param mixed $user |
|
116
|
|
|
* @return boolean |
|
117
|
|
|
*/ |
|
118
|
|
|
public function hasRoles($roles, $user = false) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repo->countRoles($user ?: \Auth::id(), $roles) ? true : false; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Assign the given role ids to the given user. |
|
125
|
|
|
* |
|
126
|
|
|
* @param integer $userId |
|
127
|
|
|
* @param array $roleIds |
|
128
|
|
|
* @return object |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function assignRoles($userId, $roleIds) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$user = false; |
|
133
|
|
|
\DB::transaction(function () use ($userId, $roleIds, &$user) { |
|
134
|
|
|
$user = $this->repo->find($userId); |
|
135
|
|
|
$this->repo->detachPermissions($userId); |
|
136
|
|
|
$this->repo->attachPermissions($userId, $roleIds); |
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
return $user; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Handle the login request to the application. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $email |
|
146
|
|
|
* @param string $password |
|
147
|
|
|
* @param string $role |
|
148
|
|
|
* @return object |
|
149
|
|
|
*/ |
|
150
|
|
|
public function login($email, $password, $role = false) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
if (! $user = $this->repo->first(['email' => $email])) { |
|
153
|
|
|
\Errors::loginFailed(); |
|
154
|
|
|
} elseif ($user->blocked) { |
|
155
|
|
|
\Errors::userIsBlocked(); |
|
156
|
|
|
} elseif (! config('skeleton.disable_confirm_email') && ! $user->confirmed) { |
|
157
|
|
|
\Errors::emailNotConfirmed(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return ['user' => $user, 'tokens' => $this->loginProxy->login($user->email, $password)]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Handle the social login request to the application. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $authCode |
|
167
|
|
|
* @param string $accessToken |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
public function loginSocial($authCode, $accessToken, $type) |
|
171
|
|
|
{ |
|
172
|
|
|
$access_token = $authCode ? Arr::get(\Socialite::driver($type)->getAccessTokenResponse($authCode), 'access_token') : $accessToken; |
|
173
|
|
|
$user = \Socialite::driver($type)->userFromToken($access_token); |
|
174
|
|
|
|
|
175
|
|
|
if (! $user->email) { |
|
176
|
|
|
\Errors::noSocialEmail(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (! $this->repo->first(['email' => $user->email])) { |
|
180
|
|
|
$this->register($user->email, '', true); |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->loginProxy->login($user->email, config('skeleton.social_pass')); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Handle the registration request. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $name |
|
190
|
|
|
* @param string $email |
|
191
|
|
|
* @param string $password |
|
192
|
|
|
* @param boolean $skipConfirmEmail |
|
193
|
|
|
* @return array |
|
194
|
|
|
*/ |
|
195
|
|
|
public function register($name, $email, $password, $skipConfirmEmail = false) |
|
196
|
|
|
{ |
|
197
|
|
|
$user = $this->repo->save([ |
|
198
|
|
|
'name' => $name, |
|
199
|
|
|
'email' => $email, |
|
200
|
|
|
'password' => $password, |
|
201
|
|
|
'confirmed' => $skipConfirmEmail |
|
202
|
|
|
]); |
|
203
|
|
|
|
|
204
|
|
|
if (! $skipConfirmEmail && ! config('skeleton.disable_confirm_email')) { |
|
205
|
|
|
$this->sendConfirmationEmail($user->email); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $user; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Block the user. |
|
213
|
|
|
* |
|
214
|
|
|
* @param integer $userId |
|
215
|
|
|
* @return object |
|
216
|
|
|
*/ |
|
217
|
|
|
public function block($userId) |
|
218
|
|
|
{ |
|
219
|
|
|
if (\Auth::id() == $userId) { |
|
220
|
|
|
\Errors::noPermissions(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return $this->repo->save(['id' => $userId, 'blocked' => 1]); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Unblock the user. |
|
228
|
|
|
* |
|
229
|
|
|
* @param integer $userId |
|
230
|
|
|
* @return object |
|
231
|
|
|
*/ |
|
232
|
|
|
public function unblock($userId) |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->repo->save(['id' => $userId, 'blocked' => 0]); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Send a reset link to the given user. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $email |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
|
|
public function sendReset($email) |
|
244
|
|
|
{ |
|
245
|
|
|
if (! $user = $this->repo->first(['email' => $email])) { |
|
246
|
|
|
\Errors::notFound('email'); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
$token = \Password::getService()->create($user); |
|
250
|
|
|
$this->notificationService->notify($user, 'ResetPassword', $token); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Reset the given user's password. |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $email |
|
257
|
|
|
* @param string $password |
|
258
|
|
|
* @param string $passwordConfirmation |
|
259
|
|
|
* @param string $token |
|
260
|
|
|
* @return string|void |
|
261
|
|
|
*/ |
|
262
|
|
|
public function resetPassword($email, $password, $passwordConfirmation, $token) |
|
263
|
|
|
{ |
|
264
|
|
|
$response = \Password::reset([ |
|
265
|
|
|
'email' => $email, |
|
266
|
|
|
'password' => $password, |
|
267
|
|
|
'password_confirmation' => $passwordConfirmation, |
|
268
|
|
|
'token' => $token |
|
269
|
|
|
], function ($user, $password) { |
|
270
|
|
|
$this->repo->save(['id' => $user->id, 'password' => $password]); |
|
271
|
|
|
}); |
|
272
|
|
|
|
|
273
|
|
|
switch ($response) { |
|
274
|
|
|
case \Password::PASSWORD_RESET: |
|
275
|
|
|
return 'success'; |
|
276
|
|
|
break; |
|
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
case \Password::INVALID_TOKEN: |
|
279
|
|
|
\Errors::invalidResetToken(); |
|
280
|
|
|
break; |
|
281
|
|
|
|
|
282
|
|
|
case \Password::INVALID_PASSWORD: |
|
283
|
|
|
\Errors::invalidResetPassword(); |
|
284
|
|
|
break; |
|
285
|
|
|
|
|
286
|
|
|
case \Password::INVALID_USER: |
|
287
|
|
|
\Errors::notFound('user'); |
|
288
|
|
|
break; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Change the logged in user password. |
|
294
|
|
|
* |
|
295
|
|
|
* @param string $password |
|
296
|
|
|
* @param string $oldPassword |
|
297
|
|
|
* @return void |
|
298
|
|
|
*/ |
|
299
|
|
|
public function changePassword($password, $oldPassword) |
|
300
|
|
|
{ |
|
301
|
|
|
$user = \Auth::user(); |
|
302
|
|
|
if (! \Hash::check($oldPassword, $user->password)) { |
|
303
|
|
|
\Errors::invalidOldPassword(); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$this->repo->save(['id' => $user->id, 'password' => $password]); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Confirm email using the confirmation code. |
|
311
|
|
|
* |
|
312
|
|
|
* @param string $confirmationCode |
|
313
|
|
|
* @return void |
|
314
|
|
|
*/ |
|
315
|
|
|
public function confirmEmail($confirmationCode) |
|
316
|
|
|
{ |
|
317
|
|
|
if (! $user = $this->repo->first(['confirmation_code' => $confirmationCode])) { |
|
318
|
|
|
\Errors::invalidConfirmationCode(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
$this->repo->save(['id' => $user->id, 'confirmed' => 1, 'confirmation_code' => null]); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Send the confirmation mail. |
|
326
|
|
|
* |
|
327
|
|
|
* @param string $email |
|
328
|
|
|
* @return void |
|
329
|
|
|
*/ |
|
330
|
|
|
public function sendConfirmationEmail($email) |
|
331
|
|
|
{ |
|
332
|
|
|
$user = $this->repo->first(['email' => $email]); |
|
333
|
|
|
if ($user->confirmed) { |
|
334
|
|
|
\Errors::emailAlreadyConfirmed(); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$this->repo->save(['id' => $user->id, 'confirmation_code' => sha1(microtime())]); |
|
338
|
|
|
$this->notificationService->notify($user, 'ConfirmEmail'); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Save the given data to the logged in user. |
|
343
|
|
|
* |
|
344
|
|
|
* @param string $name |
|
345
|
|
|
* @param string $email |
|
346
|
|
|
* @param string $profilePicture |
|
347
|
|
|
* @return void |
|
348
|
|
|
*/ |
|
349
|
|
|
public function saveProfile($name, $email, $profilePicture = false) |
|
350
|
|
|
{ |
|
351
|
|
|
if ($profilePicture) { |
|
|
|
|
|
|
352
|
|
|
$data['profile_picture'] = \Media::uploadImageBas64($profilePicture, 'users/profile_pictures'); |
|
|
|
|
|
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$data['id'] = \Auth::id(); |
|
|
|
|
|
|
356
|
|
|
return $this->repo->save([ |
|
357
|
|
|
'id' => \Auth::id(), |
|
358
|
|
|
'name' => $name, |
|
359
|
|
|
'email' => $email, |
|
360
|
|
|
'profilePicture' => $profilePicture, |
|
361
|
|
|
]); |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Logs out the user, revoke access token and refresh token. |
|
366
|
|
|
* |
|
367
|
|
|
* @return void |
|
368
|
|
|
*/ |
|
369
|
|
|
public function logout() |
|
370
|
|
|
{ |
|
371
|
|
|
$this->oauthClientService->revokeAccessToken(\Auth::user()->token()); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Attempt to refresh the access token using the given refresh token. |
|
376
|
|
|
* |
|
377
|
|
|
* @param string $refreshToken |
|
378
|
|
|
* @return array |
|
379
|
|
|
*/ |
|
380
|
|
|
public function refreshToken($refreshToken) |
|
381
|
|
|
{ |
|
382
|
|
|
return $this->loginProxy->refreshToken($refreshToken); |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.