Files
breadcrumb-the-shire/lib/Service/User/UserLifecycleRestoreService.php

179 lines
6.7 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Service\User;
2026-03-04 15:56:58 +01:00
use MintyPHP\Repository\Support\DatabaseSessionRepository;
use MintyPHP\Repository\Support\RepoQuery;
2026-02-23 12:58:19 +01:00
use MintyPHP\Repository\User\UserReadRepository;
use MintyPHP\Repository\User\UserWriteRepository;
use MintyPHP\Service\Audit\UserLifecycleAuditService;
class UserLifecycleRestoreService
{
2026-02-23 12:58:19 +01:00
public function __construct(
private readonly UserReadRepository $userReadRepository,
private readonly UserWriteRepository $userWriteRepository,
2026-03-04 15:56:58 +01:00
private readonly UserLifecycleAuditService $userLifecycleAuditService,
private readonly DatabaseSessionRepository $databaseSessionRepository
2026-02-23 12:58:19 +01:00
) {
}
public function restoreFromLifecycleAudit(int $auditId, int $actorUserId): array
{
if ($auditId <= 0 || $actorUserId <= 0) {
return ['ok' => false, 'error' => 'invalid_request'];
}
2026-03-04 15:56:58 +01:00
$transactionStarted = false;
try {
2026-03-04 15:56:58 +01:00
$this->databaseSessionRepository->beginTransaction();
$transactionStarted = true;
2026-02-23 12:58:19 +01:00
$event = $this->userLifecycleAuditService->findDeleteEventForRestore($auditId, true);
if (!$event) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'audit_event_not_found'];
}
if (!empty($event['restored_at'])) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'audit_event_already_restored'];
}
2026-02-23 12:58:19 +01:00
$snapshot = $this->userLifecycleAuditService->decryptSnapshot($event);
if (!$snapshot) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'snapshot_unavailable'];
}
$uuid = trim((string) ($snapshot['uuid'] ?? ''));
$email = trim((string) ($snapshot['email'] ?? ''));
if ($uuid === '' || $email === '') {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'snapshot_invalid'];
}
2026-02-23 12:58:19 +01:00
if ($this->userReadRepository->findByUuid($uuid)) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_uuid_exists'];
}
2026-02-23 12:58:19 +01:00
if ($this->userReadRepository->findByEmail($email)) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_email_exists'];
}
2026-02-23 12:58:19 +01:00
$createdId = $this->userWriteRepository->create([
'uuid' => $uuid,
'first_name' => trim((string) ($snapshot['first_name'] ?? '')),
'last_name' => trim((string) ($snapshot['last_name'] ?? '')),
'email' => $email,
2026-02-23 12:58:19 +01:00
'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' => '',
2026-02-23 12:58:19 +01:00
'theme' => $this->normalizeTheme($snapshot['theme'] ?? null),
'primary_tenant_id' => null,
'current_tenant_id' => null,
'created_by' => $actorUserId,
'active' => 0,
'active_changed_at' => gmdate('Y-m-d H:i:s'),
'active_changed_by' => $actorUserId,
]);
if (!$createdId) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_create_failed'];
}
$restoredUserId = (int) $createdId;
2026-02-23 12:58:19 +01:00
$marked = $this->userLifecycleAuditService->markDeleteEventRestored(
$auditId,
$actorUserId,
$restoredUserId
);
if (!$marked) {
2026-03-04 15:56:58 +01:00
$this->rollbackQuietly();
return ['ok' => false, 'error' => 'restore_mark_failed'];
}
2026-02-23 12:58:19 +01:00
$this->userLifecycleAuditService->logRestore(
RepoQuery::uuidV4(),
'manual',
[
'deactivate_days' => (int) ($event['policy_deactivate_days'] ?? 0),
'delete_days' => (int) ($event['policy_delete_days'] ?? 0),
],
$actorUserId,
[
'id' => $restoredUserId,
'uuid' => $uuid,
'email' => $email,
],
'success',
null
);
2026-03-04 15:56:58 +01:00
$this->databaseSessionRepository->commitTransaction();
$transactionStarted = false;
return [
'ok' => true,
'restored_user_id' => $restoredUserId,
'restored_user_uuid' => $uuid,
'audit_id' => $auditId,
];
} catch (\Throwable $exception) {
2026-03-04 15:56:58 +01:00
if ($transactionStarted) {
$this->rollbackQuietly();
}
return ['ok' => false, 'error' => 'restore_unexpected_error'];
}
}
2026-03-04 15:56:58 +01:00
private function rollbackQuietly(): void
{
try {
$this->databaseSessionRepository->rollbackTransaction();
} catch (\Throwable $exception) {
// no-op
}
}
2026-02-23 12:58:19 +01:00
private function nullableText(mixed $value): ?string
{
$value = trim((string) $value);
return $value !== '' ? $value : null;
}
2026-02-23 12:58:19 +01:00
private function nullableDate(mixed $value): ?string
{
$value = trim((string) $value);
if ($value === '') {
return null;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
return null;
}
return $value;
}
2026-02-23 12:58:19 +01:00
private function randomPassword(): string
{
return bin2hex(random_bytes(24));
}
2026-02-23 12:58:19 +01:00
private function normalizeTheme(mixed $value): string
{
$theme = strtolower(trim((string) $value));
$themes = appThemes();
return isset($themes[$theme]) ? $theme : appDefaultTheme();
}
}