parent
53038a5022
commit
b4f9d31c2e
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Controller;
|
||||||
|
|
||||||
|
use Silex\DI\DI;
|
||||||
|
use Silex\Http\HttpResponse;
|
||||||
|
|
||||||
|
class SecurityController
|
||||||
|
{
|
||||||
|
public function login(DI $di): HttpResponse
|
||||||
|
{
|
||||||
|
$fail = false;
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$success = $di->getSecurity()->initLogin($_POST['login'], $_POST['password']);
|
||||||
|
if ($success) {
|
||||||
|
http_response_code(303);
|
||||||
|
header('Location: ' . $di->getRouter()->url(''));
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
var_dump($success);
|
||||||
|
$fail = !$success;
|
||||||
|
}
|
||||||
|
return HttpResponse::found('login', ['fail' => $fail]);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Gateway;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use Silex\Model\User;
|
||||||
|
|
||||||
|
class UserGateway
|
||||||
|
{
|
||||||
|
private PDO $pdo;
|
||||||
|
|
||||||
|
public function __construct(PDO $pdo)
|
||||||
|
{
|
||||||
|
$this->pdo = $pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getById(int $id): ?User
|
||||||
|
{
|
||||||
|
$req = $this->pdo->prepare('SELECT * FROM registered_user WHERE id_user = :id');
|
||||||
|
$req->execute(['id' => $id]);
|
||||||
|
$req->setFetchMode(PDO::FETCH_CLASS, User::class);
|
||||||
|
$user = $req->fetch();
|
||||||
|
return $user === false ? null : $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getByLogin(string $login): ?User
|
||||||
|
{
|
||||||
|
$req = $this->pdo->prepare('SELECT * FROM registered_user WHERE login = :login;');
|
||||||
|
$req->execute(['login' => $login]);
|
||||||
|
$req->setFetchMode(PDO::FETCH_CLASS, User::class);
|
||||||
|
$user = $req->fetch();
|
||||||
|
return $user === false ? null : $user;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Model;
|
||||||
|
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
private int $id_user;
|
||||||
|
private string $login;
|
||||||
|
private string $password;
|
||||||
|
private int $role;
|
||||||
|
|
||||||
|
public static function fromRawPassword(string $login, string $password): User
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user->login = $login;
|
||||||
|
$user->password = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getId(): int
|
||||||
|
{
|
||||||
|
return $this->id_user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLogin(): string
|
||||||
|
{
|
||||||
|
return $this->login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPasswordHash(): string
|
||||||
|
{
|
||||||
|
return $this->password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRole(): int
|
||||||
|
{
|
||||||
|
return $this->role;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Security;
|
||||||
|
|
||||||
|
use Silex\Gateway\UserGateway;
|
||||||
|
use Silex\Model\User;
|
||||||
|
|
||||||
|
const USER = 'USER';
|
||||||
|
|
||||||
|
class Security
|
||||||
|
{
|
||||||
|
private array $session;
|
||||||
|
private UserGateway $userGateway;
|
||||||
|
private ?User $user = null;
|
||||||
|
|
||||||
|
public function __construct(UserGateway $userGateway, array $session)
|
||||||
|
{
|
||||||
|
$this->userGateway = $userGateway;
|
||||||
|
$this->session = $session;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function initLogin(string $login, string $rawPassword): bool
|
||||||
|
{
|
||||||
|
$user = $this->userGateway->getByLogin($login);
|
||||||
|
if ($user === null || !password_verify($rawPassword, $user->getPasswordHash())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$this->session[USER] = $user->getId();
|
||||||
|
$this->user = $user;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
$this->user = null;
|
||||||
|
unset($this->session[USER]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCurrentUser(): ?User
|
||||||
|
{
|
||||||
|
if (!empty($this->session[USER]) && $this->user === null) {
|
||||||
|
$this->user = $this->userGateway->getById($this->session[USER]);
|
||||||
|
}
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
<?php if ($params['fail']) : ?>
|
||||||
|
<article class="message is-danger">
|
||||||
|
<div class="message-header">
|
||||||
|
<p>Auth failed</p>
|
||||||
|
</div>
|
||||||
|
<div class="message-body">
|
||||||
|
Login and/or password is invalid.
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
<?php endif ?>
|
||||||
|
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
|
||||||
|
<div class="field">
|
||||||
|
<label class="label" for="login">Login</label>
|
||||||
|
<div class="control">
|
||||||
|
<input class="input" type="text" id="login" name="login">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="label" for="password">Password</label>
|
||||||
|
<div class="control">
|
||||||
|
<input class="input" type="password" id="password" name="password">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="field">
|
||||||
|
<div class="control">
|
||||||
|
<button class="button is-link">Submit</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
Loading…
Reference in new issue