]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
Corrected the keys for okta auth
[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) {
74             auth()->logout($user);
75         }
76
77         if (!$user->exists && $user->email === null && !$request->filled('email')) {
78             $request->flash();
79             session()->flash('request-email', true);
80             return redirect('/login');
81         }
82
83         if (!$user->exists && $user->email === null && $request->filled('email')) {
84             $user->email = $request->get('email');
85         }
86
87         if (!$user->exists) {
88             // Check for users with same email already
89             $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
90             if ($alreadyUser) {
91                 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
92             }
93
94             $user->save();
95             $this->userRepo->attachDefaultRole($user);
96             auth()->login($user);
97         }
98
99         $path = session()->pull('url.intended', '/');
100         $path = baseUrl($path, true);
101         return redirect($path);
102     }
103
104     /**
105      * Show the application login form.
106      * @param Request $request
107      * @return \Illuminate\Http\Response
108      */
109     public function getLogin(Request $request)
110     {
111         $socialDrivers = $this->socialAuthService->getActiveDrivers();
112         $authMethod = config('auth.method');
113
114         if ($request->has('email')) {
115             session()->flashInput([
116                 'email' => $request->get('email'),
117                 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
118             ]);
119         }
120
121         return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
122     }
123
124     /**
125      * Redirect to the relevant social site.
126      * @param $socialDriver
127      * @return \Symfony\Component\HttpFoundation\RedirectResponse
128      */
129     public function getSocialLogin($socialDriver)
130     {
131         session()->put('social-callback', 'login');
132         return $this->socialAuthService->startLogIn($socialDriver);
133     }
134 }