diff --git a/src/BaseServiceProvider.php b/src/BaseServiceProvider.php index 5c4a5552..08e59397 100644 --- a/src/BaseServiceProvider.php +++ b/src/BaseServiceProvider.php @@ -2,6 +2,7 @@ namespace Backpack\Base; +use Config; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; use Route; @@ -33,20 +34,24 @@ class BaseServiceProvider extends ServiceProvider */ public function boot(\Illuminate\Routing\Router $router) { + // ------------- // LOAD THE VIEWS - // - first the published views (in case they have any changes) + // ------------- + // first the published views (in case they have any changes) $this->loadViewsFrom(resource_path('views/vendor/backpack/base'), 'backpack'); - // - then the stock views that come with the package, in case a published view might be missing + // then the stock views that come with the package, in case a published view might be missing $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack'); $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack'); // use the vendor configuration file as fallback $this->mergeConfigFrom( - __DIR__.'/config/backpack/base.php', 'backpack.base' + __DIR__.'/config/backpack/base.php', + 'backpack.base' ); - $this->registerAdminMiddleware($this->app->router); + $this->registerGuards(); + $this->registerMiddleware($this->app->router); $this->setupRoutes($this->app->router); $this->publishFiles(); $this->loadHelpers(); @@ -80,6 +85,18 @@ public function setupRoutes(Router $router) $this->loadRoutesFrom($routeFilePathInUse); } + /** + * Register custom backpack authentication guard. + */ + public function registerGuards() + { + $backpackAuthGuard = Config::get('backpack.base.admin_guard'); + $existingGuards = Config::get('auth.guards'); + $existingGuards[$backpackAuthGuard['name']] = $backpackAuthGuard; + + Config::set('auth.guards', $existingGuards); + } + /** * Register any package services. * @@ -117,9 +134,13 @@ public function register() $this->commands($this->commands); } - public function registerAdminMiddleware(Router $router) + public function registerMiddleware(Router $router) { - Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class); + Route::aliasMiddleware('backpack.auth', \Backpack\Base\app\Http\Middleware\BackpackAuth::class); + + if (config('backpack.base.separate_admin_session')) { + Route::aliasMiddleware('backpack.auth.guard', \Backpack\Base\app\Http\Middleware\BackpackAuthGuard::class); + } } public function publishFiles() diff --git a/src/app/Http/Controllers/AdminController.php b/src/app/Http/Controllers/AdminController.php index 7ee790bd..940970ff 100644 --- a/src/app/Http/Controllers/AdminController.php +++ b/src/app/Http/Controllers/AdminController.php @@ -11,7 +11,7 @@ class AdminController extends Controller */ public function __construct() { - $this->middleware('admin'); + $this->middleware(backpack_middleware()); } /** diff --git a/src/app/Http/Controllers/Auth/ForgotPasswordController.php b/src/app/Http/Controllers/Auth/ForgotPasswordController.php index 65060e77..b0f0cb49 100644 --- a/src/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/src/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -29,7 +29,12 @@ class ForgotPasswordController extends Controller */ public function __construct() { - $this->middleware('guest'); + $this->middleware(backpack_middleware('guest')); + } + + public function guard() + { + return \Auth::guard(backpack_guard()); } // ------------------------------------------------------- diff --git a/src/app/Http/Controllers/Auth/LoginController.php b/src/app/Http/Controllers/Auth/LoginController.php index 9cde6abf..c38d63ea 100644 --- a/src/app/Http/Controllers/Auth/LoginController.php +++ b/src/app/Http/Controllers/Auth/LoginController.php @@ -31,7 +31,7 @@ class LoginController extends Controller */ public function __construct() { - $this->middleware('guest', ['except' => 'logout']); + $this->middleware(backpack_middleware('guest'), ['except' => 'logout']); // ---------------------------------- // Use the admin prefix in all routes @@ -50,6 +50,11 @@ public function __construct() // ---------------------------------- } + public function guard() + { + return \Auth::guard(backpack_guard()); + } + // ------------------------------------------------------- // Laravel overwrites for loading backpack views // ------------------------------------------------------- diff --git a/src/app/Http/Controllers/Auth/MyAccountController.php b/src/app/Http/Controllers/Auth/MyAccountController.php index af08292d..566e5cd0 100644 --- a/src/app/Http/Controllers/Auth/MyAccountController.php +++ b/src/app/Http/Controllers/Auth/MyAccountController.php @@ -15,7 +15,7 @@ class MyAccountController extends Controller public function __construct() { - $this->middleware('admin'); + $this->middleware(backpack_auth()); } /** diff --git a/src/app/Http/Controllers/Auth/RegisterController.php b/src/app/Http/Controllers/Auth/RegisterController.php index 9836bca4..b45ea992 100644 --- a/src/app/Http/Controllers/Auth/RegisterController.php +++ b/src/app/Http/Controllers/Auth/RegisterController.php @@ -30,13 +30,18 @@ class RegisterController extends Controller */ public function __construct() { - $this->middleware('guest'); + $this->middleware(backpack_middleware('guest')); // Where to redirect users after login / registration. $this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'dashboard'); } + public function guard() + { + return \Auth::guard(backpack_guard()); + } + /** * Get a validator for an incoming registration request. * diff --git a/src/app/Http/Controllers/Auth/ResetPasswordController.php b/src/app/Http/Controllers/Auth/ResetPasswordController.php index efeb8ee0..2838d9b1 100644 --- a/src/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/src/app/Http/Controllers/Auth/ResetPasswordController.php @@ -30,12 +30,17 @@ class ResetPasswordController extends Controller */ public function __construct() { - $this->middleware('guest'); + $this->middleware(backpack_middleware('guest'), ['except' => 'logout']); // where to redirect after password was reset $this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'admin').'/dashboard'; } + public function guard() + { + return \Auth::guard(backpack_guard()); + } + // ------------------------------------------------------- // Laravel overwrites for loading backpack views // ------------------------------------------------------- diff --git a/src/app/Http/Middleware/Admin.php b/src/app/Http/Middleware/BackpackAuth.php similarity index 97% rename from src/app/Http/Middleware/Admin.php rename to src/app/Http/Middleware/BackpackAuth.php index 8a622c15..6f340e15 100644 --- a/src/app/Http/Middleware/Admin.php +++ b/src/app/Http/Middleware/BackpackAuth.php @@ -5,7 +5,7 @@ use Closure; use Illuminate\Support\Facades\Auth; -class Admin +class BackpackAuth { /** * Handle an incoming request. diff --git a/src/app/Http/Middleware/BackpackAuthGuard.php b/src/app/Http/Middleware/BackpackAuthGuard.php new file mode 100644 index 00000000..c49a014e --- /dev/null +++ b/src/app/Http/Middleware/BackpackAuthGuard.php @@ -0,0 +1,31 @@ +check()) { + if ($request->ajax() || $request->wantsJson()) { + return response(trans('backpack::base.unauthorized'), 401); + } + + return redirect(config('backpack.base.route_prefix').'/login'); + } + } + + return $next($request); + } +} diff --git a/src/config/backpack/base.php b/src/config/backpack/base.php index bff9fee8..82ba518b 100644 --- a/src/config/backpack/base.php +++ b/src/config/backpack/base.php @@ -74,13 +74,21 @@ /* |-------------------------------------------------------------------------- - | User Model + | Authentication |-------------------------------------------------------------------------- */ // Fully qualified namespace of the User model 'user_model_fqn' => '\App\User', + 'separate_admin_session' => false, + + 'admin_guard' => [ + 'name' => 'admin', + 'driver' => 'session', + 'provider' => 'users', + ], + // What kind of avatar will you like to show to the user? // Default: gravatar (automatically use the gravatar for his email) // Other options: diff --git a/src/helpers.php b/src/helpers.php index 0444e472..ab344cf1 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -42,3 +42,70 @@ function backpack_avatar_url($user) } } } + +/* + * Returns the name of the middleware + * defined by the application config + * if a param is passed in, it will chain + * the backpack middelware to it. + * e.g guest:backpack.admin. + */ +if (!function_exists('backpack_middleware')) { + function backpack_middleware($chainedWith = null) + { + if (config('backpack.base.separate_admin_session')) { + $middleware = config('backpack.base.admin_guard.name'); + } else { + $middleware = 'backpack.auth'; + } + + if ($chainedWith && config('backpack.base.separate_admin_session')) { + $middleware = $chainedWith.':'.$middleware; + } elseif ($chainedWith) { + $middleware = $chainedWith; + } + + return $middleware; + } +} + +/* + * Returns the name of the guard defined + * by the application config + */ +if (!function_exists('backpack_guard')) { + function backpack_guard() + { + if (config('backpack.base.separate_admin_session')) { + $guard = config('backpack.base.admin_guard.name'); + } else { + $guard = null; + } + + return $guard; + } +} + +/* + * Returns the user instance if it exists + * of the currently authenticated admin + * based off the defined guard. + */ +if (!function_exists('backpack_auth')) { + function backpack_auth() + { + return \Auth::guard(backpack_guard())->user(); + } +} + +/* + * Returns back a user instance without + * the admin guard, however allows you + * to pass in a custom guard if you like. + */ +if (!function_exists('backpack_user')) { + function backpack_user($guard = null) + { + return \Auth::guard($guard)->user(); + } +} diff --git a/src/resources/views/inc/menu.blade.php b/src/resources/views/inc/menu.blade.php index 1639c6a7..ce92787a 100644 --- a/src/resources/views/inc/menu.blade.php +++ b/src/resources/views/inc/menu.blade.php @@ -19,7 +19,7 @@ @if (config('backpack.base.setup_auth_routes')) - @if (Auth::guest()) + @if (!backpack_auth())