3 namespace BookStack\Auth\Access;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\SocialAccount;
7 use BookStack\Auth\User;
8 use BookStack\Auth\UserRepo;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Facades\Activity;
11 use BookStack\Facades\Theme;
12 use BookStack\Theming\ThemeEvents;
14 use Illuminate\Support\Str;
16 class RegistrationService
19 protected $emailConfirmationService;
22 * RegistrationService constructor.
24 public function __construct(UserRepo $userRepo, EmailConfirmationService $emailConfirmationService)
26 $this->userRepo = $userRepo;
27 $this->emailConfirmationService = $emailConfirmationService;
31 * Check whether or not registrations are allowed in the app settings.
33 * @throws UserRegistrationException
35 public function ensureRegistrationAllowed()
37 if (!$this->registrationAllowed()) {
38 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
43 * Check if standard BookStack User registrations are currently allowed.
44 * Does not prevent external-auth based registration.
46 protected function registrationAllowed(): bool
48 $authMethod = config('auth.method');
49 $authMethodsWithRegistration = ['standard'];
51 return in_array($authMethod, $authMethodsWithRegistration) && setting('registration-enabled');
55 * Attempt to find a user in the system otherwise register them as a new
56 * user. For use with external auth systems since password is auto-generated.
58 * @throws UserRegistrationException
60 public function findOrRegister(string $name, string $email, string $externalId): User
63 ->where('external_auth_id', '=', $externalId)
70 'password' => Str::random(32),
71 'external_auth_id' => $externalId,
74 $user = $this->registerUser($userData, null, false);
81 * The registrations flow for all users.
83 * @throws UserRegistrationException
85 public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false): User
87 $userEmail = $userData['email'];
90 $this->ensureEmailDomainAllowed($userEmail);
92 // Ensure user does not already exist
93 $alreadyUser = !is_null($this->userRepo->getByEmail($userEmail));
95 throw new UserRegistrationException(trans('errors.error_user_exists_different_creds', ['email' => $userEmail]), '/login');
99 $newUser = $this->userRepo->registerNew($userData, $emailConfirmed);
101 // Assign social account if given
102 if ($socialAccount) {
103 $newUser->socialAccounts()->save($socialAccount);
106 Activity::add(ActivityType::AUTH_REGISTER, $socialAccount ?? $newUser);
107 Theme::dispatch(ThemeEvents::AUTH_REGISTER, $socialAccount ? $socialAccount->driver : auth()->getDefaultDriver(), $newUser);
109 // Start email confirmation flow if required
110 if ($this->emailConfirmationService->confirmationRequired() && !$emailConfirmed) {
114 $this->emailConfirmationService->sendConfirmation($newUser);
115 session()->flash('sent-email-confirmation', true);
116 } catch (Exception $e) {
117 $message = trans('auth.email_confirm_send_error');
119 throw new UserRegistrationException($message, '/register/confirm');
127 * Ensure that the given email meets any active email domain registration restrictions.
128 * Throws if restrictions are active and the email does not match an allowed domain.
130 * @throws UserRegistrationException
132 protected function ensureEmailDomainAllowed(string $userEmail): void
134 $registrationRestrict = setting('registration-restrict');
136 if (!$registrationRestrict) {
140 $restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
141 $userEmailDomain = $domain = mb_substr(mb_strrchr($userEmail, '@'), 1);
142 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
143 $redirect = $this->registrationAllowed() ? '/register' : '/login';
145 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), $redirect);