instances added god may help
This commit is contained in:
@@ -4,12 +4,20 @@ namespace MintyPHP\Service\User;
|
||||
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\Repository\Support\RepoQuery;
|
||||
use MintyPHP\Repository\User\UserRepository;
|
||||
use MintyPHP\Repository\User\UserReadRepository;
|
||||
use MintyPHP\Repository\User\UserWriteRepository;
|
||||
use MintyPHP\Service\Audit\UserLifecycleAuditService;
|
||||
|
||||
class UserLifecycleRestoreService
|
||||
{
|
||||
public static function restoreFromLifecycleAudit(int $auditId, int $actorUserId): array
|
||||
public function __construct(
|
||||
private readonly UserReadRepository $userReadRepository,
|
||||
private readonly UserWriteRepository $userWriteRepository,
|
||||
private readonly UserLifecycleAuditService $userLifecycleAuditService
|
||||
) {
|
||||
}
|
||||
|
||||
public function restoreFromLifecycleAudit(int $auditId, int $actorUserId): array
|
||||
{
|
||||
if ($auditId <= 0 || $actorUserId <= 0) {
|
||||
return ['ok' => false, 'error' => 'invalid_request'];
|
||||
@@ -18,7 +26,7 @@ class UserLifecycleRestoreService
|
||||
$db = DB::handle();
|
||||
$db->begin_transaction();
|
||||
try {
|
||||
$event = UserLifecycleAuditService::findDeleteEventForRestore($auditId, true);
|
||||
$event = $this->userLifecycleAuditService->findDeleteEventForRestore($auditId, true);
|
||||
if (!$event) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'audit_event_not_found'];
|
||||
@@ -28,7 +36,7 @@ class UserLifecycleRestoreService
|
||||
return ['ok' => false, 'error' => 'audit_event_already_restored'];
|
||||
}
|
||||
|
||||
$snapshot = UserLifecycleAuditService::decryptSnapshot($event);
|
||||
$snapshot = $this->userLifecycleAuditService->decryptSnapshot($event);
|
||||
if (!$snapshot) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'snapshot_unavailable'];
|
||||
@@ -41,35 +49,35 @@ class UserLifecycleRestoreService
|
||||
return ['ok' => false, 'error' => 'snapshot_invalid'];
|
||||
}
|
||||
|
||||
if (UserRepository::findByUuid($uuid)) {
|
||||
if ($this->userReadRepository->findByUuid($uuid)) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_uuid_exists'];
|
||||
}
|
||||
if (UserRepository::findByEmail($email)) {
|
||||
if ($this->userReadRepository->findByEmail($email)) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_email_exists'];
|
||||
}
|
||||
|
||||
$createdId = UserRepository::create([
|
||||
$createdId = $this->userWriteRepository->create([
|
||||
'uuid' => $uuid,
|
||||
'first_name' => trim((string) ($snapshot['first_name'] ?? '')),
|
||||
'last_name' => trim((string) ($snapshot['last_name'] ?? '')),
|
||||
'email' => $email,
|
||||
'profile_description' => self::nullableText($snapshot['profile_description'] ?? null),
|
||||
'job_title' => self::nullableText($snapshot['job_title'] ?? null),
|
||||
'phone' => self::nullableText($snapshot['phone'] ?? null),
|
||||
'mobile' => self::nullableText($snapshot['mobile'] ?? null),
|
||||
'short_dial' => self::nullableText($snapshot['short_dial'] ?? null),
|
||||
'address' => self::nullableText($snapshot['address'] ?? null),
|
||||
'postal_code' => self::nullableText($snapshot['postal_code'] ?? null),
|
||||
'city' => self::nullableText($snapshot['city'] ?? null),
|
||||
'country' => self::nullableText($snapshot['country'] ?? null),
|
||||
'region' => self::nullableText($snapshot['region'] ?? null),
|
||||
'hire_date' => self::nullableDate($snapshot['hire_date'] ?? null),
|
||||
'password' => self::randomPassword(),
|
||||
'locale' => self::nullableText($snapshot['locale'] ?? null),
|
||||
'profile_description' => $this->nullableText($snapshot['profile_description'] ?? null),
|
||||
'job_title' => $this->nullableText($snapshot['job_title'] ?? null),
|
||||
'phone' => $this->nullableText($snapshot['phone'] ?? null),
|
||||
'mobile' => $this->nullableText($snapshot['mobile'] ?? null),
|
||||
'short_dial' => $this->nullableText($snapshot['short_dial'] ?? null),
|
||||
'address' => $this->nullableText($snapshot['address'] ?? null),
|
||||
'postal_code' => $this->nullableText($snapshot['postal_code'] ?? null),
|
||||
'city' => $this->nullableText($snapshot['city'] ?? null),
|
||||
'country' => $this->nullableText($snapshot['country'] ?? null),
|
||||
'region' => $this->nullableText($snapshot['region'] ?? null),
|
||||
'hire_date' => $this->nullableDate($snapshot['hire_date'] ?? null),
|
||||
'password' => $this->randomPassword(),
|
||||
'locale' => $this->nullableText($snapshot['locale'] ?? null),
|
||||
'totp_secret' => '',
|
||||
'theme' => self::normalizeTheme($snapshot['theme'] ?? null),
|
||||
'theme' => $this->normalizeTheme($snapshot['theme'] ?? null),
|
||||
'primary_tenant_id' => null,
|
||||
'current_tenant_id' => null,
|
||||
'created_by' => $actorUserId,
|
||||
@@ -83,13 +91,17 @@ class UserLifecycleRestoreService
|
||||
}
|
||||
$restoredUserId = (int) $createdId;
|
||||
|
||||
$marked = UserLifecycleAuditService::markDeleteEventRestored($auditId, $actorUserId, $restoredUserId);
|
||||
$marked = $this->userLifecycleAuditService->markDeleteEventRestored(
|
||||
$auditId,
|
||||
$actorUserId,
|
||||
$restoredUserId
|
||||
);
|
||||
if (!$marked) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_mark_failed'];
|
||||
}
|
||||
|
||||
UserLifecycleAuditService::logRestore(
|
||||
$this->userLifecycleAuditService->logRestore(
|
||||
RepoQuery::uuidV4(),
|
||||
'manual',
|
||||
[
|
||||
@@ -123,13 +135,13 @@ class UserLifecycleRestoreService
|
||||
}
|
||||
}
|
||||
|
||||
private static function nullableText(mixed $value): ?string
|
||||
private function nullableText(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
return $value !== '' ? $value : null;
|
||||
}
|
||||
|
||||
private static function nullableDate(mixed $value): ?string
|
||||
private function nullableDate(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
if ($value === '') {
|
||||
@@ -141,16 +153,15 @@ class UserLifecycleRestoreService
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function randomPassword(): string
|
||||
private function randomPassword(): string
|
||||
{
|
||||
return bin2hex(random_bytes(24));
|
||||
}
|
||||
|
||||
private static function normalizeTheme(mixed $value): string
|
||||
private function normalizeTheme(mixed $value): string
|
||||
{
|
||||
$theme = strtolower(trim((string) $value));
|
||||
$themes = appThemes();
|
||||
return isset($themes[$theme]) ? $theme : appDefaultTheme();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user