Leonid Nikitin
b5db913c24
Added functionalities to restrict certain user operations like update, password change, and deletion in demo mode. This is done to prevent demo users from modifying crucial data. Helper methods are created for standard re-usable checks. Also, Blade directive is added for frontend UI demo checks.
69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Services\Private;
|
|
|
|
use App\Dto\Request\Private\Profile\Update;
|
|
use App\Dto\Request\Private\Profile\UpdateSettings;
|
|
use App\Dto\Request\Private\User\UpdatePassword;
|
|
use App\Helpers\Helpers;
|
|
use App\Models\User;
|
|
use App\ServiceResults\ServiceResultError;
|
|
use App\ServiceResults\ServiceResultSuccess;
|
|
use App\Services\Service;
|
|
use App\Services\User\UserCommandHandler;
|
|
|
|
final class ProfileService extends Service
|
|
{
|
|
public function __construct(
|
|
private readonly UserCommandHandler $userCommandHandler
|
|
) { }
|
|
|
|
public function update(Update $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
{
|
|
if (Helpers::isDemoModeAndUserDenyUpdate($user)) {
|
|
return $this->errValidate(__('Demo Mode'));
|
|
}
|
|
|
|
try {
|
|
$data = [
|
|
'name' => $update->getName()
|
|
];
|
|
$this->userCommandHandler->handleUpdate($user, $data);
|
|
} catch (\Throwable $e) {
|
|
report($e->getMessage());
|
|
return $this->errService($e->getMessage());
|
|
}
|
|
return $this->ok(__('Profile saved successfully'));
|
|
}
|
|
|
|
public function updatePassword(UpdatePassword $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
{
|
|
if (Helpers::isDemoModeAndUserDenyUpdate($user)) {
|
|
return $this->errValidate(__('Demo Mode'));
|
|
}
|
|
|
|
try {
|
|
$this->userCommandHandler->handleUpdatePassword($user, $update->getPassword());
|
|
} catch (\Throwable $e) {
|
|
report($e->getMessage());
|
|
return $this->errService($e->getMessage());
|
|
}
|
|
return $this->ok(__('The password has been changed'));
|
|
}
|
|
|
|
public function updateSettings(UpdateSettings $update, User $user): ServiceResultError | ServiceResultSuccess
|
|
{
|
|
try {
|
|
$data = [
|
|
'lang' => $update->getLang(),
|
|
'timezone' => $update->getTimezone(),
|
|
];
|
|
$this->userCommandHandler->handleUpdate($user, $data);
|
|
} catch (\Throwable $e) {
|
|
report($e->getMessage());
|
|
return $this->errService($e->getMessage());
|
|
}
|
|
return $this->ok(__('The settings have been saved'));
|
|
}
|
|
}
|