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

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