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

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

View File

@ -34,131 +34,133 @@ $app->get('/cms/{id_cms}', 'CmsController@get');
$app->get('/countries', 'CountryController@lists'); $app->get('/countries', 'CountryController@lists');
$app->group(['middleware' => 'auth'], function() use ($app) { $app->group(['middleware' => 'auth'], function() use ($app) {
/* $app->group(['middleware' => 'version'], function() use ($app) {
|-------------------------------------------------------------------------- /*
| AUTH ROUTES |--------------------------------------------------------------------------
|-------------------------------------------------------------------------- | AUTH ROUTES
*/ |--------------------------------------------------------------------------
$app->post('/signin', 'AuthController@signin'); */
$app->post('/signin', 'AuthController@signin');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| SALE ROUTES | SALE ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/sales', 'SaleController@lists'); $app->get('/sales', 'SaleController@lists');
$app->get('/tags', 'TagController@lists'); $app->get('/tags', 'TagController@lists');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| CATEGORY ROUTES | CATEGORY ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/category/{id_category}', 'CategoryController@get'); $app->get('/category/{id_category}', 'CategoryController@get');
$app->get('/category/{id_category}/products', 'CategoryController@list_products'); $app->get('/category/{id_category}/products', 'CategoryController@list_products');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| PRODUCT ROUTES | PRODUCT ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/product/{id_product}', 'ProductController@get'); $app->get('/product/{id_product}', 'ProductController@get');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| CART ROUTES | CART ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/cart', 'CartController@get'); $app->get('/cart', 'CartController@get');
$app->get('/cart/validate', 'CartController@validateCart'); $app->get('/cart/validate', 'CartController@validateCart');
/* CART CARRIERS */ /* CART CARRIERS */
$app->get('/cart/carriers', 'CartController@getCarriers'); $app->get('/cart/carriers', 'CartController@getCarriers');
$app->put('/cart/carrier', 'CartController@setCarrier'); $app->put('/cart/carrier', 'CartController@setCarrier');
$app->post('/cart/carrier/socol', 'CartController@setSocolInfos'); $app->post('/cart/carrier/socol', 'CartController@setSocolInfos');
/* CART PRODUCTS */ /* CART PRODUCTS */
$app->put('/cart/product', 'CartController@addProduct'); $app->put('/cart/product', 'CartController@addProduct');
$app->delete('/cart/product', 'CartController@removeProduct'); $app->delete('/cart/product', 'CartController@removeProduct');
/* CART DISCOUNTS */ /* CART DISCOUNTS */
$app->put('/cart/discount', 'CartController@addDiscount'); $app->put('/cart/discount', 'CartController@addDiscount');
$app->get('/cart/discounts', 'CartController@getAvailableDiscounts'); $app->get('/cart/discounts', 'CartController@getAvailableDiscounts');
$app->delete('/cart/discount', 'CartController@removeDiscount'); $app->delete('/cart/discount', 'CartController@removeDiscount');
/* CART ADDRESS */ /* CART ADDRESS */
$app->put('/cart/address', 'CartController@setAddress'); $app->put('/cart/address', 'CartController@setAddress');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| PAYMENT ROUTES | PAYMENT ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->post('/payment/cheque', 'Payments\\ChequeController@execPayment'); $app->post('/payment/cheque', 'Payments\\ChequeController@execPayment');
$app->get('/payment/paybox', 'Payments\\PayboxController@get'); $app->get('/payment/paybox', 'Payments\\PayboxController@get');
$app->get('/payment/paybox/numquestion', 'Payments\\PayboxController@getQuestion'); $app->get('/payment/paybox/numquestion', 'Payments\\PayboxController@getQuestion');
$app->post('/payment/paybox/{type:normal|card}', 'Payments\\PayboxController@validateOrder'); $app->post('/payment/paybox/{type:normal|card}', 'Payments\\PayboxController@validateOrder');
$app->get('/payment/paypal', 'Payments\\PaypalController@getUrl'); $app->get('/payment/paypal', 'Payments\\PaypalController@getUrl');
$app->post('/payment/paypal', 'Payments\\PaypalController@execPayement'); $app->post('/payment/paypal', 'Payments\\PaypalController@execPayement');
// $app->post('/payment/paybox/{type:normal|save_card}', 'Payments\\PayboxController@execPayment'); // $app->post('/payment/paybox/{type:normal|save_card}', 'Payments\\PayboxController@execPayment');
// $app->post('/payment/paybox/card', 'Payments\\PayboxController@execPaymentWithSavedCard'); // $app->post('/payment/paybox/card', 'Payments\\PayboxController@execPaymentWithSavedCard');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| CONTACT ROUTES | CONTACT ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/contacts', 'ContactController@lists'); $app->get('/contacts', 'ContactController@lists');
$app->post('/contact', 'ContactController@create'); $app->post('/contact', 'ContactController@create');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| SPONSOR | SPONSOR
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/sponsors', 'SponsorController@lists'); $app->get('/sponsors', 'SponsorController@lists');
$app->post('/sponsors', 'SponsorController@invite'); $app->post('/sponsors', 'SponsorController@invite');
$app->post('/sponsor/{id_invite}/revive', 'SponsorController@revive'); $app->post('/sponsor/{id_invite}/revive', 'SponsorController@revive');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| RELAYS | RELAYS
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/relays/socolissimo/{type:office|pickup}', 'Relays\\SocolissimoController@lists'); $app->get('/relays/socolissimo/{type:office|pickup}', 'Relays\\SocolissimoController@lists');
$app->put('/relays/socolissimo/{type:office|pickup}/address', 'Relays\\SocolissimoController@setAddressRelay'); $app->put('/relays/socolissimo/{type:office|pickup}/address', 'Relays\\SocolissimoController@setAddressRelay');
$app->get('/relays/mondialrelay', 'Relays\\MondialRelayController@lists'); $app->get('/relays/mondialrelay', 'Relays\\MondialRelayController@lists');
$app->put('/relays/mondialrelay/address', 'Relays\\MondialRelayController@setAddressRelay'); $app->put('/relays/mondialrelay/address', 'Relays\\MondialRelayController@setAddressRelay');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| USER ROUTES | USER ROUTES
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
$app->get('/user', 'UserController@get'); $app->get('/user', 'UserController@get');
$app->put('/user', 'UserController@update'); $app->put('/user', 'UserController@update');
$app->put('/user/newsletter', 'UserController@updateNewsletter'); $app->put('/user/newsletter', 'UserController@updateNewsletter');
$app->get('/user/discounts', 'DiscountController@lists'); $app->get('/user/discounts', 'DiscountController@lists');
$app->get('/user/discount/{id_discount}', 'DiscountController@get'); $app->get('/user/discount/{id_discount}', 'DiscountController@get');
$app->get('/user/orders', 'OrderController@lists'); $app->get('/user/orders', 'OrderController@lists');
$app->get('/user/refunds', 'OrderController@list_refunds'); $app->get('/user/refunds', 'OrderController@list_refunds');
$app->get('/user/order/{id_order}', 'OrderController@get'); $app->get('/user/order/{id_order}', 'OrderController@get');
$app->get('/user/addresses', 'AddressController@lists'); $app->get('/user/addresses', 'AddressController@lists');
$app->get('/user/address/{id_address}', 'AddressController@get'); $app->get('/user/address/{id_address}', 'AddressController@get');
$app->put('/user/address/{id_address}', 'AddressController@update'); $app->put('/user/address/{id_address}', 'AddressController@update');
$app->post('/user/address', 'AddressController@create'); $app->post('/user/address', 'AddressController@create');
$app->delete('/user/address/{id_address}', 'AddressController@delete'); $app->delete('/user/address/{id_address}', 'AddressController@delete');
$app->get('/user/accounts', 'AccountPaymentController@list_accounts'); $app->get('/user/accounts', 'AccountPaymentController@list_accounts');
$app->delete('/user/account/paypal/{id_paypal}', 'AccountPaymentController@delete_paypal'); $app->delete('/user/account/paypal/{id_paypal}', 'AccountPaymentController@delete_paypal');
$app->delete('/user/account/paybox/{id_paybox_card}', 'AccountPaymentController@delete_paybox'); $app->delete('/user/account/paybox/{id_paybox_card}', 'AccountPaymentController@delete_paybox');
});
}); });