Refactor middleware

This commit is contained in:
Graham Campbell
2015-12-24 16:52:56 +00:00
parent fa235857b7
commit 445f5c662a
12 changed files with 47 additions and 47 deletions

View File

@@ -23,10 +23,26 @@ class Kernel extends HttpKernel
protected $middleware = [ protected $middleware = [
'Fideloper\Proxy\TrustProxies', 'Fideloper\Proxy\TrustProxies',
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', 'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', ];
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession', /**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken',
],
'api' => [
'CachetHQ\Cachet\Http\Middleware\Acceptable',
'CachetHQ\Cachet\Http\Middleware\Timezone',
],
]; ];
/** /**
@@ -35,19 +51,15 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected $routeMiddleware = [ protected $routeMiddleware = [
'accept' => 'CachetHQ\Cachet\Http\Middleware\Acceptable',
'admin' => 'CachetHQ\Cachet\Http\Middleware\Admin', 'admin' => 'CachetHQ\Cachet\Http\Middleware\Admin',
'app.hasSetting' => 'CachetHQ\Cachet\Http\Middleware\HasSetting', 'app.hasSetting' => 'CachetHQ\Cachet\Http\Middleware\HasSetting',
'app.isSetup' => 'CachetHQ\Cachet\Http\Middleware\AppIsSetup', 'app.isSetup' => 'CachetHQ\Cachet\Http\Middleware\AppIsSetup',
'app.subscribers' => 'CachetHQ\Cachet\Http\Middleware\SubscribersConfigured', 'app.subscribers' => 'CachetHQ\Cachet\Http\Middleware\SubscribersConfigured',
'auth' => 'CachetHQ\Cachet\Http\Middleware\Authenticate', 'auth' => 'CachetHQ\Cachet\Http\Middleware\Authenticate',
'auth.api' => 'CachetHQ\Cachet\Http\Middleware\ApiAuthenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'auth.api.optional' => 'CachetHQ\Cachet\Http\Middleware\ApiOptionalAuthenticate', 'auth.api.optional' => 'CachetHQ\Cachet\Http\Middleware\ApiOptionalAuthenticate',
'csrf' => 'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken', 'auth.api.required' => 'CachetHQ\Cachet\Http\Middleware\ApiAuthenticate',
'guest' => 'CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated', 'guest' => 'CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated',
'localize' => 'CachetHQ\Cachet\Http\Middleware\Localize', 'localize' => 'CachetHQ\Cachet\Http\Middleware\Localize',
'timezone' => 'CachetHQ\Cachet\Http\Middleware\Timezone',
'throttling' => 'GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware', 'throttling' => 'GrahamCampbell\Throttle\Http\Middleware\ThrottleMiddleware',
]; ];
} }

View File

@@ -21,13 +21,13 @@ class Acceptable
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $next * @param \Closure $next
* @param string $type * @param string|null $type
* *
* @return mixed * @return mixed
*/ */
public function handle($request, Closure $next, $type) public function handle($request, Closure $next, $type = null)
{ {
if (!$request->accepts($type)) { if (!$request->accepts($type ?: 'accept:application/json')) {
throw new NotAcceptableHttpException(); throw new NotAcceptableHttpException();
} }

View File

@@ -55,10 +55,6 @@ class ApiAuthenticate
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
throw new HttpException(401); throw new HttpException(401);
} }
} elseif ($request->getUser()) {
if ($this->auth->onceBasic() !== null) {
throw new HttpException(401);
}
} else { } else {
throw new HttpException(401); throw new HttpException(401);
} }

View File

@@ -54,10 +54,6 @@ class ApiOptionalAuthenticate
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
// //
} }
} elseif ($request->getUser()) {
if ($this->auth->onceBasic() !== null) {
//
}
} }
} }

View File

