1 <?php namespace BookStack\Auth\Access;
3 use BookStack\Auth\SocialAccount;
4 use BookStack\Auth\User;
5 use BookStack\Auth\UserRepo;
6 use BookStack\Exceptions\UserRegistrationException;
9 class RegistrationService
13 protected $emailConfirmationService;
16 * RegistrationService constructor.
18 public function __construct(UserRepo $userRepo, EmailConfirmationService $emailConfirmationService)
20 $this->userRepo = $userRepo;
21 $this->emailConfirmationService = $emailConfirmationService;
25 * Check whether or not registrations are allowed in the app settings.
26 * @throws UserRegistrationException
28 public function ensureRegistrationAllowed()
30 if (!$this->registrationAllowed()) {
31 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
36 * Check if standard BookStack User registrations are currently allowed.
37 * Does not prevent external-auth based registration.
39 protected function registrationAllowed(): bool
41 $authMethod = config('auth.method');
42 $authMethodsWithRegistration = ['standard'];
43 return in_array($authMethod, $authMethodsWithRegistration) && setting('registration-enabled');
47 * The registrations flow for all users.
48 * @throws UserRegistrationException
50 public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false): User
52 $userEmail = $userData['email'];
55 $this->ensureEmailDomainAllowed($userEmail);
57 // Ensure user does not already exist
58 $alreadyUser = !is_null($this->userRepo->getByEmail($userEmail));
60 throw new UserRegistrationException(trans('errors.error_user_exists_different_creds', ['email' => $userEmail]));
64 $newUser = $this->userRepo->registerNew($userData, $emailConfirmed);
66 // Assign social account if given
68 $newUser->socialAccounts()->save($socialAccount);
71 // Start email confirmation flow if required
72 if ($this->emailConfirmationService->confirmationRequired() && !$emailConfirmed) {
77 $this->emailConfirmationService->sendConfirmation($newUser);
78 } catch (Exception $e) {
79 $message = trans('auth.email_confirm_send_error');
82 throw new UserRegistrationException($message, '/register/confirm');
89 * Ensure that the given email meets any active email domain registration restrictions.
90 * Throws if restrictions are active and the email does not match an allowed domain.
91 * @throws UserRegistrationException
93 protected function ensureEmailDomainAllowed(string $userEmail): void
95 $registrationRestrict = setting('registration-restrict');
97 if (!$registrationRestrict) {
101 $restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
102 $userEmailDomain = $domain = mb_substr(mb_strrchr($userEmail, "@"), 1);
103 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
104 $redirect = $this->registrationAllowed() ? '/register' : '/login';
105 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), $redirect);
110 * Alias to the UserRepo method of the same name.
111 * Attaches the default system role, if configured, to the given user.
113 public function attachDefaultRole(User $user): void
115 $this->userRepo->attachDefaultRole($user);