3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Exceptions\AuthException;
6 use BookStack\Http\Controllers\Controller;
7 use BookStack\Repos\UserRepo;
8 use BookStack\Repos\LdapRepo;
9 use BookStack\Services\LdapService;
10 use BookStack\Services\SocialAuthService;
11 use Illuminate\Contracts\Auth\Authenticatable;
12 use Illuminate\Foundation\Auth\AuthenticatesUsers;
13 use Illuminate\Http\Request;
15 class LoginController extends Controller
18 |--------------------------------------------------------------------------
20 |--------------------------------------------------------------------------
22 | This controller handles authenticating users for the application and
23 | redirecting them to your home screen. The controller uses a trait
24 | to conveniently provide its functionality to your applications.
28 use AuthenticatesUsers;
31 * Where to redirect users after login.
35 protected $redirectTo = '/';
37 protected $redirectPath = '/';
38 protected $redirectAfterLogout = '/login';
40 protected $socialAuthService;
44 * Create a new controller instance.
46 * @param SocialAuthService $socialAuthService
47 * @param UserRepo $userRepo
49 public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
51 $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
52 $this->socialAuthService = $socialAuthService;
53 $this->userRepo = $userRepo;
54 $this->redirectPath = baseUrl('/');
55 $this->redirectAfterLogout = baseUrl('/login');
56 parent::__construct();
59 public function username()
61 return config('auth.method') === 'standard' ? 'email' : 'username';
65 * Overrides the action when a user is authenticated.
66 * If the user authenticated but does not exist in the user table we create them.
67 * @param Request $request
68 * @param Authenticatable $user
69 * @return \Illuminate\Http\RedirectResponse
70 * @throws AuthException
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);
101 // ldap groups refresh
102 if (config('services.ldap.user_to_groups') !== false && $request->filled('username')) {
103 $ldapRepo = new LdapRepo($this->userRepo, app(LdapService::class));
104 $ldapRepo->syncGroups($user, $request->input('username'));
108 $path = session()->pull('url.intended', '/');
109 $path = baseUrl($path, true);
110 return redirect($path);
114 * Show the application login form.
115 * @param Request $request
116 * @return \Illuminate\Http\Response
118 public function getLogin(Request $request)
120 $socialDrivers = $this->socialAuthService->getActiveDrivers();
121 $authMethod = config('auth.method');
123 if ($request->has('email')) {
124 session()->flashInput([
125 'email' => $request->get('email'),
126 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
130 return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
134 * Redirect to the relevant social site.
135 * @param $socialDriver
136 * @return \Symfony\Component\HttpFoundation\RedirectResponse
138 public function getSocialLogin($socialDriver)
140 session()->put('social-callback', 'login');
141 return $this->socialAuthService->startLogIn($socialDriver);