2020-01-26 09:42:50 -05:00
|
|
|
<?php namespace BookStack\Auth\Access;
|
|
|
|
|
2020-11-20 14:33:11 -05:00
|
|
|
use BookStack\Actions\ActivityType;
|
2020-01-26 09:42:50 -05:00
|
|
|
use BookStack\Auth\SocialAccount;
|
2020-02-02 12:31:00 -05:00
|
|
|
use BookStack\Auth\User;
|
2020-01-26 09:42:50 -05:00
|
|
|
use BookStack\Auth\UserRepo;
|
|
|
|
use BookStack\Exceptions\UserRegistrationException;
|
2020-11-20 14:33:11 -05:00
|
|
|
use BookStack\Facades\Activity;
|
2020-01-26 09:42:50 -05:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class RegistrationService
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $userRepo;
|
|
|
|
protected $emailConfirmationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RegistrationService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(UserRepo $userRepo, EmailConfirmationService $emailConfirmationService)
|
|
|
|
{
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
$this->emailConfirmationService = $emailConfirmationService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether or not registrations are allowed in the app settings.
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
2020-02-02 12:31:00 -05:00
|
|
|
public function ensureRegistrationAllowed()
|
2020-01-26 09:42:50 -05:00
|
|
|
{
|
2020-02-02 12:31:00 -05:00
|
|
|
if (!$this->registrationAllowed()) {
|
2020-01-26 09:42:50 -05:00
|
|
|
throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
/**
|
|
|
|
* Check if standard BookStack User registrations are currently allowed.
|
|
|
|
* Does not prevent external-auth based registration.
|
|
|
|
*/
|
|
|
|
protected function registrationAllowed(): bool
|
|
|
|
{
|
|
|
|
$authMethod = config('auth.method');
|
|
|
|
$authMethodsWithRegistration = ['standard'];
|
|
|
|
return in_array($authMethod, $authMethodsWithRegistration) && setting('registration-enabled');
|
|
|
|
}
|
|
|
|
|
2020-01-26 09:42:50 -05:00
|
|
|
/**
|
|
|
|
* The registrations flow for all users.
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
2020-02-02 12:31:00 -05:00
|
|
|
public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false): User
|
2020-01-26 09:42:50 -05:00
|
|
|
{
|
2020-02-02 12:31:00 -05:00
|
|
|
$userEmail = $userData['email'];
|
2020-01-26 09:42:50 -05:00
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
// Email restriction
|
|
|
|
$this->ensureEmailDomainAllowed($userEmail);
|
|
|
|
|
|
|
|
// Ensure user does not already exist
|
|
|
|
$alreadyUser = !is_null($this->userRepo->getByEmail($userEmail));
|
|
|
|
if ($alreadyUser) {
|
2020-09-26 11:43:06 -04:00
|
|
|
throw new UserRegistrationException(trans('errors.error_user_exists_different_creds', ['email' => $userEmail]), '/login');
|
2020-01-26 09:42:50 -05:00
|
|
|
}
|
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
// Create the user
|
|
|
|
$newUser = $this->userRepo->registerNew($userData, $emailConfirmed);
|
2020-01-26 09:42:50 -05:00
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
// Assign social account if given
|
2020-01-26 09:42:50 -05:00
|
|
|
if ($socialAccount) {
|
|
|
|
$newUser->socialAccounts()->save($socialAccount);
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:33:11 -05:00
|
|
|
Activity::add(ActivityType::AUTH_REGISTER, $socialAccount ?? $newUser);
|
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
// Start email confirmation flow if required
|
|
|
|
if ($this->emailConfirmationService->confirmationRequired() && !$emailConfirmed) {
|
2020-01-26 09:42:50 -05:00
|
|
|
$newUser->save();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->emailConfirmationService->sendConfirmation($newUser);
|
2020-09-05 12:26:48 -04:00
|
|
|
session()->flash('sent-email-confirmation', true);
|
2020-01-26 09:42:50 -05:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$message = trans('auth.email_confirm_send_error');
|
2020-08-04 12:54:50 -04:00
|
|
|
throw new UserRegistrationException($message, '/register/confirm');
|
2020-01-26 09:42:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:31:00 -05:00
|
|
|
return $newUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the given email meets any active email domain registration restrictions.
|
|
|
|
* Throws if restrictions are active and the email does not match an allowed domain.
|
|
|
|
* @throws UserRegistrationException
|
|
|
|
*/
|
|
|
|
protected function ensureEmailDomainAllowed(string $userEmail): void
|
|
|
|
{
|
|
|
|
$registrationRestrict = setting('registration-restrict');
|
|
|
|
|
|
|
|
if (!$registrationRestrict) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
|
|
|
|
$userEmailDomain = $domain = mb_substr(mb_strrchr($userEmail, "@"), 1);
|
|
|
|
if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
|
|
|
|
$redirect = $this->registrationAllowed() ? '/register' : '/login';
|
|
|
|
throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), $redirect);
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 17:24:05 -05:00
|
|
|
}
|