]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
LDAP groups sync to Bookstack roles.
[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\SocialAuthService;
10 use Illuminate\Contracts\Auth\Authenticatable;
11 use Illuminate\Foundation\Auth\AuthenticatesUsers;
12 use Illuminate\Http\Request;
13
14 class LoginController extends Controller
15 {
16     /*
17     |--------------------------------------------------------------------------
18     | Login Controller
19     |--------------------------------------------------------------------------
20     |
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.
24     |
25     */
26
27     use AuthenticatesUsers;
28
29     /**
30      * Where to redirect users after login.
31      *
32      * @var string
33      */
34     protected $redirectTo = '/';
35
36     protected $redirectPath = '/';
37     protected $redirectAfterLogout = '/login';
38
39     protected $socialAuthService;
40     protected $userRepo;
41
42     /**
43      * Create a new controller instance.
44      *
45      * @param SocialAuthService $socialAuthService
46      * @param UserRepo $userRepo
47      */
48     public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
49     {
50         $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
51         $this->socialAuthService = $socialAuthService;
52         $this->userRepo = $userRepo;
53         $this->redirectPath = baseUrl('/');
54         $this->redirectAfterLogout = baseUrl('/login');
55         parent::__construct();
56     }
57
58     public function username()
59     {
60         return config('auth.method') === 'standard' ? 'email' : 'username';
61     }
62
63     /**
64      * Overrides the action when a user is authenticated.
65      * If the user authenticated but does not exist in the user table we create them.
66      * @param Request $request
67      * @param Authenticatable $user
68      * @return \Illuminate\Http\RedirectResponse
69      * @throws AuthException
70      */
71     protected function authenticated(Request $request, Authenticatable $user)
72     {
73         // Explicitly log them out for now if they do no exist.
74         if (!$user->exists) {
75             auth()->logout($user);
76         }
77
78         if (!$user->exists && $user->email === null && !$request->filled('email')) {
79             $request->flash();
80             session()->flash('request-email', true);
81             return redirect('/login');
82         }
83
84         if (!$user->exists && $user->email === null && $request->filled('email')) {
85             $user->email = $request->get('email');
86         }
87
88         if (!$user->exists) {
89             // Check for users with same email already
90             $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
91             if ($alreadyUser) {
92                 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
93             }
94
95             $user->save();
96             $this->userRepo->attachDefaultRole($user);
97             auth()->login($user);
98         }
99
100                 // ldap groups refresh
101                 if (config('services.ldap.user_to_groups') !== false && $request->filled('username')) {
102                         $ldapRepo = new LdapRepo($this->userRepo);
103                         $ldapRepo->syncGroups($user,$request->input('username'));
104                 }
105
106
107                 $path = session()->pull('url.intended', '/');
108         $path = baseUrl($path, true);
109         return redirect($path);
110     }
111
112     /**
113      * Show the application login form.
114      * @param Request $request
115      * @return \Illuminate\Http\Response
116      */
117     public function getLogin(Request $request)
118     {
119         $socialDrivers = $this->socialAuthService->getActiveDrivers();
120         $authMethod = config('auth.method');
121
122         if ($request->has('email')) {
123             session()->flashInput([
124                 'email' => $request->get('email'),
125                 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
126             ]);
127         }
128
129         return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
130     }
131
132     /**
133      * Redirect to the relevant social site.
134      * @param $socialDriver
135      * @return \Symfony\Component\HttpFoundation\RedirectResponse
136      */
137     public function getSocialLogin($socialDriver)
138     {
139         session()->put('social-callback', 'login');
140         return $this->socialAuthService->startLogIn($socialDriver);
141     }
142 }