X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/263384cf99864ebdb0408fd4e478f783aa487c1a..refs/pull/3693/head:/app/Http/Controllers/Auth/LoginController.php diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index d12d7c9bc..f1a1a8bd6 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -2,11 +2,11 @@ namespace BookStack\Http\Controllers\Auth; -use Activity; use BookStack\Auth\Access\LoginService; use BookStack\Auth\Access\SocialAuthService; use BookStack\Exceptions\LoginAttemptEmailNeededException; use BookStack\Exceptions\LoginAttemptException; +use BookStack\Facades\Activity; use BookStack\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; @@ -25,17 +25,16 @@ class LoginController extends Controller | */ - use AuthenticatesUsers; + use AuthenticatesUsers { logout as traitLogout; } /** * Redirection paths. */ protected $redirectTo = '/'; protected $redirectPath = '/'; - protected $redirectAfterLogout = '/login'; - protected $socialAuthService; - protected $loginService; + protected SocialAuthService $socialAuthService; + protected LoginService $loginService; /** * Create a new controller instance. @@ -50,7 +49,6 @@ class LoginController extends Controller $this->loginService = $loginService; $this->redirectPath = url('/'); - $this->redirectAfterLogout = url('/http/source.bookstackapp.com/login'); } public function username() @@ -73,6 +71,7 @@ class LoginController extends Controller { $socialDrivers = $this->socialAuthService->getActiveDrivers(); $authMethod = config('auth.method'); + $preventInitiation = $request->get('prevent_auto_init') === 'true'; if ($request->has('email')) { session()->flashInput([ @@ -84,6 +83,12 @@ class LoginController extends Controller // Store the previous location for redirect after login $this->updateIntendedFromPrevious(); + if (!$preventInitiation && $this->shouldAutoInitiate()) { + return view('auth.login-initiate', [ + 'authMethod' => $authMethod, + ]); + } + return view('auth.login', [ 'socialDrivers' => $socialDrivers, 'authMethod' => $authMethod, @@ -176,16 +181,16 @@ class LoginController extends Controller */ protected function validateLogin(Request $request) { - $rules = ['password' => 'required|string']; + $rules = ['password' => ['required', 'string']]; $authMethod = config('auth.method'); if ($authMethod === 'standard') { - $rules['email'] = 'required|email'; + $rules['email'] = ['required', 'email']; } if ($authMethod === 'ldap') { - $rules['username'] = 'required|string'; - $rules['email'] = 'email'; + $rules['username'] = ['required', 'string']; + $rules['email'] = ['email']; } $request->validate($rules); @@ -251,4 +256,32 @@ class LoginController extends Controller redirect()->setIntendedUrl($previous); } + + /** + * Check if login auto-initiate should be valid based upon authentication config. + */ + protected function shouldAutoInitiate(): bool + { + $socialDrivers = $this->socialAuthService->getActiveDrivers(); + $authMethod = config('auth.method'); + $autoRedirect = config('auth.auto_initiate'); + + return $autoRedirect && count($socialDrivers) === 0 && in_array($authMethod, ['oidc', 'saml2']); + } + + /** + * Logout user and perform subsequent redirect. + * + * @param \Illuminate\Http\Request $request + * + * @return mixed + */ + public function logout(Request $request) + { + $this->traitLogout($request); + + $redirectUri = $this->shouldAutoInitiate() ? '/login?prevent_auto_init=true' : '/'; + + return redirect($redirectUri); + } }