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