Refactoring
This commit is contained in:
parent
e4ed4c1862
commit
623de4b5e0
4 changed files with 67 additions and 46 deletions
|
@ -3,7 +3,9 @@
|
|||
|
||||
namespace App\Controllers\Auth;
|
||||
|
||||
use App\Controllers\Common\ValidateUser;
|
||||
use App\Controllers\Controller;
|
||||
use App\Exceptions\ValidationException;
|
||||
use App\Web\Mail;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
@ -11,6 +13,7 @@ use Slim\Exception\HttpNotFoundException;
|
|||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
use ValidateUser;
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
|
@ -51,10 +54,10 @@ class RegisterController extends Controller
|
|||
throw new HttpNotFoundException($request);
|
||||
}
|
||||
|
||||
if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) {
|
||||
$this->session->alert(lang('email_required'), 'danger');
|
||||
|
||||
return redirect($response, route('register.show'));
|
||||
try {
|
||||
$this->validateUser($request, $response, route('register.show'));
|
||||
} catch (ValidationException $e) {
|
||||
return $e->response();
|
||||
}
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ?', param($request, 'email'))->fetch()->count > 0) {
|
||||
|
@ -63,18 +66,6 @@ class RegisterController extends Controller
|
|||
return redirect($response, route('register.show'));
|
||||
}
|
||||
|
||||
if (param($request, 'username') === null) {
|
||||
$this->session->alert(lang('username_required'), 'danger');
|
||||
|
||||
return redirect($response, route('register.show'));
|
||||
}
|
||||
|
||||
if (param($request, 'password') === null) {
|
||||
$this->session->alert(lang('password_required'), 'danger');
|
||||
|
||||
return redirect($response, route('register.show'));
|
||||
}
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ?', param($request, 'username'))->fetch()->count > 0) {
|
||||
$this->session->alert(lang('username_taken'), 'danger');
|
||||
|
||||
|
|
|
@ -16,9 +16,9 @@ use Slim\Exception\HttpNotFoundException;
|
|||
use Slim\Exception\HttpUnauthorizedException;
|
||||
|
||||
/**
|
||||
* @property Session|null session
|
||||
* @property Session session
|
||||
* @property View view
|
||||
* @property DB|null database
|
||||
* @property DB database
|
||||
* @property Logger|null logger
|
||||
* @property Filesystem|null storage
|
||||
* @property Lang lang
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Exceptions\ValidationException;
|
||||
use App\Validators\ValidateUser;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
|
@ -9,6 +11,8 @@ use Slim\Exception\HttpUnauthorizedException;
|
|||
|
||||
class UserController extends Controller
|
||||
{
|
||||
use ValidateUser;
|
||||
|
||||
const PER_PAGE = 15;
|
||||
|
||||
/**
|
||||
|
@ -66,10 +70,10 @@ class UserController extends Controller
|
|||
*/
|
||||
public function store(Request $request, Response $response): Response
|
||||
{
|
||||
if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) {
|
||||
$this->session->alert(lang('email_required'), 'danger');
|
||||
|
||||
return redirect($response, route('user.create'));
|
||||
try {
|
||||
$this->validateUser($request, $response, route('user.create'));
|
||||
} catch (ValidationException $e) {
|
||||
return $e->response();
|
||||
}
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ?', param($request, 'email'))->fetch()->count > 0) {
|
||||
|
@ -78,18 +82,6 @@ class UserController extends Controller
|
|||
return redirect($response, route('user.create'));
|
||||
}
|
||||
|
||||
if (param($request, 'username') === null) {
|
||||
$this->session->alert(lang('username_required'), 'danger');
|
||||
|
||||
return redirect($response, route('user.create'));
|
||||
}
|
||||
|
||||
if (param($request, 'password') === null) {
|
||||
$this->session->alert(lang('password_required'), 'danger');
|
||||
|
||||
return redirect($response, route('user.create'));
|
||||
}
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ?', param($request, 'username'))->fetch()->count > 0) {
|
||||
$this->session->alert(lang('username_taken'), 'danger');
|
||||
|
||||
|
@ -169,26 +161,20 @@ class UserController extends Controller
|
|||
*/
|
||||
public function update(Request $request, Response $response, int $id): Response
|
||||
{
|
||||
$user = $this->getUser($request, $id, false);
|
||||
|
||||
if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) {
|
||||
$this->session->alert(lang('email_required'), 'danger');
|
||||
|
||||
return redirect($response, route('user.edit', ['id' => $id]));
|
||||
try {
|
||||
$this->validateUser($request, $response, route('user.edit', ['id' => $id]));
|
||||
} catch (ValidationException $e) {
|
||||
return $e->response();
|
||||
}
|
||||
|
||||
$user = $this->getUser($request, $id, false);
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `email` = ? AND `email` <> ?', [param($request, 'email'), $user->email])->fetch()->count > 0) {
|
||||
$this->session->alert(lang('email_taken'), 'danger');
|
||||
|
||||
return redirect($response, route('user.edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
if (param($request, 'username') === null) {
|
||||
$this->session->alert(lang('username_required'), 'danger');
|
||||
|
||||
return redirect($response, route('user.edit', ['id' => $id]));
|
||||
}
|
||||
|
||||
if ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `username` = ? AND `username` <> ?', [param($request, 'username'), $user->username])->fetch()->count > 0) {
|
||||
$this->session->alert(lang('username_taken'), 'danger');
|
||||
|
||||
|
|
44
app/Validators/ValidateUser.php
Normal file
44
app/Validators/ValidateUser.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Validators;
|
||||
|
||||
use App\Exceptions\ValidationException;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
trait ValidateUser
|
||||
{
|
||||
|
||||
/**
|
||||
* Partially validate a manager user request
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param $routeOnFail
|
||||
* @return bool
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function validateUser(Request $request, Response $response, $routeOnFail)
|
||||
{
|
||||
if (param($request, 'email') === null && !filter_var(param($request, 'email'), FILTER_VALIDATE_EMAIL)) {
|
||||
$this->session->alert(lang('email_required'), 'danger');
|
||||
|
||||
throw new ValidationException(redirect($response, $routeOnFail));
|
||||
}
|
||||
|
||||
if (param($request, 'username') === null) {
|
||||
$this->session->alert(lang('username_required'), 'danger');
|
||||
|
||||
throw new ValidationException(redirect($response, $routeOnFail));
|
||||
}
|
||||
|
||||
if (param($request, 'password') === null) {
|
||||
$this->session->alert(lang('password_required'), 'danger');
|
||||
|
||||
throw new ValidationException(redirect($response, $routeOnFail));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue