Adding version checker middleware

This commit is contained in:
Christophe LATOUR 2017-11-21 12:11:23 +01:00
parent 74ca447bbf
commit 90443cf0e8
5 changed files with 158 additions and 106 deletions

View File

@ -3,6 +3,7 @@
namespace App\Exceptions;
use Exception;
use App\Exceptions\VersionException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -11,6 +12,8 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
class Handler extends ExceptionHandler
{
const ERROR_VERSION = 0;
/**
* A list of the exception types that should not be reported.
*
@ -49,15 +52,25 @@ class Handler extends ExceptionHandler
return parent::render($request, $e);
} else {
$statusCode = 500;
$customCode = 0;
if ($e instanceof ModelNotFoundException) {
$statusCode = 404;
} elseif ($e instanceof HttpException) {
$statusCode = $e->getStatusCode();
} elseif ($e instanceof VersionException) {
$statusCode = 400;
$customCode = self::ERROR_VERSION;
}
if ($statusCode <= 200) {
$statusCode = 500;
}
return response()->json(array(
'errors' => array(
$e->getMessage(),
)
),
'code' => $customCode,
), $statusCode);
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Exceptions;
use Symfony\Component\HttpKernel\Exception\HttpException;
class VersionException extends HttpException {
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Web\Middlewares;
use Closure;
use App\Exceptions\VersionException;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class Version {
const REGEX_VERSION = '/^([\d])+.([\d])+.([\d]+)$/';
public function handle(Request $request, Closure $next) {
$version = $request->headers->get('x-device-version');
if (preg_match(self::REGEX_VERSION, $version, $matches) === 1) {
list($match, $major, $medium, $minor) = $matches;
if ($major < env('MINIMUM_MAJOR') || $medium < env('MINIMUM_MEDIUM') || $minor < env('MINIMUM_MINOR')) {
throw new VersionException(400, "");
}
} else {
throw new VersionException(400, "");
}
return $next($request);
}
}

View File

@ -82,6 +82,7 @@ $app->singleton(
$app->routeMiddleware([
'auth' => App\Web\Middlewares\Authenticate::class,
'version' => App\Web\Middlewares\Version::class,
]);
/*

View File

@ -34,6 +34,7 @@ $app->get('/cms/{id_cms}', 'CmsController@get');
$app->get('/countries', 'CountryController@lists');
$app->group(['middleware' => 'auth'], function() use ($app) {
$app->group(['middleware' => 'version'], function() use ($app) {
/*
|--------------------------------------------------------------------------
| AUTH ROUTES
@ -161,4 +162,5 @@ $app->group(['middleware' => 'auth'], function() use ($app) {
$app->get('/user/accounts', 'AccountPaymentController@list_accounts');
$app->delete('/user/account/paypal/{id_paypal}', 'AccountPaymentController@delete_paypal');
$app->delete('/user/account/paybox/{id_paybox_card}', 'AccountPaymentController@delete_paybox');
});
});