@@ -30,28 +30,25 @@ class ApiRoutes
$router->group([ $router->group([
'namespace' => 'Api', 'namespace' => 'Api',
'prefix' => 'api/v1', 'prefix' => 'api/v1',
'middleware' => ['accept:application/json', 'timezone', 'auth.api.optional'], 'middleware' => ['api'],
], function ($router) { ], function ($router) {
// General $router->group(['middleware' => ['auth.api.optional']], function ($router) {
$router->get('ping', 'GeneralController@ping'); $router->get('ping', 'GeneralController@ping');
// Components $router->get('components', 'ComponentController@getComponents');
$router->get('components', 'ComponentController@getComponents'); $router->get('components/groups', 'ComponentGroupController@getGroups');
$router->get('components/groups', 'ComponentGroupController@getGroups'); $router->get('components/groups/{component_group}', 'ComponentGroupController@getGroup');
$router->get('components/groups/{component_group}', 'ComponentGroupController@getGroup'); $router->get('components/{component}', 'ComponentController@getComponent');
$router->get('components/{component}', 'ComponentController@getComponent');
// Incidents $router->get('incidents', 'IncidentController@getIncidents');
$router->get('incidents', 'IncidentController@getIncidents'); $router->get('incidents/{incident}', 'IncidentController@getIncident');
$router->get('incidents/{incident}', 'IncidentController@getIncident');
// Metrics $router->get('metrics', 'MetricController@getMetrics');
$router->get('metrics', 'MetricController@getMetrics'); $router->get('metrics/{metric}', 'MetricController@getMetric');
$router->get('metrics/{metric}', 'MetricController@getMetric'); $router->get('metrics/{metric}/points', 'MetricController@getMetricPoints');
$router->get('metrics/{metric}/points', 'MetricController@getMetricPoints'); });
// Authorization Required $router->group(['middleware' => ['auth.api.required']], function ($router) {
$router->group(['middleware' => 'auth.api'], function ($router) {
$router->get('subscribers', 'SubscriberController@getSubscribers'); $router->get('subscribers', 'SubscriberController@getSubscribers');
$router->post('components', 'ComponentController@postComponents'); $router->post('components', 'ComponentController@postComponents');

View File

@@ -29,7 +29,7 @@ class AuthRoutes
{ {
$router->group([ $router->group([
'as' => 'auth.', 'as' => 'auth.',
'middleware' => 'app.hasSetting', 'middleware' => ['web', 'app.hasSetting'],
'prefix' => 'auth', 'prefix' => 'auth',
'setting' => 'app_name', 'setting' => 'app_name',
], function ($router) { ], function ($router) {
@@ -40,7 +40,7 @@ class AuthRoutes
]); ]);
$router->post('login', [ $router->post('login', [
'middleware' => ['guest', 'csrf', 'throttling:10,10'], 'middleware' => ['guest', 'throttling:10,10'],
'uses' => 'AuthController@postLogin', 'uses' => 'AuthController@postLogin',
]); ]);
@@ -51,7 +51,7 @@ class AuthRoutes
]); ]);
$router->post('2fa', [ $router->post('2fa', [
'middleware' => ['csrf', 'throttling:10,10'], 'middleware' => ['throttling:10,10'],
'uses' => 'AuthController@postTwoFactor', 'uses' => 'AuthController@postTwoFactor',
]); ]);

View File

@@ -28,7 +28,7 @@ class DashboardRoutes
public function map(Registrar $router) public function map(Registrar $router)
{ {
$router->group([ $router->group([
'middleware' => 'auth', 'middleware' => ['web', 'auth'],
'prefix' => 'dashboard', 'prefix' => 'dashboard',
'namespace' => 'Dashboard', 'namespace' => 'Dashboard',
'as' => 'dashboard.', 'as' => 'dashboard.',

View File

@@ -27,9 +27,8 @@ class FeedRoutes
*/ */
public function map(Registrar $router) public function map(Registrar $router)
{ {
// Prevent access until the app is setup.
$router->group([ $router->group([
'middleware' => 'app.hasSetting', 'middleware' => ['web', 'app.hasSetting'],
'setting' => 'app_name', 'setting' => 'app_name',
], function ($router) { ], function ($router) {
$router->get('/atom/{component_group?}', [ $router->get('/atom/{component_group?}', [

View File

@@ -27,7 +27,7 @@ class SetupRoutes
*/ */
public function map(Registrar $router) public function map(Registrar $router)
{ {
$router->group(['middleware' => ['app.isSetup', 'csrf']], function ($router) { $router->group(['middleware' => ['web', 'app.isSetup']], function ($router) {
$router->controller('setup', 'SetupController'); $router->controller('setup', 'SetupController');
}); });
} }

View File

@@ -28,7 +28,7 @@ class SignupRoutes
public function map(Registrar $router) public function map(Registrar $router)
{ {
$router->group([ $router->group([
'middleware' => ['app.hasSetting', 'guest'], 'middleware' => ['web', 'app.hasSetting', 'guest'],
'setting' => 'app_name', 'setting' => 'app_name',
'as' => 'signup.', 'as' => 'signup.',
], function ($router) { ], function ($router) {

View File

@@ -28,7 +28,7 @@ class StatusPageRoutes
public function map(Registrar $router) public function map(Registrar $router)
{ {
$router->group([ $router->group([
'middleware' => ['app.hasSetting', 'localize'], 'middleware' => ['web', 'app.hasSetting', 'localize'],
'setting' => 'app_name', 'setting' => 'app_name',
], function ($router) { ], function ($router) {
$router->get('/', [ $router->get('/', [

View File

@@ -28,7 +28,7 @@ class SubscribeRoutes
public function map(Registrar $router) public function map(Registrar $router)
{ {
$router->group([ $router->group([
'middleware' => ['app.hasSetting', 'localize'], 'middleware' => ['web', 'app.hasSetting', 'localize'],
'setting' => 'app_name', 'setting' => 'app_name',
'as' => 'subscribe.', 'as' => 'subscribe.',
], function ($router) { ], function ($router) {