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