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