Skip to content

Commit 0731c96

Browse files
committed
fix: make first and last name optional + track crud action
Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent f209733 commit 0731c96

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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: 44 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,31 @@ 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 = Auth::user();
132+
133+
if ($current_user instanceof User) {
134+
$action = "{$action_type} USER BY USER {$current_user->getEmail()} ({$current_user->getId()}): {$payload_json}";
135+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
136+
return;
137+
}
138+
139+
//check if it's a service app
140+
$current_client = $this->client_repository->getClientById($this->server_ctx->getCurrentClientId());
141+
if ($current_client instanceof Client and
142+
$current_client->getApplicationType() == IClient::ApplicationType_Service) {
143+
$action = "{$action_type} USER BY SERVICE {$current_client->getApplicationName()} ({$current_client->getId()}): {$payload_json}";
144+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
145+
return;
146+
}
147+
148+
$action = "{$action_type} USER: {$payload_json}";
149+
AddUserAction::dispatch($user->getId(), IPHelper::getUserIp(), $action);
150+
return;
115151
}
116152

117153
/**
@@ -212,7 +248,7 @@ public function saveProfileInfo($user_id, $show_pic, $show_full_name, $show_emai
212248
*/
213249
public function create(array $payload): IEntity
214250
{
215-
return $this->tx_service->transaction(function () use ($payload) {
251+
$user = $this->tx_service->transaction(function () use ($payload) {
216252
if (isset($payload["email"])) {
217253
$former_user = $this->repository->getByEmailOrName(trim($payload["email"]));
218254
if (!is_null($former_user))
@@ -240,6 +276,10 @@ public function create(array $payload): IEntity
240276

241277
return $user;
242278
});
279+
280+
$this->addUserCRUDAction($user, $payload);
281+
282+
return $user;
243283
}
244284

245285
/**
@@ -324,6 +364,8 @@ public function update(int $id, array $payload): IEntity
324364
Log::warning($ex);
325365
}
326366

367+
$this->addUserCRUDAction($user, $payload, "UPDATE");
368+
327369
return $user;
328370
}
329371

0 commit comments

Comments
 (0)