From 90443cf0e88ff444af7ade64e6a12a057f7239b4 Mon Sep 17 00:00:00 2001 From: Christophe LATOUR Date: Tue, 21 Nov 2017 12:11:23 +0100 Subject: [PATCH] Adding version checker middleware --- app/Exceptions/Handler.php | 15 +- app/Exceptions/VersionException.php | 8 ++ app/Web/Middlewares/Version.php | 28 ++++ bootstrap/app.php | 3 +- routes/web.php | 210 ++++++++++++++-------------- 5 files changed, 158 insertions(+), 106 deletions(-) create mode 100644 app/Exceptions/VersionException.php create mode 100644 app/Web/Middlewares/Version.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e5e74cc..1ba38d3 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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); } diff --git a/app/Exceptions/VersionException.php b/app/Exceptions/VersionException.php new file mode 100644 index 0000000..647168b --- /dev/null +++ b/app/Exceptions/VersionException.php @@ -0,0 +1,8 @@ +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); + } +} \ No newline at end of file diff --git a/bootstrap/app.php b/bootstrap/app.php index 6e53141..95d37c8 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -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, ]); /* diff --git a/routes/web.php b/routes/web.php index 98449d2..21c5e31 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'); + }); }); \ No newline at end of file