Skip to content

Commit 937a65a

Browse files
authored
fix: make first and last name optional + track crud action (#68)
* fix: make first and last name optional + track crud action Signed-off-by: romanetar <roman_ag@hotmail.com> * fix: track crud action Signed-off-by: romanetar <roman_ag@hotmail.com> --------- Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent d07fbb4 commit 937a65a

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

app/Http/Controllers/Factories/UserValidationRulesFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public static function build(array $data, $update = false, ?User $currentUser =
7676
}
7777

7878
return [
79-
'first_name' => 'required|string',
80-
'last_name' => 'required|string',
79+
'first_name' => 'sometimes|string',
80+
'last_name' => 'sometimes|string',
8181
'email' => 'required|email',
8282
'identifier' => 'sometimes|string',
8383
'bio' => 'nullable|string',

app/Services/OpenId/UserService.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use App\Events\UserEmailUpdated;
1616
use App\Events\UserPasswordResetSuccessful;
17+
use App\Jobs\AddUserAction;
1718
use App\Jobs\PublishUserDeleted;
1819
use App\Jobs\PublishUserUpdated;
1920
use App\libs\Auth\Factories\UserFactory;
@@ -33,10 +34,14 @@
3334
use Illuminate\Support\Facades\Storage;
3435
use models\exceptions\EntityNotFoundException;
3536
use models\exceptions\ValidationException;
37+
use Models\OAuth2\Client;
3638
use models\utils\IEntity;
3739
use OAuth2\IResourceServerContext;
40+
use OAuth2\Models\IClient;
41+
use OAuth2\Repositories\IClientRepository;
3842
use OpenId\Services\IUserService;
3943
use Utils\Db\ITransactionService;
44+
use Utils\IPHelper;
4045
use Utils\Services\ILogService;
4146
use Utils\Services\IServerConfigurationService;
4247

@@ -71,6 +76,11 @@ final class UserService extends AbstractService implements IUserService
7176
*/
7277
private $group_repository;
7378

79+
/**
80+
* @var IClientRepository
81+
*/
82+
private $client_repository;
83+
7484
/**
7585
* @var IResourceServerContext
7686
*/
@@ -101,7 +111,8 @@ public function __construct
101111
IServerConfigurationService $configuration_service,
102112
ILogService $log_service,
103113
IResourceServerContext $server_ctx,
104-
IUserIdentifierGeneratorService $identifier_service
114+
IUserIdentifierGeneratorService $identifier_service,
115+
IClientRepository $client_repository
105116
)
106117
{
107118
parent::__construct($tx_service);
@@ -112,6 +123,29 @@ public function __construct
112123
$this->log_service = $log_service;
113124
$this->server_ctx = $server_ctx;
114125
$this->identifier_service = $identifier_service;
126+
$this->client_repository = $client_repository;
127+
}
128+
129+
private function addUserCRUDAction(User $user, $payload, string $action_type = "CREATE") {
130+
$payload_json = json_encode($payload);
131+
$current_user_id = $this->server_ctx->getCurrentUserId();
132+
133+
if (!is_null($current_user_id)) {
134+
$action = "{$action_type} USER BY USER {$this->server_ctx->getCurrentUserEmail()} ({$current_user_id}): {$payload_json}";
135+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
136+
return;
137+
}
138+
139+
//check if it's a service app
140+
if ($this->server_ctx->getApplicationType() == IClient::ApplicationType_Service) {
141+
$action = "{$action_type} USER BY SERVICE {$this->server_ctx->getCurrentClientId()}: {$payload_json}";
142+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
143+
return;
144+
}
145+
146+
$action = "{$action_type} USER: {$payload_json}";
147+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
148+
return;
115149
}
116150

117151
/**
@@ -212,7 +246,7 @@ public function saveProfileInfo($user_id, $show_pic, $show_full_name, $show_emai
212246
*/
213247
public function create(array $payload): IEntity
214248
{
215-
return $this->tx_service->transaction(function () use ($payload) {
249+
$user = $this->tx_service->transaction(function () use ($payload) {
216250
if (isset($payload["email"])) {
217251
$former_user = $this->repository->getByEmailOrName(trim($payload["email"]));
218252
if (!is_null($former_user))
@@ -240,6 +274,10 @@ public function create(array $payload): IEntity
240274

241275
return $user;
242276
});
277+
278+
$this->addUserCRUDAction($user, $payload);
279+
280+
return $user;
243281
}
244282

245283
/**
@@ -324,6 +362,8 @@ public function update(int $id, array $payload): IEntity
324362
Log::warning($ex);
325363
}
326364

365+
$this->addUserCRUDAction($user, $payload, "UPDATE");
366+
327367
return $user;
328368
}
329369

0 commit comments

Comments
 (0)