Less home code more F3

This commit is contained in:
julien
2026-03-30 00:00:03 +02:00
parent d71cf304a9
commit fac7f60190
30 changed files with 818 additions and 1552 deletions

View File

@@ -23,41 +23,34 @@ class User extends DB\SQL\Mapper
)');
}
public function findById(int $id): ?array
public function findPublic(int $id): ?array
{
if ($id <= 0) {
return null;
}
$this->load(['id = ?', $id]);
if ($this->dry()) {
return null;
}
$data = $this->cast();
unset($data['password_hash']); // Ne jamais exposer le hash hors de l'authentification.
return $data;
}
public function findByUsername(string $username): ?array
{
$this->load(['username = ?', $username]);
return $this->dry() ? null : $this->cast();
return [
'id' => (int) $this->id,
'username' => (string) $this->username,
'created_at' => (string) $this->created_at,
];
}
public function create(string $username, string $password): int
{
$username = Base::instance()->clean($username);
$username = Base::instance()->clean(trim($username));
$password = trim($password);
if ($username === '' || $password === '') {
throw new RuntimeException('Nom dutilisateur et mot de passe obligatoires.');
}
if (mb_strlen($password) < 10) {
throw new RuntimeException('Le mot de passe doit contenir au moins 10 caractères.');
}
if ($this->findByUsername($username) !== null) {
$this->load(['username = ?', $username]);
if (!$this->dry()) {
throw new RuntimeException('Cet utilisateur existe déjà.');
}
@@ -67,6 +60,6 @@ class User extends DB\SQL\Mapper
$this->created_at = app_now();
$this->save();
return (int) $this->get('id');
return (int) $this->id;
}
}