Ajoute la vue d'erreur et un validateur
This commit is contained in:
@@ -7,33 +7,37 @@ namespace Silex\Controller;
|
|||||||
use Silex\DI\DI;
|
use Silex\DI\DI;
|
||||||
use Silex\Http\HttpResponse;
|
use Silex\Http\HttpResponse;
|
||||||
use Silex\Model\User;
|
use Silex\Model\User;
|
||||||
|
use Silex\Validation\UserValidation;
|
||||||
|
|
||||||
class SecurityController
|
class SecurityController
|
||||||
{
|
{
|
||||||
public function login(DI $di): HttpResponse
|
public function login(DI $di): HttpResponse
|
||||||
{
|
{
|
||||||
$fail = false;
|
$errors = [];
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && UserValidation::isValidLogin($_POST,$errors)) {
|
||||||
$success = $di->getSecurity()->initLogin($_POST['login'], $_POST['password']);
|
$success = $di->getSecurity()->initLogin($_POST['login'], $_POST['password']);
|
||||||
if ($success) {
|
if ($success) {
|
||||||
HttpResponse::redirect($di->getRouter()->url(''));
|
HttpResponse::redirect($di->getRouter()->url(''));
|
||||||
|
} else {
|
||||||
|
$errors[] = 'Login or password invalid';
|
||||||
}
|
}
|
||||||
$fail = !$success;
|
|
||||||
}
|
}
|
||||||
return HttpResponse::found('login', ['fail' => $fail]);
|
return HttpResponse::found('login', ['errors' => $errors]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register(DI $di): HttpResponse
|
public function register(DI $di): HttpResponse
|
||||||
{
|
{
|
||||||
$fail = false;
|
$errors = [];
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && UserValidation::isValidUser($_POST,$errors)) {
|
||||||
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
|
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
|
||||||
if ($user !== null) {
|
if ($user !== null) {
|
||||||
HttpResponse::redirect($di->getRouter()->url(''));
|
HttpResponse::redirect($di->getRouter()->url(''));
|
||||||
}
|
}
|
||||||
$fail = $user === null;
|
if($user === null){
|
||||||
|
$errors[] = 'Login is already taken';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return HttpResponse::found('register', ['fail' => $fail]);
|
return HttpResponse::found('register', ['errors' => $errors]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function logout(DI $di): void
|
public function logout(DI $di): void
|
||||||
|
41
src/Silex/Validation/UserValidation.php
Normal file
41
src/Silex/Validation/UserValidation.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Silex\Validation;
|
||||||
|
|
||||||
|
final class UserValidation
|
||||||
|
{
|
||||||
|
public static function isValidLogin(array &$post, array &$errors): bool
|
||||||
|
{
|
||||||
|
if(empty($post['login'])) {
|
||||||
|
$errors[] = 'Login error';
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($post['password'])) {
|
||||||
|
$errors[] = 'Password error';
|
||||||
|
}
|
||||||
|
return empty($errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isValidUser(array &$post, array &$errors): bool
|
||||||
|
{
|
||||||
|
if(empty($post['login'])) {
|
||||||
|
$errors[] = 'Login empty error';
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($post['password'])) {
|
||||||
|
$errors[] = 'Password empty error';
|
||||||
|
}
|
||||||
|
|
||||||
|
if(empty($post['password-confirmation'])) {
|
||||||
|
$errors[] = 'Password confirmation empty error';
|
||||||
|
}
|
||||||
|
|
||||||
|
if($post['password'] !== $post['password-confirmation']){
|
||||||
|
$errors[] = 'Password confirmation not matching error';
|
||||||
|
}
|
||||||
|
|
||||||
|
return empty($errors);
|
||||||
|
}
|
||||||
|
}
|
11
views/errors.php
Normal file
11
views/errors.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php if ($params['errors']) :
|
||||||
|
foreach ($params['errors'] as $error) { ?>
|
||||||
|
<article class="message is-danger">
|
||||||
|
<div class="message-header">
|
||||||
|
<p>Auth failed</p>
|
||||||
|
</div>
|
||||||
|
<div class="message-body">
|
||||||
|
<?= $error?>
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
<?php } endif ?>
|
@@ -1,13 +1,4 @@
|
|||||||
<?php if ($params['fail']) : ?>
|
<?php require 'errors.php' ?>
|
||||||
<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">
|
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label" for="login">Login</label>
|
<label class="label" for="login">Login</label>
|
||||||
|
@@ -1,13 +1,4 @@
|
|||||||
<?php if ($params['fail']) : ?>
|
<?php require 'errors.php' ?>
|
||||||
<article class="message is-danger">
|
|
||||||
<div class="message-header">
|
|
||||||
<p>Registration failed</p>
|
|
||||||
</div>
|
|
||||||
<div class="message-body">
|
|
||||||
Login is already taken.
|
|
||||||
</div>
|
|
||||||
</article>
|
|
||||||
<?php endif ?>
|
|
||||||
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
|
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label" for="login">Login</label>
|
<label class="label" for="login">Login</label>
|
||||||
|
Reference in New Issue
Block a user