forked from LibreCoopUruguay/MultipleLocalAuth
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAccountLifecycleService.php
More file actions
126 lines (105 loc) · 3.83 KB
/
Copy pathAccountLifecycleService.php
File metadata and controls
126 lines (105 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace MultipleLocalAuth;
/**
* Regras de ciclo de vida de conta isoladas do Provider (testáveis sem App):
* - troca de senha forçada por admin;
* - login barrado para conta na lixeira + confirmação de restauração via e-mail.
*/
class AccountLifecycleService
{
public const FORCE_PASSWORD_CHANGE_METADATA = 'forcePasswordChange';
public const PENDING_TRASH_RESTORE_CONFIRM_METADATA = 'pendingTrashRestoreConfirm';
public const PENDING_TRASH_RESTORE_SESSION_KEY = 'pendingTrashRestoreUserId';
/** Espelha MapasCulturais\Entity::STATUS_TRASH */
public const STATUS_TRASH = -10;
/** Entidades que User::delete() manda para a lixeira junto com o usuário. */
public const RELATED_ENTITY_TYPES = ['agents', 'spaces', 'projects', 'opportunities', 'events'];
public static function mustChangePassword(?string $forcePasswordChangeMetadata): bool
{
return $forcePasswordChangeMetadata === '1';
}
/**
* Pré-condições da ação admin forcePasswordChange().
* Retorna código de erro ou null se ok: 'permission' | 'not_found' | null
*/
public static function forcePasswordChangeError(bool $isAdmin, bool $userExists): ?string
{
if (!$isAdmin) {
return 'permission';
}
if (!$userExists) {
return 'not_found';
}
return null;
}
/**
* Pré-condição de doForcedPasswordChange(): precisa haver flag pendente.
*/
public static function canDoForcedPasswordChange(bool $mustChangePassword): bool
{
return $mustChangePassword;
}
/**
* Login com senha correta + conta na lixeira: não autentica; pede confirmação de restore.
*/
public static function shouldOfferTrashRestore(bool $hasOtherErrors, int $userStatus, int $trashStatus = self::STATUS_TRASH): bool
{
return !$hasOtherErrors && $userStatus === $trashStatus;
}
/**
* @return array{success: false, accountInTrash: true, profileName: string, errors: array}
*/
public static function buildAccountInTrashLoginResult(string $profileName, array $errors = []): array
{
return [
'success' => false,
'accountInTrash' => true,
'profileName' => $profileName,
'errors' => $errors,
];
}
/**
* Pré-condições de confirmRestoreAccount().
* Retorna 'expired' | 'not_trash' | null (ok).
*
* @param object|null $user objeto com propriedade status (User ou stub)
*/
public static function confirmRestoreError(?int $sessionUserId, $user, int $trashStatus = self::STATUS_TRASH): ?string
{
if (!$sessionUserId) {
return 'expired';
}
if (!$user || (int) $user->status !== $trashStatus) {
return 'not_trash';
}
return null;
}
public static function shouldRestoreOnEmailConfirm(?string $pendingTrashRestoreConfirmMetadata): bool
{
return $pendingTrashRestoreConfirmMetadata === '1';
}
public static function relatedEntityTypesToRestore(): array
{
return self::RELATED_ENTITY_TYPES;
}
public static function entityShouldBeUndeleted(int $entityStatus, int $trashStatus = self::STATUS_TRASH): bool
{
return $entityStatus === $trashStatus;
}
public static function storePendingTrashRestore(int $userId): void
{
$_SESSION[self::PENDING_TRASH_RESTORE_SESSION_KEY] = $userId;
}
public static function getPendingTrashRestoreUserId(): ?int
{
$userId = $_SESSION[self::PENDING_TRASH_RESTORE_SESSION_KEY] ?? null;
if ($userId === null || $userId === '') {
return null;
}
return (int) $userId;
}
public static function clearPendingTrashRestore(): void
{
unset($_SESSION[self::PENDING_TRASH_RESTORE_SESSION_KEY]);
}
}