X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/92690d1ae92363467c6a49664a558320b8b5771b..refs/pull/3598/head:/app/Http/Controllers/Auth/SocialController.php diff --git a/app/Http/Controllers/Auth/SocialController.php b/app/Http/Controllers/Auth/SocialController.php index bcd82a9c0..1691668a2 100644 --- a/app/Http/Controllers/Auth/SocialController.php +++ b/app/Http/Controllers/Auth/SocialController.php @@ -2,6 +2,7 @@ namespace BookStack\Http\Controllers\Auth; +use BookStack\Auth\Access\LoginService; use BookStack\Auth\Access\RegistrationService; use BookStack\Auth\Access\SocialAuthService; use BookStack\Exceptions\SocialDriverNotConfigured; @@ -9,58 +10,64 @@ use BookStack\Exceptions\SocialSignInAccountNotUsed; use BookStack\Exceptions\SocialSignInException; use BookStack\Exceptions\UserRegistrationException; use BookStack\Http\Controllers\Controller; -use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; -use Illuminate\Routing\Redirector; use Illuminate\Support\Str; use Laravel\Socialite\Contracts\User as SocialUser; class SocialController extends Controller { - protected $socialAuthService; protected $registrationService; + protected $loginService; /** * SocialController constructor. */ - public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService) - { + public function __construct( + SocialAuthService $socialAuthService, + RegistrationService $registrationService, + LoginService $loginService + ) { $this->middleware('guest')->only(['getRegister', 'postRegister']); $this->socialAuthService = $socialAuthService; $this->registrationService = $registrationService; + $this->loginService = $loginService; } - /** * Redirect to the relevant social site. - * @throws \BookStack\Exceptions\SocialDriverNotConfigured + * + * @throws SocialDriverNotConfigured */ - public function getSocialLogin(string $socialDriver) + public function login(string $socialDriver) { session()->put('social-callback', 'login'); + return $this->socialAuthService->startLogIn($socialDriver); } /** * Redirect to the social site for authentication intended to register. + * * @throws SocialDriverNotConfigured * @throws UserRegistrationException */ - public function socialRegister(string $socialDriver) + public function register(string $socialDriver) { - $this->registrationService->checkRegistrationAllowed(); + $this->registrationService->ensureRegistrationAllowed(); session()->put('social-callback', 'register'); + return $this->socialAuthService->startRegister($socialDriver); } /** * The callback for social login services. + * * @throws SocialSignInException * @throws SocialDriverNotConfigured * @throws UserRegistrationException */ - public function socialCallback(Request $request, string $socialDriver) + public function callback(Request $request, string $socialDriver) { if (!session()->has('social-callback')) { throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login'); @@ -70,7 +77,7 @@ class SocialController extends Controller if ($request->has('error') && $request->has('error_description')) { throw new SocialSignInException(trans('errors.social_login_bad_response', [ 'socialAccount' => $socialDriver, - 'error' => $request->get('error_description'), + 'error' => $request->get('error_description'), ]), '/login'); } @@ -78,18 +85,19 @@ class SocialController extends Controller // Attempt login or fall-back to register if allowed. $socialUser = $this->socialAuthService->getSocialUser($socialDriver); - if ($action == 'login') { + if ($action === 'login') { try { return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser); } catch (SocialSignInAccountNotUsed $exception) { if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) { return $this->socialRegisterCallback($socialDriver, $socialUser); } + throw $exception; } } - if ($action == 'register') { + if ($action === 'register') { return $this->socialRegisterCallback($socialDriver, $socialUser); } @@ -99,41 +107,41 @@ class SocialController extends Controller /** * Detach a social account from a user. */ - public function detachSocialAccount(string $socialDriver) + public function detach(string $socialDriver) { $this->socialAuthService->detachSocialAccount($socialDriver); session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)])); + return redirect(user()->getEditUrl()); } /** * Register a new user after a registration callback. - * @return RedirectResponse|Redirector + * * @throws UserRegistrationException */ protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser) { $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser); - $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser); + $socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser); $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver); // Create an array of the user data to create a new user instance $userData = [ - 'name' => $socialUser->getName(), - 'email' => $socialUser->getEmail(), - 'password' => Str::random(30) + 'name' => $socialUser->getName(), + 'email' => $socialUser->getEmail(), + 'password' => Str::random(32), ]; - try { - $this->registrationService->registerUser($userData, $socialAccount, $emailVerified); - } catch (UserRegistrationException $exception) { - if ($exception->getMessage()) { - $this->showErrorNotification($exception->getMessage()); - } - return redirect($exception->redirectLocation); + // Take name from email address if empty + if (!$userData['name']) { + $userData['name'] = explode('@', $userData['email'])[0]; } + $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified); $this->showSuccessNotification(trans('auth.register_success')); + $this->loginService->login($user, $socialDriver); + return redirect('/'); } }