]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/SocialController.php
447f0afc9358f85eb8e7975ea08930878c332c1a
[bookstack] / app / Http / Controllers / Auth / SocialController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Exceptions\SocialDriverNotConfigured;
9 use BookStack\Exceptions\SocialSignInAccountNotUsed;
10 use BookStack\Exceptions\SocialSignInException;
11 use BookStack\Exceptions\UserRegistrationException;
12 use BookStack\Facades\Theme;
13 use BookStack\Http\Controllers\Controller;
14 use BookStack\Theming\ThemeEvents;
15 use Illuminate\Http\Request;
16 use Illuminate\Support\Str;
17 use Laravel\Socialite\Contracts\User as SocialUser;
18
19 class SocialController extends Controller
20 {
21
22     protected $socialAuthService;
23     protected $registrationService;
24
25     /**
26      * SocialController constructor.
27      */
28     public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
29     {
30         $this->middleware('guest')->only(['getRegister', 'postRegister']);
31         $this->socialAuthService = $socialAuthService;
32         $this->registrationService = $registrationService;
33     }
34
35     /**
36      * Redirect to the relevant social site.
37      * @throws SocialDriverNotConfigured
38      */
39     public function login(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 register(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 callback(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 detach(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->newSocialAccount($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         Theme::dispatch(ThemeEvents::AUTH_LOGIN, $socialDriver, $user);
134         $this->logActivity(ActivityType::AUTH_LOGIN, $user);
135
136         $this->showSuccessNotification(trans('auth.register_success'));
137         return redirect('/');
138     }
139 }