2020-01-26 15:42:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2021-03-19 22:54:50 +01:00
|
|
|
use BookStack\Actions\ActivityType;
|
2020-01-26 15:42:50 +01:00
|
|
|
use BookStack\Auth\Access\RegistrationService;
|
|
|
|
use BookStack\Auth\Access\SocialAuthService;
|
|
|
|
use BookStack\Exceptions\SocialDriverNotConfigured;
|
|
|
|
use BookStack\Exceptions\SocialSignInAccountNotUsed;
|
|
|
|
use BookStack\Exceptions\SocialSignInException;
|
|
|
|
use BookStack\Exceptions\UserRegistrationException;
|
2021-03-19 22:54:50 +01:00
|
|
|
use BookStack\Facades\Theme;
|
2020-01-26 15:42:50 +01:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
2021-03-19 22:54:50 +01:00
|
|
|
use BookStack\Theming\ThemeEvents;
|
2020-01-26 15:42:50 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Socialite\Contracts\User as SocialUser;
|
|
|
|
|
|
|
|
class SocialController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $socialAuthService;
|
|
|
|
protected $registrationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SocialController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
|
|
|
|
{
|
|
|
|
$this->middleware('guest')->only(['getRegister', 'postRegister']);
|
|
|
|
$this->socialAuthService = $socialAuthService;
|
|
|
|
$this->registrationService = $registrationService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the relevant social site.
|
2021-03-10 23:37:53 +01:00
|
|
|
* @throws SocialDriverNotConfigured
|
2020-01-26 15:42:50 +01:00
|
|
|
*/
|
2021-03-10 23:37:53 +01:00
|
|
|
public function login(string $socialDriver)
|
2020-01-26 15:42:50 +01:00
|
|
|
{
|
|
|
|
session()->put('social-callback', 'login');
|
|
|
|
return $this->socialAuthService->startLogIn($socialDriver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the social site for authentication intended to register.
|
|
|
|
* @throws SocialDriverNotConfigured
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
2021-03-10 23:37:53 +01:00
|
|
|
public function register(string $socialDriver)
|
2020-01-26 15:42:50 +01:00
|
|
|
{
|
2020-02-02 18:31:00 +01:00
|
|
|
$this->registrationService->ensureRegistrationAllowed();
|
2020-01-26 15:42:50 +01:00
|
|
|
session()->put('social-callback', 'register');
|
|
|
|
return $this->socialAuthService->startRegister($socialDriver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The callback for social login services.
|
|
|
|
* @throws SocialSignInException
|
|
|
|
* @throws SocialDriverNotConfigured
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
2021-03-10 23:37:53 +01:00
|
|
|
public function callback(Request $request, string $socialDriver)
|
2020-01-26 15:42:50 +01:00
|
|
|
{
|
|
|
|
if (!session()->has('social-callback')) {
|
|
|
|
throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check request for error information
|
|
|
|
if ($request->has('error') && $request->has('error_description')) {
|
|
|
|
throw new SocialSignInException(trans('errors.social_login_bad_response', [
|
|
|
|
'socialAccount' => $socialDriver,
|
|
|
|
'error' => $request->get('error_description'),
|
|
|
|
]), '/login');
|
|
|
|
}
|
|
|
|
|
|
|
|
$action = session()->pull('social-callback');
|
|
|
|
|
|
|
|
// Attempt login or fall-back to register if allowed.
|
|
|
|
$socialUser = $this->socialAuthService->getSocialUser($socialDriver);
|
2020-02-02 18:31:00 +01:00
|
|
|
if ($action === 'login') {
|
2020-01-26 15:42:50 +01:00
|
|
|
try {
|
|
|
|
return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
|
|
|
|
} catch (SocialSignInAccountNotUsed $exception) {
|
|
|
|
if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
|
|
|
|
return $this->socialRegisterCallback($socialDriver, $socialUser);
|
|
|
|
}
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:31:00 +01:00
|
|
|
if ($action === 'register') {
|
2020-01-26 15:42:50 +01:00
|
|
|
return $this->socialRegisterCallback($socialDriver, $socialUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detach a social account from a user.
|
|
|
|
*/
|
2021-03-10 23:37:53 +01:00
|
|
|
public function detach(string $socialDriver)
|
2020-01-26 15:42:50 +01:00
|
|
|
{
|
|
|
|
$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.
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
|
|
|
protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
|
|
|
|
{
|
|
|
|
$socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
|
2021-03-19 17:16:26 +01:00
|
|
|
$socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser);
|
2020-01-26 15:42:50 +01:00
|
|
|
$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(),
|
2020-02-02 18:31:00 +01:00
|
|
|
'password' => Str::random(32)
|
2020-01-26 15:42:50 +01:00
|
|
|
];
|
|
|
|
|
2020-03-10 20:09:22 +01:00
|
|
|
// Take name from email address if empty
|
|
|
|
if (!$userData['name']) {
|
|
|
|
$userData['name'] = explode('@', $userData['email'])[0];
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:31:00 +01:00
|
|
|
$user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
|
|
|
|
auth()->login($user);
|
2021-03-19 22:54:50 +01:00
|
|
|
Theme::dispatch(ThemeEvents::AUTH_LOGIN, $socialDriver, $user);
|
|
|
|
$this->logActivity(ActivityType::AUTH_LOGIN, $user);
|
2020-01-26 15:42:50 +01:00
|
|
|
|
|
|
|
$this->showSuccessNotification(trans('auth.register_success'));
|
|
|
|
return redirect('/');
|
|
|
|
}
|
|
|
|
}
|