Skip to content

Commit

Permalink
update: routing and validator support
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Mar 26, 2022
1 parent 1a99368 commit 69b9eba
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ COPY . ./

EXPOSE 8080

CMD php console server:start --host=0.0.0.0 --port=8080
CMD php console serve --host=0.0.0.0 --port=8080
5 changes: 2 additions & 3 deletions app/Http/Actions/UserActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public static function update(array $data, string $email)
}

$user->fill($data);
$user = $user->save();

return $user;

return $user->save();
}

public static function updatPassword(string $password, string $email)
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/EmailVerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public function notify(Request $request, Response $response)
public function verify(Request $request, Response $response)
{
if (!$request->hasQuery('email', 'token')) {
$response->send(__('bad_request'), [], 400);
$response->send(data: __('bad_request'), code: 400);
}

$token = Token::findBy('email', $request->email);

if (!$token || $token->token !== $request->token) {
$response->send(__('invalid_password_reset_link'), [], 400);
$response->send(data: __('invalid_password_reset_link'), code: 400);
}

if (Carbon::parse($token->expire)->lt(Carbon::now())) {
$response->send(__('expired_password_reset_link'), [], 400);
$response->send(data: __('expired_password_reset_link'), code: 400);
}

$token->delete();
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public function notify(Request $request, Response $response)
public function reset(Request $request, Response $response)
{
if (!$request->hasQuery('email', 'token')) {
$response->send(__('bad_request'), [], 400);
$response->send(data: __('bad_request'), code: 400);
}

$token = Token::findBy('email', $request->email);

if (!$token || $token->token !== $request->token) {
$response->send(__('invalid_password_reset_link'), [], 400);
$response->send(data: __('invalid_password_reset_link'), code: 400);
}

if (Carbon::parse($token->expire)->lt(Carbon::now())) {
$response->send(__('expired_password_reset_link'), [], 400);
$response->send(data: __('expired_password_reset_link'), code: 400);
}

$token->delete();
Expand All @@ -64,7 +64,7 @@ public function reset(Request $request, Response $response)

public function update(Request $request, Response $response, LoginValidator $loginValidator)
{
$loginValidator->validate($request->inputs());
$loginValidator->validate($request->inputs(), $response);
$user = UserActions::updatPassword($request->password, $request->email);

if (!$user) {
Expand Down
9 changes: 2 additions & 7 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace App\Http\Controllers\Auth;

use Carbon\Carbon;
use Core\Http\Request;
use Core\Support\Auth;
use Core\Support\Alert;
Expand All @@ -28,19 +27,15 @@ public function index(Request $request, Response $response)

public function authenticate(Request $request, Response $response, LoginValidator $loginValidator)
{
$loginValidator->validate($request->inputs());
$loginValidator->validate($request->inputs(), $response);

if (Auth::attempt($request->only('email', 'password'), $request->has('remember'))) {
if (Auth::attempt($response, $request->only('email', 'password'), $request->has('remember'))) {
$uri = !Session::has('intended') ? config('app.home') : Session::pull('intended');

Alert::toast(__('welcome', ['name' => Auth::get('name')]))->success();
$response->redirect()->to($uri)->go();
}

if (Auth::attemptsExceeded()) {
$response->redirect()->back()->with('auth_attempts_timeout', Carbon::now()->addMinutes(config('security.auth.unlock_timeout'))->toDateTimeString())->go();
}

Alert::default(__('login_failed'))->error();
$response->redirect()->to('login')->withInputs($request->only('email', 'password'))->withErrors([__('login_failed')])->go();
}
Expand Down
18 changes: 9 additions & 9 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public function index(Request $request, Response $response)

public function register(Request $request, Response $response, RegisterValidator $registerValidator)
{
$data = $registerValidator->validate($request->inputs())->validated();
$user = UserActions::create($data);

if (!config('security.auth.email_verification')) {
Mail::send(new WelcomeMail($user->email, $user->name));
Alert::default(__('account_created'))->success();

$response->redirect()->to('login')->go();
$validator = $registerValidator->validate($request->inputs(), $response);
$user = UserActions::create($validator->validated());

if (config('security.auth.email_verification')) {
$response->redirect()->to('email/notify')->go();
}

$response->redirect()->to('email/notify')->go();
Mail::send(new WelcomeMail($user->email, $user->name));
Alert::default(__('account_created'))->success();

$response->redirect()->to('login')->go();
}
}
4 changes: 2 additions & 2 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

/**
* Custom routes paths configuration
* Routes paths configuration
*/

return [
'paths' => [],
'paths' => ['/'],
];
2 changes: 1 addition & 1 deletion core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()
Whoops::register();
}

public function execute()
public function run()
{
$response = new Response();

Expand Down
21 changes: 21 additions & 0 deletions core/Exceptions/RoutesPathsNotDefinedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* @copyright (2019 - 2022) - N'Guessan Kouadio Elisée ([email protected])
* @license MIT (https://opensource.org/licenses/MIT)
* @link https://github.com/eliseekn/tinymvc
*/

namespace Core\Exceptions;

use Exception;

/**
* This exception occurs when routes paths not defined
*/
class RoutesPathsNotDefinedException extends Exception
{
public function __construct() {
parent::__construct('Routes paths not defined');
}
}
12 changes: 6 additions & 6 deletions core/Http/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ public function go(int $code = 302)
exit($this->headers('Location', url($this->uri), $code));
}

public function withCookie(string $name, string $value, int $expire = 3600, bool $secure = false, string $domain = ''): self
{
Cookies::create($name, $value, $expire, $secure, $domain);
return $this;
}

public function with(string $key, $data): self
{
Session::create($key, $data);
Expand All @@ -76,4 +70,10 @@ public function withInputs(array $inputs): self
Session::create('inputs', $inputs);
return $this;
}

public function withCookie(string $name, string $value, int $expire = 3600, bool $secure = false, string $domain = ''): self
{
Cookies::create($name, $value, $expire, $secure, $domain);
return $this;
}
}
4 changes: 2 additions & 2 deletions core/Http/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Validator implements ValidatorInterface
protected $errors;
protected array $inputs = [];

public function validate(array $inputs): self
public function validate(array $inputs, Response $response): self
{
$this->inputs = $inputs;
$this->errors = GUMP::is_valid($this->inputs, $this->rules(), $this->messages());

if ($this->fails()) (new Response())->redirect()->back()->withErrors($this->errors())->withInputs($this->inputs)->go();
if ($this->fails()) $response->redirect()->back()->withErrors($this->errors())->withInputs($this->inputs)->go();
return $this;
}

Expand Down
4 changes: 3 additions & 1 deletion core/Http/Validator/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

namespace Core\Http\Validator;

use Core\Http\Response\Response;

interface ValidatorInterface
{
public function addCustomRule(string $rule, callable $callback, string $error_message): self;

public function validate(array $inputs): self;
public function validate(array $inputs, Response $response): self;

public function fails(): bool;

Expand Down
31 changes: 22 additions & 9 deletions core/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Core\Routing;

use Core\Exceptions\RoutesPathsNotDefinedException;
use Core\Support\Storage;
use Core\Http\Response\Response;

Expand Down Expand Up @@ -62,6 +63,18 @@ public static function any(string $uri, $handler): self
return static::add('GET|POST|DELETE|PUT|OPTIONS|PATCH ' . $uri, $handler);
}

public static function all(string $name, string $controller): self
{
return self::group(function() use ($name, $controller) {
self::get('/' . $name, 'index')->name('index');
self::post('/' . $name, 'store')->name('store');
self::match('PATCH|PUT', '/' . $name . '/{id:num}', 'update')->name('update');
self::get('/' . $name . '/{id:num}', 'show')->name('show');
self::get('/' . $name . '/{id:num}/edit', 'edit')->name('edit');
self::delete('/' . $name . '/{id:num}', 'delete')->name('delete');
})->byController($controller)->byName($name);
}

public static function match(string $methods, string $uri, $handler): self
{
return static::add($methods . ' ' . $uri, $handler);
Expand Down Expand Up @@ -192,20 +205,20 @@ private static function update(string $old, string $new)

public static function load()
{
$routes = Storage::path(config('storage.routes'))->getFiles();

foreach ($routes as $route) {
require_once config('storage.routes') . $route;
if (empty(config('routes.paths'))) {
throw new RoutesPathsNotDefinedException();
}

$paths = config('routes.paths');
$paths = array_map(function ($path) {
$path = Storage::path(config('storage.routes'))->addPath($path, '')->getPath();
return str_replace(['//', '//"'], ['/', '/"'], $path);
}, config('routes.paths'));

foreach ($paths as $path) {
$storage = Storage::path(config('storage.routes'))->addPath($path);
$custom_routes = $storage->getFiles();
$routes = Storage::path($path)->getFiles();

foreach ($custom_routes as $custom_route) {
require_once $storage->file($custom_route);
foreach ($routes as $route) {
require_once config('storage.routes') . $route;
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions core/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

namespace Core\Support;

use Carbon\Carbon;
use Core\Http\Request;
use Core\Support\Cookies;
use Core\Support\Session;
use Core\Support\Encryption;
use App\Database\Models\User;
use App\Database\Models\Token;
use Core\Http\Response\Response;

/**
* Manage authentications
Expand All @@ -25,16 +27,17 @@ public static function getAttempts()
return Session::get('auth_attempts', 0);
}

public static function attemptsExceeded()
{
return config('security.auth.max_attempts') > 0 && Auth::getAttempts() >= config('security.auth.max_attempts');
}

public static function attempt(array $credentials, bool $remember = false)
public static function attempt(Response $response, array $credentials, bool $remember = false)
{
Session::push('auth_attempts', 1, 0);

if (!self::checkCredentials($credentials['email'], $credentials['password'], $user)) return false;
if (!self::checkCredentials($credentials['email'], $credentials['password'], $user)) {
if (config('security.auth.max_attempts') > 0 && Auth::getAttempts() >= config('security.auth.max_attempts')) {
$response->redirect()->back()->with('auth_attempts_timeout', Carbon::now()->addMinutes(config('security.auth.unlock_timeout'))->toDateTimeString())->go();
}

return false;
}

Session::forget('auth_attempts', 'auth_attempts_timeout');
Session::create('user', $user);
Expand Down
11 changes: 4 additions & 7 deletions core/Support/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function getPath()
return self::$path;
}

public function addPath(string $path): self
public function addPath(string $path, string $trailling_slash = DS): self
{
self::$path .= real_path($path) . DS;
self::$path .= real_path($path) . $trailling_slash;
return $this;
}

Expand All @@ -50,7 +50,7 @@ public function createDir(string $pathname = '', bool $recursive = false, int $m
public function writeFile(string $filename, $content, bool $append = false)
{
if (!$this->isDir()) {
if (!$this->createDir('', true)) return false;
if (!$this->createDir(recursive: true)) return false;
}

$flag = $append ? FILE_APPEND | LOCK_EX : 0;
Expand Down Expand Up @@ -105,10 +105,7 @@ public function deleteDir(string $pathname = '')

foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (
$this->isDir($pathname . $object) &&
!is_link(self::$path . $pathname . $object)
) {
if ($this->isDir($pathname . $object) && !is_link(self::$path . $pathname . $object)) {
$this->deleteDir($pathname . $object);
} else {
$this->deleteFile($pathname . $object);
Expand Down
3 changes: 1 addition & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
require 'vendor/autoload.php';
require_once 'bootstrap.php';

$app = new Application();
$app->execute();
(new Application())->run();
2 changes: 1 addition & 1 deletion routes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Route::view('/forgot', 'auth.password.forgot');

Route::get('/new', function (Request $request, Response $response) {
$response->view('auth.password.new', ['email' => $request->queries('email')]);
$response->view('auth.password.new', $request->only('email'));
});
})->byPrefix('password')->register();

Expand Down

0 comments on commit 69b9eba

Please sign in to comment.