add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks - Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists - Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services - Add Microsoft OIDC SSO, API token management, and user lifecycle features - Add swagger-ui vendor integration and OpenAPI spec - Add production Docker setup and bin/ scripts - Update composer dependencies, config, templates, and frontend assets throughout Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
156
lib/Service/User/UserLifecycleRestoreService.php
Normal file
156
lib/Service/User/UserLifecycleRestoreService.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Service\User;
|
||||
|
||||
use MintyPHP\DB;
|
||||
use MintyPHP\Repository\Support\RepoQuery;
|
||||
use MintyPHP\Repository\User\UserRepository;
|
||||
use MintyPHP\Service\Audit\UserLifecycleAuditService;
|
||||
|
||||
class UserLifecycleRestoreService
|
||||
{
|
||||
public static function restoreFromLifecycleAudit(int $auditId, int $actorUserId): array
|
||||
{
|
||||
if ($auditId <= 0 || $actorUserId <= 0) {
|
||||
return ['ok' => false, 'error' => 'invalid_request'];
|
||||
}
|
||||
|
||||
$db = DB::handle();
|
||||
$db->begin_transaction();
|
||||
try {
|
||||
$event = UserLifecycleAuditService::findDeleteEventForRestore($auditId, true);
|
||||
if (!$event) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'audit_event_not_found'];
|
||||
}
|
||||
if (!empty($event['restored_at'])) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'audit_event_already_restored'];
|
||||
}
|
||||
|
||||
$snapshot = UserLifecycleAuditService::decryptSnapshot($event);
|
||||
if (!$snapshot) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'snapshot_unavailable'];
|
||||
}
|
||||
|
||||
$uuid = trim((string) ($snapshot['uuid'] ?? ''));
|
||||
$email = trim((string) ($snapshot['email'] ?? ''));
|
||||
if ($uuid === '' || $email === '') {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'snapshot_invalid'];
|
||||
}
|
||||
|
||||
if (UserRepository::findByUuid($uuid)) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_uuid_exists'];
|
||||
}
|
||||
if (UserRepository::findByEmail($email)) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_email_exists'];
|
||||
}
|
||||
|
||||
$createdId = UserRepository::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),
|
||||
'totp_secret' => '',
|
||||
'theme' => self::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) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_create_failed'];
|
||||
}
|
||||
$restoredUserId = (int) $createdId;
|
||||
|
||||
$marked = UserLifecycleAuditService::markDeleteEventRestored($auditId, $actorUserId, $restoredUserId);
|
||||
if (!$marked) {
|
||||
$db->rollback();
|
||||
return ['ok' => false, 'error' => 'restore_mark_failed'];
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
$db->commit();
|
||||
return [
|
||||
'ok' => true,
|
||||
'restored_user_id' => $restoredUserId,
|
||||
'restored_user_uuid' => $uuid,
|
||||
'audit_id' => $auditId,
|
||||
];
|
||||
} catch (\Throwable $exception) {
|
||||
if ($db->errno === 0) {
|
||||
$db->rollback();
|
||||
} else {
|
||||
$db->rollback();
|
||||
}
|
||||
return ['ok' => false, 'error' => 'restore_unexpected_error'];
|
||||
}
|
||||
}
|
||||
|
||||
private static function nullableText(mixed $value): ?string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
return $value !== '' ? $value : null;
|
||||
}
|
||||
|
||||
private static 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;
|
||||
}
|
||||
|
||||
private static function randomPassword(): string
|
||||
{
|
||||
return bin2hex(random_bytes(24));
|
||||
}
|
||||
|
||||
private static 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