web: Ajoute une fixture pour le premier utilisateur
This commit is contained in:
7
web/autoload.php
Normal file
7
web/autoload.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/src/Oki/Config/SplClassLoader.php';
|
||||
require __DIR__ . '/src/Oki/Config/Config.php';
|
||||
|
||||
$loader = new SplClassLoader('Oki', __DIR__ . '/src');
|
||||
$loader->register();
|
10
web/fixtures/InitialUser.php
Normal file
10
web/fixtures/InitialUser.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use Oki\DI\DI;
|
||||
use Oki\Model\User;
|
||||
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
$user = User::fromRawPassword('test', 'test');
|
||||
$security = (new DI())->getSecurity();
|
||||
$security->register($user);
|
@@ -1,10 +1,6 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../src/Oki/Config/SplClassLoader.php';
|
||||
require __DIR__ . '/../src/Oki/Config/Config.php';
|
||||
|
||||
$loader = new SplClassLoader('Oki', __DIR__ . '/../src');
|
||||
$loader->register();
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
$home = new \Oki\Controller\HomeController();
|
||||
$api = new \Oki\Controller\ApiController();
|
||||
|
@@ -47,30 +47,14 @@ class UserController
|
||||
|
||||
public function register(DI $di): HttpResponse
|
||||
{
|
||||
|
||||
$errors = [];
|
||||
|
||||
$fail = false;
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
print_r($_POST);
|
||||
|
||||
if (empty($_POST['login']) || empty($_POST['password']) || empty($_POST['password_confirmation'])) {
|
||||
$html_code = 400;
|
||||
$errors[] = 'Fields are empty';
|
||||
} else {
|
||||
$login = $_POST['login'];
|
||||
$password = $_POST['password'];
|
||||
$password_confirmation = $_POST['password_confirmation'];
|
||||
|
||||
if ($this->insertUser($login, $password, $password_confirmation, $di) != 200) {
|
||||
$html_code = 400;
|
||||
$errors[] = 'Failed to register';
|
||||
}
|
||||
}
|
||||
if ($html_code == 200 || $html_code == 400) {
|
||||
return new HtmlResponse($html_code, 'register', ['register' => $errors]);
|
||||
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
|
||||
if ($user !== null) {
|
||||
//HttpResponse::redirect($di->getRouter()->url(''));
|
||||
}
|
||||
$fail = $user === null;
|
||||
}
|
||||
return new HtmlResponse(200, 'register');
|
||||
return new HtmlResponse(200, 'register', ['fail' => $fail]);
|
||||
}
|
||||
}
|
||||
|
@@ -37,6 +37,6 @@ class UserGateway
|
||||
public function insert(User $user)
|
||||
{
|
||||
$prep = $this->pdo->prepare("INSERT INTO user (login, password, perm) Values(:login, :password, :perm)");
|
||||
$prep->execute(['login' => $user->getLogin(), 'password' => $user->getPassword(), 'perm' => $user->getPerm()]);
|
||||
$prep->execute(['login' => $user->getLogin(), 'password' => $user->getPassword(), 'perm' => $user->getPermissions()]);
|
||||
}
|
||||
}
|
||||
|
@@ -6,19 +6,28 @@ namespace Oki\Model;
|
||||
|
||||
class User
|
||||
{
|
||||
private int $id;
|
||||
private int $id_user;
|
||||
private string $login;
|
||||
private string $password;
|
||||
private int $permissions;
|
||||
|
||||
public static function fromRawPassword(string $login, string $password, int $permissions = 0): User
|
||||
{
|
||||
$user = new User();
|
||||
$user->login = $login;
|
||||
$user->password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$user->permissions = $permissions;
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function setId(int $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->id_user = $id;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
return $this->id_user;
|
||||
}
|
||||
|
||||
public function setLogin(string $login)
|
||||
|
Reference in New Issue
Block a user