-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.php
More file actions
553 lines (473 loc) · 20.1 KB
/
UserService.php
File metadata and controls
553 lines (473 loc) · 20.1 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php namespace Services\OpenId;
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Events\UserEmailUpdated;
use App\Events\UserPasswordResetSuccessful;
use App\Jobs\AddUserAction;
use App\Jobs\RevokeUserGrantsOnSessionRevocation;
use App\Jobs\PublishUserDeleted;
use App\Jobs\PublishUserUpdated;
use App\libs\Auth\Factories\UserFactory;
use App\libs\Auth\Repositories\IGroupRepository;
use App\libs\Utils\FileSystem\FileNameSanitizer;
use App\Mail\MonitoredSecurityGroupNotificationEmail;
use App\Services\AbstractService;
use App\Services\Auth\IUserIdentifierGeneratorService;
use Auth\Group;
use Auth\IUserNameGeneratorService;
use Auth\Repositories\IUserRepository;
use Auth\User;
use Doctrine\Common\Collections\ArrayCollection;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\utils\IEntity;
use OAuth2\IResourceServerContext;
use OAuth2\Models\IClient;
use OAuth2\Repositories\IClientRepository;
use OpenId\Services\IUserService;
use Utils\Db\ITransactionService;
use Utils\IPHelper;
use Utils\Services\ILogService;
use Utils\Services\IServerConfigurationService;
/**
* Class UserService
* @package Services\OpenId
*/
final class UserService extends AbstractService implements IUserService
{
/**
* @var IUserRepository
*/
private $repository;
/**
* @var ILogService
*/
private $log_service;
/**
* @var IUserNameGeneratorService
*/
private $user_name_generator;
/**
* @var IServerConfigurationService
*/
private $configuration_service;
/**
* @var IGroupRepository
*/
private $group_repository;
/**
* @var IClientRepository
*/
private $client_repository;
/**
* @var IResourceServerContext
*/
private $server_ctx;
/**
* @var IUserIdentifierGeneratorService
*/
private $identifier_service;
/**
* UserService constructor.
* @param IUserRepository $repository
* @param IGroupRepository $group_repository
* @param IUserNameGeneratorService $user_name_generator
* @param ITransactionService $tx_service
* @param IServerConfigurationService $configuration_service
* @param ILogService $log_service
* @param IResourceServerContext $server_ctx
* @param IUserIdentifierGeneratorService $identifier_service
*/
public function __construct
(
IUserRepository $repository,
IGroupRepository $group_repository,
IUserNameGeneratorService $user_name_generator,
ITransactionService $tx_service,
IServerConfigurationService $configuration_service,
ILogService $log_service,
IResourceServerContext $server_ctx,
IUserIdentifierGeneratorService $identifier_service,
IClientRepository $client_repository
)
{
parent::__construct($tx_service);
$this->repository = $repository;
$this->group_repository = $group_repository;
$this->user_name_generator = $user_name_generator;
$this->configuration_service = $configuration_service;
$this->log_service = $log_service;
$this->server_ctx = $server_ctx;
$this->identifier_service = $identifier_service;
$this->client_repository = $client_repository;
}
private function addUserCRUDAction(User $user, $payload, string $action_type = "CREATE") {
$payload_json = json_encode($payload);
$current_user_id = $this->server_ctx->getCurrentUserId();
if (!is_null($current_user_id)) {
$action = "{$action_type} USER BY USER {$this->server_ctx->getCurrentUserEmail()} ({$current_user_id}): {$payload_json}";
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
return;
}
//check if it's a service app
$app_type = $this->server_ctx->getApplicationType();
if (!empty($app_type) && $app_type == IClient::ApplicationType_Service) {
$action = "{$action_type} USER BY SERVICE {$this->server_ctx->getCurrentClientId()}: {$payload_json}";
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
return;
}
$action = "{$action_type} USER: {$payload_json}";
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
return;
}
/**
* @param int $user_id
* @return User
* @throws EntityNotFoundException
*/
public function updateLastLoginDate(int $user_id): User
{
return $this->tx_service->transaction(function () use ($user_id) {
$user = $this->repository->getById($user_id);
if (is_null($user) || !$user instanceof User) throw new EntityNotFoundException();
$user->updateLastLoginDate();
return $user;
});
}
/**
* @param int $user_id
* @return User
* @throws EntityNotFoundException
*/
public function updateFailedLoginAttempts(int $user_id): User
{
return $this->tx_service->transaction(function () use ($user_id) {
$user = $this->repository->getById($user_id);
if (!$user instanceof User) throw new EntityNotFoundException();
$user->updateLoginFailedAttempt();
return $user;
});
}
/**
* @param int $user_id
* @return User
* @throws EntityNotFoundException
*/
public function lockUser(int $user_id): User
{
return $this->tx_service->transaction(function () use ($user_id) {
$user = $this->repository->getById($user_id);
if (!$user instanceof User) throw new EntityNotFoundException();
return $user->lock();
});
}
/**
* @param int $user_id
* @return void
* @throws EntityNotFoundException
*/
public function unlockUser(int $user_id): User
{
return $this->tx_service->transaction(function () use ($user_id) {
$user = $this->repository->getById($user_id);
if (!$user instanceof User) throw new EntityNotFoundException();
return $user->unlock();
});
}
/**
* @param int $user_id
* @param bool $show_pic
* @param bool $show_full_name
* @param bool $show_email
* @param string $identifier
* @return User
* @throws EntityNotFoundException
* @throws ValidationException
*/
public function saveProfileInfo($user_id, $show_pic, $show_full_name, $show_email, $identifier): User
{
return $this->tx_service->transaction(function () use ($user_id, $show_pic, $show_full_name, $show_email, $identifier) {
$user = $this->repository->getById($user_id);
if (is_null($user) || !$user instanceof User) throw new EntityNotFoundException();
$former_user = $this->repository->getByIdentifier($identifier);
if (!is_null($former_user) && $former_user->getId() != $user_id) {
throw new ValidationException("there is already another user with that openid identifier");
}
$user->setPublicProfileShowPhoto($show_pic);
$user->setPublicProfileShowFullname($show_full_name);
$user->setPublicProfileShowEmail($show_email);
$user->setIdentifier($identifier);
return $user;
});
}
/**
* @param array $payload
* @return IEntity
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function create(array $payload): IEntity
{
$user = $this->tx_service->transaction(function () use ($payload) {
if (isset($payload["email"])) {
$former_user = $this->repository->getByEmailOrName(trim($payload["email"]));
if (!is_null($former_user))
throw new ValidationException(sprintf("email %s already belongs to another user", $payload["email"]));
}
if (isset($payload["identifier"]) && !empty($payload["identifier"])) {
$former_user = $this->repository->getByIdentifier(trim($payload["identifier"]));
if (!is_null($former_user))
throw new ValidationException(sprintf("identifier %s already belongs to another user", $payload["identifier"]));
}
$user = UserFactory::build($payload);
if (isset($payload['groups'])) {
foreach ($payload['groups'] as $group_id) {
$group = $this->group_repository->getById($group_id);
if (is_null($group))
throw new EntityNotFoundException("group not found");
$user->addToGroup($group);
}
}
$this->identifier_service->generateIdentifier($user);
$this->repository->add($user);
return $user;
});
$this->addUserCRUDAction($user, $payload);
return $user;
}
/**
* @param int $id
* @param array $payload
* @return IEntity
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function update(int $id, array $payload): IEntity
{
$password_changed = false;
$user = $this->tx_service->transaction(function () use ($id, $payload, &$password_changed) {
$user = $this->repository->getById($id);
if (!$user instanceof User)
throw new EntityNotFoundException("User not found.");
$former_email = $user->getEmail();
$former_password = $user->getPassword();
$current_user = !empty($this->server_ctx->getCurrentUserId()) ? $this->repository->getById($this->server_ctx->getCurrentUserId()) : Auth::user();
$should_ask_4_cur_password_validation = !(!is_null($current_user) &&
$user->getId() != $current_user->getId() && // is not the same user as current user
($current_user->isAdmin() || $current_user->isSuperAdmin()) && // current user should be admin
(!$user->isAdmin() && !$user->isSuperAdmin())) // user to update is not admin
&& $user->hasPasswordSet();
if ($should_ask_4_cur_password_validation) {
if (isset($payload["password"]) && !empty($payload["password"])) {
// changing password
if (!isset($payload['current_password']))
throw new ValidationException(sprintf("current_password is needed to update user password."));
if (!$user->checkPassword(trim($payload['current_password']))) {
throw new ValidationException(sprintf("current_password is not correct."));
}
}
}
if (isset($payload["email"]) && !empty($payload["email"])) {
$former_user = $this->repository->getByEmailOrName(trim($payload["email"]));
if (!is_null($former_user) && $former_user->getId() != $id)
throw new ValidationException(sprintf("email %s already belongs to another user", $payload["email"]));
}
if (isset($payload["identifier"]) && !empty($payload["identifier"])) {
$former_user = $this->repository->getByIdentifier(trim($payload["identifier"]));
if (!is_null($former_user) && $former_user->getId() != $id)
throw new ValidationException(sprintf("identifier %s already belongs to another user", $payload["identifier"]));
}
$user = UserFactory::populate($user, $payload);
if (isset($payload['groups'])) {
// update groups
// Get current groups
$current_groups = $user->getGroups();
$new_groups = new ArrayCollection();
foreach ($payload['groups'] as $group_id) {
$group = $this->group_repository->getById($group_id);
if (!$group instanceof Group) {
throw new EntityNotFoundException("Group not found.");
}
$new_groups->add($group);
}
// Remove groups not in the new list
foreach ($current_groups as $group) {
if (!$new_groups->contains($group)) {
$user->removeFromGroup($group);
}
}
// Add new groups not already in current
foreach ($new_groups as $group) {
if (!$current_groups->contains($group)) {
$user->addToGroup($group);
}
}
}
if ($former_email != $user->getEmail()) {
Log::warning(sprintf("UserService::update use id %s - email changed old %s - email new %s", $id, $former_email, $user->getEmail()));
$user->clearEmailVerification();
Event::dispatch(new UserEmailUpdated($user->getId()));
}
if ($former_password != $user->getPassword()) {
Log::warning(sprintf("UserService::update use id %s - password changed", $id));
$password_changed = true;
}
return $user;
});
if ($password_changed) {
Session::regenerate();
Event::dispatch(new UserPasswordResetSuccessful($user->getId()));
}
try {
if (Config::get("queue.enable_message_broker", false) == true)
PublishUserUpdated::dispatch($user)->onConnection('message_broker');
} catch (\Exception $ex) {
Log::warning($ex);
}
$this->addUserCRUDAction($user, $payload, "UPDATE");
return $user;
}
/**
* @param int $id
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function delete(int $id): void
{
$this->tx_service->transaction(function () use ($id) {
$user = $this->repository->getById($id);
if (is_null($user) || !$user instanceof User)
throw new EntityNotFoundException("user not found");
try {
if (Config::get("queue.enable_message_broker", false) == true)
PublishUserDeleted::dispatch($user)->onConnection('message_broker');
} catch (\Exception $ex) {
Log::warning($ex);
}
$this->repository->delete($user);
});
}
/**
* @inheritDoc
*/
public function updateProfilePhoto($user_id, UploadedFile $file, $max_file_size = 10485760): User
{
$user = $this->tx_service->transaction(function () use ($user_id, $file, $max_file_size) {
Log::debug(sprintf("UserService::updateProfilePhoto user %s", $user_id));
$allowed_extensions = ['png', 'jpg', 'jpeg'];
$user = $this->repository->getById($user_id);
if (!$user instanceof User)
throw new EntityNotFoundException("User not found.");
$fileName = $file->getClientOriginalName();
$fileExt = $file->extension() ?? pathinfo($fileName, PATHINFO_EXTENSION);
Log::debug(sprintf("UserService::updateProfilePhoto user %s fileName %s fileExt %s", $user_id, $fileName, $fileExt));
if (!in_array($fileExt, $allowed_extensions)) {
throw new ValidationException(sprintf("File does not has a valid extension (%s).", join(",", $allowed_extensions)));
}
if ($file->getSize() > $max_file_size) {
throw new ValidationException(sprintf("File exceeds max_file_size (%s MB).", ($max_file_size / 1024) / 1024));
}
$storage = Storage::disk(Config::get("filesystems.cloud"));
// normalize fileName
$fileName = FileNameSanitizer::sanitize($fileName);
// generate path
$path = sprintf("%s/%s", User::getProfilePicFolder(), $user->getId());
$index = 1;
// create unique file name
while($storage->exists(sprintf("%s/%s", $path, $fileName))){
Log::debug(sprintf("UserService::updateProfilePhoto user %s file %s already exists", $user_id, $fileName));
$fileName = sprintf("%s_%s", $index++, $fileName);
Log::debug(sprintf("UserService::updateProfilePhoto user %s new file name %s", $user_id, $fileName));
}
Log::debug(sprintf("UserService::updateProfilePhoto user %s saving file to swift path %s fileName %s", $user_id, $path, $fileName));
Storage::disk(Config::get("filesystems.cloud"))->putFileAs($path, $file, $fileName, 'public');
$user->setPic($fileName);
return $user;
});
try {
if (Config::get("queue.enable_message_broker", false) == true)
PublishUserUpdated::dispatch($user)->onConnection('message_broker');
} catch (\Exception $ex) {
Log::warning($ex);
}
return $user;
}
public function revokeAllGrantsOnSessionRevocation(int $user_id): void
{
$user = $this->tx_service->transaction(function () use ($user_id) {
$user = $this->repository->getById($user_id);
if (!$user instanceof User)
throw new EntityNotFoundException("User not found.");
$user->setRememberToken(\Illuminate\Support\Str::random(60));
return $user;
});
RevokeUserGrantsOnSessionRevocation::dispatch($user)->afterResponse();
}
public function notifyMonitoredSecurityGroupActivity
(
string $action,
int $user_id,
string $user_email,
string $user_name,
int $group_id,
string $group_name,
string $group_slug,
string $action_by
): void
{
$watcher_groups = Config::get('audit.monitored_security_groups_set_activity_watchers', []);
if (!count($watcher_groups)) {
Log::warning("UserService::notifyMonitoredSecurityGroupActivity No monitored security groups set for activity watchers.");
return;
}
$notified_users = [];
foreach ($watcher_groups as $watcher_group_slug) {
Log::debug(sprintf("UserService::notifyMonitoredSecurityGroupActivity processing %s", $watcher_group_slug));
$group = $this->group_repository->getOneBySlug($watcher_group_slug);
if(!$group instanceof Group) {
Log::warning(sprintf("UserService::notifyMonitoredSecurityGroupActivity group %s not found", $watcher_group_slug));
continue;
}
foreach($group->getUsers() as $user){
if(in_array($user->getId(), $notified_users)){
continue;
}
$notified_users[] = $user->getId();
Log::debug(sprintf("UserService::notifyMonitoredSecurityGroupActivity processing user %s", $user->getId()));
Mail::queue
(
new MonitoredSecurityGroupNotificationEmail
(
$user->getEmail(),
$action,
$action_by,
$user_id,
$user_email,
$user_name,
$group_id,
$group_name,
$group_slug
)
);
}
}
}
}