-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
150 lines (127 loc) · 4.76 KB
/
UserRepository.php
File metadata and controls
150 lines (127 loc) · 4.76 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace App\Repositories;
use App\Models\Event;
use App\Models\Stream;
use App\Models\User;
use DateTime;
use PDO;
class UserRepository
{
public function __construct(
private PDO $pdo,
private string $prefix
) {}
public function insert(string $email): User
{
$password = bin2hex(random_bytes(15));
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'users (email, password) VALUES (:email, :password)');
$stmt->execute([
':email' => $email,
':password' => password_hash($password, PASSWORD_DEFAULT),
]);
$user = new User();
$user->id = $this->pdo->lastInsertId();
$user->email = $email;
return $user;
}
/**
* Crée un utilisateur avec un mot de passe choisi et l'email non vérifié.
*/
public function insertWithPassword(string $email, string $password): User
{
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'users (email, password, email_verified) VALUES (:email, :password, 0)');
$stmt->execute([
':email' => $email,
':password' => password_hash($password, PASSWORD_DEFAULT),
]);
$user = new User();
$user->id = $this->pdo->lastInsertId();
$user->email = $email;
$user->email_verified = 0;
return $user;
}
/**
* Marque l'email d'un utilisateur comme vérifié.
*/
public function verifyEmail(User $user): void
{
$stmt = $this->pdo->prepare('UPDATE ' . $this->prefix . 'users SET email_verified = 1, reset_token = NULL, reset_token_expires_at = NULL WHERE id = :id');
$stmt->execute([':id' => $user->id]);
$user->email_verified = 1;
$user->reset_token = null;
$user->reset_token_expires_at = null;
}
public function insertRight(User $user, ?Stream $stream, ?Event $event): void
{
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'user_right (id_user, id_charity_event, id_charity_stream) VALUES (:id_user, :id_charity_event, :id_charity_stream)');
$stmt->execute([
':id_user' => $user->id,
':id_charity_event' => $event?->id,
':id_charity_stream' => $stream?->id,
]);
}
public function select(string $email): ?User
{
$stmt = $this->pdo->prepare('SELECT * FROM ' . $this->prefix . 'users WHERE email = ?');
$stmt->execute([$email]);
$stmt->setFetchMode(PDO::FETCH_CLASS, User::class);
return $stmt->fetch() ?: null;
}
public function selectAll(): array
{
$stmt = $this->pdo->query('SELECT id, email, role, email_verified, creation_date FROM ' . $this->prefix . 'users ORDER BY creation_date DESC');
$stmt->setFetchMode(PDO::FETCH_CLASS, User::class);
return $stmt->fetchAll();
}
public function findOrCreate(string $email): User
{
return $this->select($email) ?? $this->insert($email);
}
/**
* Recherche un utilisateur par son token de réinitialisation, uniquement si le token n'est pas expiré.
*/
public function selectByToken(string $token): ?User
{
$stmt = $this->pdo->prepare('
SELECT * FROM ' . $this->prefix . 'users
WHERE reset_token = ?
AND reset_token_expires_at > NOW()
');
$stmt->execute([$token]);
$stmt->setFetchMode(PDO::FETCH_CLASS, User::class);
return $stmt->fetch() ?: null;
}
/**
* Génère un token de réinitialisation valable 1 heure.
*/
public function insertResetToken(User $user): User
{
$token = bin2hex(random_bytes(30));
$expiresAt = (new DateTime())->modify('+1 hour')->format('Y-m-d H:i:s');
$stmt = $this->pdo->prepare('UPDATE ' . $this->prefix . 'users SET reset_token = :token, reset_token_expires_at = :expires WHERE id = :id');
$stmt->execute([
':id' => $user->id,
':token' => $token,
':expires' => $expiresAt,
]);
$user->reset_token = $token;
return $user;
}
public function updatePassword(User $user, ?string $password = null): User
{
if (!$password) {
$password = bin2hex(random_bytes(15));
}
$stmt = $this->pdo->prepare('UPDATE ' . $this->prefix . 'users SET password = :password, reset_token = null, reset_token_expires_at = null WHERE email = :email');
$stmt->execute([
':email' => $user->email,
':password' => password_hash($password, PASSWORD_DEFAULT),
]);
return $user;
}
public function delete(string $email): void
{
$stmt = $this->pdo->prepare('DELETE FROM ' . $this->prefix . 'users WHERE email = ?');
$stmt->execute([$email]);
}
}