Permet l'inscription d'utilisateurs

This commit is contained in:
2022-11-30 11:21:48 +01:00
parent 1bce756470
commit d6a3a7938a
7 changed files with 81 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ namespace Silex\Controller;
use Silex\DI\DI;
use Silex\Http\HttpResponse;
use Silex\Model\User;
class SecurityController
{
@@ -19,9 +20,23 @@ class SecurityController
header('Location: ' . $di->getRouter()->url(''));
exit();
}
var_dump($success);
$fail = !$success;
}
return HttpResponse::found('login', ['fail' => $fail]);
}
}
public function register(DI $di): HttpResponse
{
$fail = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
if ($user !== null) {
http_response_code(303);
header('Location: ' . $di->getRouter()->url(''));
exit();
}
$fail = $user === null;
}
return HttpResponse::found('register', ['fail' => $fail]);
}
}

View File

@@ -33,4 +33,12 @@ class UserGateway
$user = $req->fetch();
return $user === false ? null : $user;
}
public function insert(User $user): bool
{
$req = $this->pdo->prepare('INSERT INTO registered_user (login, password, role) VALUES (:login, :password, :role);');
$req->execute(['login' => $user->getLogin(), 'password' => $user->getPasswordHash(), 'role' => $user->getRole()]);
$user->setId(intval($this->pdo->lastInsertId()));
return true;
}
}

View File

@@ -11,11 +11,12 @@ class User
private string $password;
private int $role;
public static function fromRawPassword(string $login, string $password): User
public static function fromRawPassword(string $login, string $password, int $role = 0): User
{
$user = new User();
$user->login = $login;
$user->password = password_hash($password, PASSWORD_DEFAULT);
$user->role = $role;
return $user;
}
@@ -38,4 +39,9 @@ class User
{
return $this->role;
}
public function setId(int $id)
{
$this->id_user = $id;
}
}

View File

@@ -45,4 +45,14 @@ class Security
}
return $this->user;
}
public function register(User $user): ?User
{
if (!$this->userGateway->insert($user)) {
return null;
}
$this->session[USER] = $user->getId();
$this->user = $user;
return $user;
}
}