Less home code more F3
This commit is contained in:
@@ -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 d’utilisateur 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user