From b2cf7acfb614f241d7f54f43bc6b93eb439a8187 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Thu, 18 Jul 2019 10:51:05 -0400 Subject: [PATCH] Middleware to detect is update is pending; move Routes into HTTP --- Makefile | 7 +++ app/Console/Kernel.php | 2 +- .../Controllers/Admin/DashboardController.php | 11 ++-- app/Http/Kernel.php | 63 ++++++++++++------- app/Http/Middleware/UpdatePending.php | 39 ++++++++++++ app/{ => Http}/Routes/admin.php | 2 +- app/{ => Http}/Routes/api.php | 0 app/{ => Http}/Routes/console.php | 0 app/{ => Http}/Routes/web.php | 2 +- app/Providers/RouteServiceProvider.php | 4 +- config/laratrust.php | 48 +------------- 11 files changed, 98 insertions(+), 80 deletions(-) create mode 100644 app/Http/Middleware/UpdatePending.php rename app/{ => Http}/Routes/admin.php (98%) rename app/{ => Http}/Routes/api.php (100%) rename app/{ => Http}/Routes/console.php (100%) rename app/{ => Http}/Routes/web.php (97%) diff --git a/Makefile b/Makefile index 7eff61a2..99800900 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,13 @@ clean: clean-routes: @php artisan route:clear +.PHONY: clear +clear: + @php artisan cache:clear + @php artisan config:clear + @php artisan route:clear + @php artisan view:clear + .PHONY: build build: @php $(COMPOSER) install --no-interaction diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a7a88416..0750b910 100755 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -36,7 +36,7 @@ class Kernel extends ConsoleKernel */ protected function commands(): void { - require app_path('Routes/console.php'); + require app_path('Http/Routes/console.php'); $this->load(__DIR__.'/Commands'); $this->load(__DIR__.'/Cron'); } diff --git a/app/Http/Controllers/Admin/DashboardController.php b/app/Http/Controllers/Admin/DashboardController.php index 52390fda..bbaeac02 100644 --- a/app/Http/Controllers/Admin/DashboardController.php +++ b/app/Http/Controllers/Admin/DashboardController.php @@ -7,16 +7,13 @@ use App\Facades\Utils; use App\Repositories\NewsRepository; use App\Repositories\PirepRepository; use App\Repositories\UserRepository; -use Auth; -use Flash; use Illuminate\Http\Request; -use Log; -use Version; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Log; +use Laracasts\Flash\Flash; +use SebastianBergmann\Version; use vierbergenlars\SemVer\version as semver; -/** - * Class DashboardController - */ class DashboardController extends Controller { private $newsRepo; diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 3180a07d..3c49bf27 100755 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,41 +2,60 @@ namespace App\Http; +use App\Http\Middleware\ApiAuth; +use App\Http\Middleware\EncryptCookies; +use App\Http\Middleware\InstalledCheck; +use App\Http\Middleware\JsonResponse; +use App\Http\Middleware\RedirectIfAuthenticated; +use App\Http\Middleware\UpdatePending; +use App\Http\Middleware\VerifyCsrfToken; +use Illuminate\Auth\Middleware\Authenticate; +use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth; +use Illuminate\Auth\Middleware\Authorize; +use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse; use Illuminate\Foundation\Http\Kernel as HttpKernel; +use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode; +use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull; +use Illuminate\Foundation\Http\Middleware\TrimStrings; +use Illuminate\Routing\Middleware\SubstituteBindings; +use Illuminate\Routing\Middleware\ThrottleRequests; +use Illuminate\Session\Middleware\StartSession; +use Illuminate\View\Middleware\ShareErrorsFromSession; class Kernel extends HttpKernel { protected $middleware = [ - \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, - \Illuminate\Foundation\Http\Middleware\TrimStrings::class, - \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, + CheckForMaintenanceMode::class, + TrimStrings::class, + ConvertEmptyStringsToNull::class, ]; - protected $middlewareGroups = [ - 'web' => [ - \App\Http\Middleware\InstalledCheck::class, - \App\Http\Middleware\EncryptCookies::class, - \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, - \Illuminate\Session\Middleware\StartSession::class, - \Illuminate\View\Middleware\ShareErrorsFromSession::class, - \App\Http\Middleware\VerifyCsrfToken::class, - \Illuminate\Routing\Middleware\SubstituteBindings::class, - //\Spatie\Pjax\Middleware\FilterIfPjax::class, - ], + protected $middlewareGroups = [ 'api' => [ 'throttle:60,1', 'bindings', 'json', ], + 'web' => [ + InstalledCheck::class, + EncryptCookies::class, + AddQueuedCookiesToResponse::class, + StartSession::class, + ShareErrorsFromSession::class, + VerifyCsrfToken::class, + SubstituteBindings::class, + //\Spatie\Pjax\Middleware\FilterIfPjax::class, + ], ]; protected $routeMiddleware = [ - 'api.auth' => \App\Http\Middleware\ApiAuth::class, - 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, - 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, - 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, - 'can' => \Illuminate\Auth\Middleware\Authorize::class, - 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, - 'json' => \App\Http\Middleware\JsonResponse::class, - 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + 'api.auth' => ApiAuth::class, + 'auth' => Authenticate::class, + 'auth.basic' => AuthenticateWithBasicAuth::class, + 'bindings' => SubstituteBindings::class, + 'can' => Authorize::class, + 'guest' => RedirectIfAuthenticated::class, + 'json' => JsonResponse::class, + 'throttle' => ThrottleRequests::class, + 'update_pending' => UpdatePending::class, ]; } diff --git a/app/Http/Middleware/UpdatePending.php b/app/Http/Middleware/UpdatePending.php new file mode 100644 index 00000000..4d29f9db --- /dev/null +++ b/app/Http/Middleware/UpdatePending.php @@ -0,0 +1,39 @@ +migrationSvc = $migrationSvc; + } + + /** + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * + * @return mixed + */ + public function handle($request, Closure $next) + { + if (count($this->migrationSvc->migrationsAvailable()) > 0) { + return redirect('/update/step1'); + } + + return $next($request); + } +} diff --git a/app/Routes/admin.php b/app/Http/Routes/admin.php similarity index 98% rename from app/Routes/admin.php rename to app/Http/Routes/admin.php index b802fd03..766dbddf 100644 --- a/app/Routes/admin.php +++ b/app/Http/Routes/admin.php @@ -4,7 +4,7 @@ */ Route::group([ 'namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.', - 'middleware' => ['ability:admin,admin-access'], + 'middleware' => ['update_pending', 'ability:admin,admin-access'], ], static function () { // CRUD for airlines Route::resource('airlines', 'AirlinesController'); diff --git a/app/Routes/api.php b/app/Http/Routes/api.php similarity index 100% rename from app/Routes/api.php rename to app/Http/Routes/api.php diff --git a/app/Routes/console.php b/app/Http/Routes/console.php similarity index 100% rename from app/Routes/console.php rename to app/Http/Routes/console.php diff --git a/app/Routes/web.php b/app/Http/Routes/web.php similarity index 97% rename from app/Routes/web.php rename to app/Http/Routes/web.php index e55642f6..e7fc1f11 100755 --- a/app/Routes/web.php +++ b/app/Http/Routes/web.php @@ -47,4 +47,4 @@ Route::group([ Auth::routes(['verify' => true]); Route::get('/logout', 'Auth\LoginController@logout')->name('logout'); -require app_path('Routes/admin.php'); +require app_path('Http/Routes/admin.php'); diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 6f0382f9..8b952050 100755 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -40,7 +40,7 @@ class RouteServiceProvider extends ServiceProvider 'middleware' => 'web', 'namespace' => $this->namespace, ], function ($router) { - require app_path('Routes/web.php'); + require app_path('Http/Routes/web.php'); }); } @@ -59,7 +59,7 @@ class RouteServiceProvider extends ServiceProvider 'prefix' => 'api', 'as' => 'api.', ], function ($router) { - require app_path('Routes/api.php'); + require app_path('Http/Routes/api.php'); }); } } diff --git a/config/laratrust.php b/config/laratrust.php index f4049405..7b5b95bb 100644 --- a/config/laratrust.php +++ b/config/laratrust.php @@ -158,61 +158,17 @@ return [ ], - /* - |-------------------------------------------------------------------------- - | Laratrust Foreign Keys - |-------------------------------------------------------------------------- - | - | These are the foreign keys used by laratrust in the intermediate tables. - | - */ 'foreign_keys' => [ - /* - * User foreign key on Laratrust's role_user and permission_user tables. - */ 'user' => 'user_id', - - /* - * Role foreign key on Laratrust's role_user and permission_role tables. - */ 'role' => 'role_id', - - /* - * Role foreign key on Laratrust's permission_user and permission_role tables. - */ 'permission' => 'permission_id', - - /* - * Role foreign key on Laratrust's role_user and permission_user tables. - */ 'team' => 'team_id', - ], - /* - |-------------------------------------------------------------------------- - | Laratrust Middleware - |-------------------------------------------------------------------------- - | - | This configuration helps to customize the Laratrust middleware behavior. - | - */ 'middleware' => [ - /* - * Define if the laratrust middleware are registered automatically in the service provider - */ 'register' => true, - - /* - * Method to be called in the middleware return case. - * Available: abort|redirect - */ - 'handling' => 'abort', - - /* - * Parameter passed to the middleware_handling method - */ - 'params' => '403', + 'handling' => 'redirect', + 'params' => '/login', ],