3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\LdapService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Exceptions\AuthException;
9 use BookStack\Http\Controllers\Controller;
10 use Illuminate\Contracts\Auth\Authenticatable;
11 use Illuminate\Foundation\Auth\AuthenticatesUsers;
12 use Illuminate\Http\Request;
14 class LoginController extends Controller
17 |--------------------------------------------------------------------------
19 |--------------------------------------------------------------------------
21 | This controller handles authenticating users for the application and
22 | redirecting them to your home screen. The controller uses a trait
23 | to conveniently provide its functionality to your applications.
27 use AuthenticatesUsers;
30 * Where to redirect users after login.
34 protected $redirectTo = '/';
36 protected $redirectPath = '/';
37 protected $redirectAfterLogout = '/login';
39 protected $socialAuthService;
40 protected $ldapService;
44 * Create a new controller instance.
46 * @param \BookStack\Auth\\BookStack\Auth\Access\SocialAuthService $socialAuthService
47 * @param LdapService $ldapService
48 * @param \BookStack\Auth\UserRepo $userRepo
50 public function __construct(SocialAuthService $socialAuthService, LdapService $ldapService, UserRepo $userRepo)
52 $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
53 $this->socialAuthService = $socialAuthService;
54 $this->ldapService = $ldapService;
55 $this->userRepo = $userRepo;
56 $this->redirectPath = url('/');
57 $this->redirectAfterLogout = url('/login');
58 parent::__construct();
61 public function username()
63 return config('auth.method') === 'standard' ? 'email' : 'username';
67 * Overrides the action when a user is authenticated.
68 * If the user authenticated but does not exist in the user table we create them.
69 * @throws AuthException
70 * @throws \BookStack\Exceptions\LdapException
72 protected function authenticated(Request $request, Authenticatable $user)
74 // Explicitly log them out for now if they do no exist.
76 auth()->logout($user);
79 if (!$user->exists && $user->email === null && !$request->filled('email')) {
81 session()->flash('request-email', true);
82 return redirect('/login');
85 if (!$user->exists && $user->email === null && $request->filled('email')) {
86 $user->email = $request->get('email');
90 // Check for users with same email already
91 $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
93 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
97 $this->userRepo->attachDefaultRole($user);
98 $this->userRepo->downloadAndAssignUserAvatar($user);
102 // Sync LDAP groups if required
103 if ($this->ldapService->shouldSyncGroups()) {
104 $this->ldapService->syncGroups($user, $request->get($this->username()));
107 return redirect()->intended('/');
111 * Show the application login form.
113 public function getLogin(Request $request)
115 $socialDrivers = $this->socialAuthService->getActiveDrivers();
116 $authMethod = config('auth.method');
117 $samlEnabled = config('saml2.enabled') === true;
119 if ($request->has('email')) {
120 session()->flashInput([
121 'email' => $request->get('email'),
122 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
126 return view('auth.login', [
127 'socialDrivers' => $socialDrivers,
128 'authMethod' => $authMethod,
129 'samlEnabled' => $samlEnabled,
134 * Log the user out of the application.
136 public function logout(Request $request)
138 if (config('saml2.enabled') && session()->get('last_login_type') === 'saml2') {
139 return redirect('/saml2/logout');
142 $this->guard()->logout();
143 $request->session()->invalidate();
145 return $this->loggedOut($request) ?: redirect('/');