]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/SocialController.php
Merge branch 'validation_fixes' of git://github.com/TBK/BookStack into TBK-validation...
[bookstack] / app / Http / Controllers / Auth / SocialController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\RegistrationService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Exceptions\SocialDriverNotConfigured;
8 use BookStack\Exceptions\SocialSignInAccountNotUsed;
9 use BookStack\Exceptions\SocialSignInException;
10 use BookStack\Exceptions\UserRegistrationException;
11 use BookStack\Http\Controllers\Controller;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
14 use Illuminate\Routing\Redirector;
15 use Illuminate\Support\Str;
16 use Laravel\Socialite\Contracts\User as SocialUser;
17
18 class SocialController extends Controller
19 {
20
21     protected $socialAuthService;
22     protected $registrationService;
23
24     /**
25      * SocialController constructor.
26      */
27     public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
28     {
29         $this->middleware('guest')->only(['getRegister', 'postRegister']);
30         $this->socialAuthService = $socialAuthService;
31         $this->registrationService = $registrationService;
32     }
33
34
35     /**
36      * Redirect to the relevant social site.
37      * @throws \BookStack\Exceptions\SocialDriverNotConfigured
38      */
39     public function getSocialLogin(string $socialDriver)
40     {
41         session()->put('social-callback', 'login');
42         return $this->socialAuthService->startLogIn($socialDriver);
43     }
44
45     /**
46      * Redirect to the social site for authentication intended to register.
47      * @throws SocialDriverNotConfigured
48      * @throws UserRegistrationException
49      */
50     public function socialRegister(string $socialDriver)
51     {
52         $this->registrationService->ensureRegistrationAllowed();
53         session()->put('social-callback', 'register');
54         return $this->socialAuthService->startRegister($socialDriver);
55     }
56
57     /**
58      * The callback for social login services.
59      * @throws SocialSignInException
60      * @throws SocialDriverNotConfigured
61      * @throws UserRegistrationException
62      */
63     public function socialCallback(Request $request, string $socialDriver)
64     {
65         if (!session()->has('social-callback')) {
66             throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
67         }
68
69         // Check request for error information
70         if ($request->has('error') && $request->has('error_description')) {
71             throw new SocialSignInException(trans('errors.social_login_bad_response', [
72                 'socialAccount' => $socialDriver,
73                 'error' => $request->get('error_description'),
74             ]), '/login');
75         }
76
77         $action = session()->pull('social-callback');
78
79         // Attempt login or fall-back to register if allowed.
80         $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
81         if ($action === 'login') {
82             try {
83                 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
84             } catch (SocialSignInAccountNotUsed $exception) {
85                 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
86                     return $this->socialRegisterCallback($socialDriver, $socialUser);
87                 }
88                 throw $exception;
89             }
90         }
91
92         if ($action === 'register') {
93             return $this->socialRegisterCallback($socialDriver, $socialUser);
94         }
95
96         return redirect()->back();
97     }
98
99     /**
100      * Detach a social account from a user.
101      */
102     public function detachSocialAccount(string $socialDriver)
103     {
104         $this->socialAuthService->detachSocialAccount($socialDriver);
105         session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
106         return redirect(user()->getEditUrl());
107     }
108
109     /**
110      * Register a new user after a registration callback.
111      * @throws UserRegistrationException
112      */
113     protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
114     {
115         $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
116         $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
117         $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
118
119         // Create an array of the user data to create a new user instance
120         $userData = [
121             'name' => $socialUser->getName(),
122             'email' => $socialUser->getEmail(),
123             'password' => Str::random(32)
124         ];
125
126         // Take name from email address if empty
127         if (!$userData['name']) {
128             $userData['name'] = explode('@', $userData['email'])[0];
129         }
130
131         $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
132         auth()->login($user);
133
134         $this->showSuccessNotification(trans('auth.register_success'));
135         return redirect('/');
136     }
137 }