From ccbf8fbabbcb66aea81d7bb6f1cabfbeafc32fa1 Mon Sep 17 00:00:00 2001 From: Joren Van Hee Date: Fri, 28 Apr 2017 16:15:34 +0200 Subject: [PATCH 01/15] Make it possible to switch guards --- src/app/Http/Controllers/AdminController.php | 5 +++- .../Auth/ForgotPasswordController.php | 14 ++++++++++- .../Http/Controllers/Auth/LoginController.php | 14 ++++++++++- .../Controllers/Auth/RegisterController.php | 14 ++++++++++- .../Auth/ResetPasswordController.php | 23 ++++++++++++++++++- src/config/backpack/base.php | 10 +++++++- 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/app/Http/Controllers/AdminController.php b/src/app/Http/Controllers/AdminController.php index 7ee790bd..aa63544e 100644 --- a/src/app/Http/Controllers/AdminController.php +++ b/src/app/Http/Controllers/AdminController.php @@ -11,7 +11,10 @@ class AdminController extends Controller */ public function __construct() { - $this->middleware('admin'); + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + $this->middleware("admin:$guard"); } /** diff --git a/src/app/Http/Controllers/Auth/ForgotPasswordController.php b/src/app/Http/Controllers/Auth/ForgotPasswordController.php index 65060e77..cce076c8 100644 --- a/src/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/src/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -4,6 +4,7 @@ use Backpack\Base\app\Http\Controllers\Controller; use Illuminate\Foundation\Auth\SendsPasswordResetEmails; +use Illuminate\Support\Facades\Password; class ForgotPasswordController extends Controller { @@ -29,7 +30,10 @@ class ForgotPasswordController extends Controller */ public function __construct() { - $this->middleware('guest'); + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + $this->middleware("guest:$guard"); } // ------------------------------------------------------- @@ -47,4 +51,12 @@ public function showLinkRequestForm() return view('backpack::auth.passwords.email', $this->data); } + + public function broker() + { + $passwords = config('backpack.base.passwords') + ?: config('auth.defaults.passwords'); + + return Password::broker($passwords); + } } diff --git a/src/app/Http/Controllers/Auth/LoginController.php b/src/app/Http/Controllers/Auth/LoginController.php index 9cde6abf..eac1d3cc 100644 --- a/src/app/Http/Controllers/Auth/LoginController.php +++ b/src/app/Http/Controllers/Auth/LoginController.php @@ -5,6 +5,7 @@ use Backpack\Base\app\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; +use Auth; class LoginController extends Controller { @@ -31,7 +32,10 @@ class LoginController extends Controller */ public function __construct() { - $this->middleware('guest', ['except' => 'logout']); + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + $this->middleware("guest:$guard", ['except' => 'logout']); // ---------------------------------- // Use the admin prefix in all routes @@ -81,4 +85,12 @@ public function logout(Request $request) // And redirect to custom location return redirect($this->redirectAfterLogout); } + + protected function guard() + { + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + return Auth::guard($guard); + } } diff --git a/src/app/Http/Controllers/Auth/RegisterController.php b/src/app/Http/Controllers/Auth/RegisterController.php index 9836bca4..18ece068 100644 --- a/src/app/Http/Controllers/Auth/RegisterController.php +++ b/src/app/Http/Controllers/Auth/RegisterController.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Http\Request; use Validator; +use Auth; class RegisterController extends Controller { @@ -30,7 +31,10 @@ class RegisterController extends Controller */ public function __construct() { - $this->middleware('guest'); + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + $this->middleware("guest:$guard"); // Where to redirect users after login / registration. $this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo @@ -113,4 +117,12 @@ public function register(Request $request) return redirect($this->redirectPath()); } + + protected function guard() + { + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + return Auth::guard($guard); + } } diff --git a/src/app/Http/Controllers/Auth/ResetPasswordController.php b/src/app/Http/Controllers/Auth/ResetPasswordController.php index efeb8ee0..7713c3f4 100644 --- a/src/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/src/app/Http/Controllers/Auth/ResetPasswordController.php @@ -5,6 +5,8 @@ use Backpack\Base\app\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Password; +use Auth; class ResetPasswordController extends Controller { @@ -30,7 +32,10 @@ class ResetPasswordController extends Controller */ public function __construct() { - $this->middleware('guest'); + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + $this->middleware("guest:$guard"); // where to redirect after password was reset $this->redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : config('backpack.base.route_prefix', 'admin').'/dashboard'; @@ -58,4 +63,20 @@ public function showResetForm(Request $request, $token = null) ['token' => $token, 'email' => $request->email] ); } + + public function broker() + { + $passwords = config('backpack.base.passwords') + ?: config('auth.defaults.passwords'); + + return Password::broker($passwords); + } + + protected function guard() + { + $guard = config('backpack.base.guard') + ?: config('auth.defaults.guard'); + + return Auth::guard($guard); + } } diff --git a/src/config/backpack/base.php b/src/config/backpack/base.php index 82042a62..9d72142d 100644 --- a/src/config/backpack/base.php +++ b/src/config/backpack/base.php @@ -68,11 +68,19 @@ /* |-------------------------------------------------------------------------- - | User Model + | Authentication |-------------------------------------------------------------------------- */ // Fully qualified namespace of the User model 'user_model_fqn' => '\App\User', + // The guard that protects the Backpack admin panel. The default guard will + // be used if this is null. + 'guard' => null, + + // The password reset configuration for Backpack. The default configuration + // will be used if this is null. + 'passwords' => null, + ]; From a592571dd1ec798e9f48ec0062a6375fe88d51f3 Mon Sep 17 00:00:00 2001 From: Joren Van Hee Date: Tue, 2 May 2017 10:55:17 +0200 Subject: [PATCH 02/15] Use correct guard in views --- src/BaseServiceProvider.php | 3 +++ src/app/Http/ViewComposers/AuthComposer.php | 19 +++++++++++++++++++ src/resources/views/inc/menu.blade.php | 2 +- src/resources/views/inc/sidebar.blade.php | 6 +++--- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/app/Http/ViewComposers/AuthComposer.php diff --git a/src/BaseServiceProvider.php b/src/BaseServiceProvider.php index 83cf0c1b..c42f85e7 100644 --- a/src/BaseServiceProvider.php +++ b/src/BaseServiceProvider.php @@ -3,6 +3,7 @@ namespace Backpack\Base; use Illuminate\Routing\Router; +use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use Route; @@ -28,6 +29,8 @@ public function boot(\Illuminate\Routing\Router $router) // - then the stock views that come with the package, in case a published view might be missing $this->loadViewsFrom(realpath(__DIR__.'/resources/views'), 'backpack'); + View::composer('backpack::*', \Backpack\Base\app\Http\ViewComposers\AuthComposer::class); + $this->loadTranslationsFrom(realpath(__DIR__.'/resources/lang'), 'backpack'); // use the vendor configuration file as fallback diff --git a/src/app/Http/ViewComposers/AuthComposer.php b/src/app/Http/ViewComposers/AuthComposer.php new file mode 100644 index 00000000..0143be6e --- /dev/null +++ b/src/app/Http/ViewComposers/AuthComposer.php @@ -0,0 +1,19 @@ +with([ + 'backpackAuth' => Auth::guard($guard), + ]); + } +} diff --git a/src/resources/views/inc/menu.blade.php b/src/resources/views/inc/menu.blade.php index 0b47cbdf..41516792 100644 --- a/src/resources/views/inc/menu.blade.php +++ b/src/resources/views/inc/menu.blade.php @@ -19,7 +19,7 @@ - @if (Auth::guest()) + @if ($backpackAuth->guest())
  • {{ trans('backpack::base.login') }}
  • @if (config('backpack.base.registration_open'))
  • {{ trans('backpack::base.register') }}
  • diff --git a/src/resources/views/inc/sidebar.blade.php b/src/resources/views/inc/sidebar.blade.php index a6a7fd22..ff8f8eb4 100644 --- a/src/resources/views/inc/sidebar.blade.php +++ b/src/resources/views/inc/sidebar.blade.php @@ -1,4 +1,4 @@ -@if (Auth::check()) +@if ($backpackAuth->check())