Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/Factories/UserValidationRulesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
44 changes: 42 additions & 2 deletions app/Services/OpenId/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -71,6 +76,11 @@ final class UserService extends AbstractService implements IUserService
*/
private $group_repository;

/**
* @var IClientRepository
*/
private $client_repository;

/**
* @var IResourceServerContext
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -240,6 +274,10 @@ public function create(array $payload): IEntity

return $user;
});

$this->addUserCRUDAction($user, $payload);

return $user;
}

/**
Expand Down Expand Up @@ -324,6 +362,8 @@ public function update(int $id, array $payload): IEntity
Log::warning($ex);
}

$this->addUserCRUDAction($user, $payload, "UPDATE");

return $user;
}

Expand Down