]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
Added tests to cover ldap group mapping
[bookstack] / app / Http / Controllers / Auth / LoginController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
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;
14
15 class LoginController extends Controller
16 {
17     /*
18     |--------------------------------------------------------------------------
19     | Login Controller
20     |--------------------------------------------------------------------------
21     |
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.
25     |
26     */
27
28     use AuthenticatesUsers;
29
30     /**
31      * Where to redirect users after login.
32      *
33      * @var string
34      */
35     protected $redirectTo = '/';
36
37     protected $redirectPath = '/';
38     protected $redirectAfterLogout = '/login';
39
40     protected $socialAuthService;
41     protected $userRepo;
42
43     /**
44      * Create a new controller instance.
45      *
46      * @param SocialAuthService $socialAuthService
47      * @param UserRepo $userRepo
48      */
49     public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
50     {
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();
57     }
58
59     public function username()
60     {
61         return config('auth.method') === 'standard' ? 'email' : 'username';
62     }
63
64     /**
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
71      */
72     protected function authenticated(Request $request, Authenticatable $user)
73     {
74         // Explicitly log them out for now if they do no exist.
75         if (!$user->exists) {
76             auth()->logout($user);
77         }
78
79         if (!$user->exists && $user->email === null && !$request->filled('email')) {
80             $request->flash();
81             session()->flash('request-email', true);
82             return redirect('/login');
83         }
84
85         if (!$user->exists && $user->email === null && $request->filled('email')) {
86             $user->email = $request->get('email');
87         }
88
89         if (!$user->exists) {
90             // Check for users with same email already
91             $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
92             if ($alreadyUser) {
93                 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
94             }
95
96             $user->save();
97             $this->userRepo->attachDefaultRole($user);
98             auth()->login($user);
99         }
100
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'));
105         }
106
107
108         $path = session()->pull('url.intended', '/');
109         $path = baseUrl($path, true);
110         return redirect($path);
111     }
112
113     /**
114      * Show the application login form.
115      * @param Request $request
116      * @return \Illuminate\Http\Response
117      */
118     public function getLogin(Request $request)
119     {
120         $socialDrivers = $this->socialAuthService->getActiveDrivers();
121         $authMethod = config('auth.method');
122
123         if ($request->has('email')) {
124             session()->flashInput([
125                 'email' => $request->get('email'),
126                 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
127             ]);
128         }
129
130         return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
131     }
132
133     /**
134      * Redirect to the relevant social site.
135      * @param $socialDriver
136      * @return \Symfony\Component\HttpFoundation\RedirectResponse
137      */
138     public function getSocialLogin($socialDriver)
139     {
140         session()->put('social-callback', 'login');
141         return $this->socialAuthService->startLogIn($socialDriver);
142     }
143 }