diff --git a/app/Http/Controllers/Factories/UserValidationRulesFactory.php b/app/Http/Controllers/Factories/UserValidationRulesFactory.php index fd957b6e..53718249 100644 --- a/app/Http/Controllers/Factories/UserValidationRulesFactory.php +++ b/app/Http/Controllers/Factories/UserValidationRulesFactory.php @@ -76,8 +76,8 @@ public static function build(array $data, $update = false, ?User $currentUser = } return [ - 'first_name' => 'required|string', - 'last_name' => 'required|string', + 'first_name' => 'sometimes|string', + 'last_name' => 'sometimes|string', 'email' => 'required|email', 'identifier' => 'sometimes|string', 'bio' => 'nullable|string', diff --git a/app/Services/OpenId/UserService.php b/app/Services/OpenId/UserService.php index 65b6de00..04582c86 100644 --- a/app/Services/OpenId/UserService.php +++ b/app/Services/OpenId/UserService.php @@ -14,6 +14,7 @@ use App\Events\UserEmailUpdated; use App\Events\UserPasswordResetSuccessful; +use App\Jobs\AddUserAction; use App\Jobs\PublishUserDeleted; use App\Jobs\PublishUserUpdated; use App\libs\Auth\Factories\UserFactory; @@ -33,10 +34,14 @@ use Illuminate\Support\Facades\Storage; use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; +use Models\OAuth2\Client; 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; @@ -71,6 +76,11 @@ final class UserService extends AbstractService implements IUserService */ private $group_repository; + /** + * @var IClientRepository + */ + private $client_repository; + /** * @var IResourceServerContext */ @@ -101,7 +111,8 @@ public function __construct IServerConfigurationService $configuration_service, ILogService $log_service, IResourceServerContext $server_ctx, - IUserIdentifierGeneratorService $identifier_service + IUserIdentifierGeneratorService $identifier_service, + IClientRepository $client_repository ) { parent::__construct($tx_service); @@ -112,6 +123,29 @@ public function __construct $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 + if ($this->server_ctx->getApplicationType() == 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; } /** @@ -212,7 +246,7 @@ public function saveProfileInfo($user_id, $show_pic, $show_full_name, $show_emai */ public function create(array $payload): IEntity { - return $this->tx_service->transaction(function () use ($payload) { + $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)) @@ -240,6 +274,10 @@ public function create(array $payload): IEntity return $user; }); + + $this->addUserCRUDAction($user, $payload); + + return $user; } /** @@ -324,6 +362,8 @@ public function update(int $id, array $payload): IEntity Log::warning($ex); } + $this->addUserCRUDAction($user, $payload, "UPDATE"); + return $user; }