]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
added Arabic to locales
[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\LdapService;
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 $ldapService;
41     protected $userRepo;
42
43     /**
44      * Create a new controller instance.
45      *
46      * @param SocialAuthService $socialAuthService
47      * @param LdapService $ldapService
48      * @param UserRepo $userRepo
49      */
50     public function __construct(SocialAuthService $socialAuthService, LdapService $ldapService, UserRepo $userRepo)
51     {
52         $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
53         $this->socialAuthService = $socialAuthService;
54         $this->ldapService = $ldapService;
55         $this->userRepo = $userRepo;
56         $this->redirectPath = baseUrl('/');
57         $this->redirectAfterLogout = baseUrl('/login');
58         parent::__construct();
59     }
60
61     public function username()
62     {
63         return config('auth.method') === 'standard' ? 'email' : 'username';
64     }
65
66     /**
67      * Overrides the action when a user is authenticated.
68      * If the user authenticated but does not exist in the user table we create them.
69      * @param Request $request
70      * @param Authenticatable $user
71      * @return \Illuminate\Http\RedirectResponse
72      * @throws AuthException
73      */
74     protected function authenticated(Request $request, Authenticatable $user)
75     {
76         // Explicitly log them out for now if they do no exist.
77         if (!$user->exists) {
78             auth()->logout($user);
79         }
80
81         if (!$user->exists && $user->email === null && !$request->filled('email')) {
82             $request->flash();
83             session()->flash('request-email', true);
84             return redirect('/login');
85         }
86
87         if (!$user->exists && $user->email === null && $request->filled('email')) {
88             $user->email = $request->get('email');
89         }
90
91         if (!$user->exists) {
92             // Check for users with same email already
93             $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
94             if ($alreadyUser) {
95                 throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
96             }
97
98             $user->save();
99             $this->userRepo->attachDefaultRole($user);
100             auth()->login($user);
101         }
102
103         // Sync LDAP groups if required
104         if ($this->ldapService->shouldSyncGroups()) {
105             $this->ldapService->syncGroups($user);
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      * @throws \BookStack\Exceptions\SocialDriverNotConfigured
138      */
139     public function getSocialLogin($socialDriver)
140     {
141         session()->put('social-callback', 'login');
142         return $this->socialAuthService->startLogIn($socialDriver);
143     }
144 }