3 namespace BookStack\Access;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Exceptions\UserRegistrationException;
7 use BookStack\Facades\Activity;
8 use BookStack\Facades\Theme;
9 use BookStack\Theming\ThemeEvents;
10 use BookStack\Users\Models\User;
11 use BookStack\Users\UserRepo;
13 use Illuminate\Support\Str;
15 class RegistrationService
18 protected $emailConfirmationService;
21 * RegistrationService constructor.
23 public function __construct(UserRepo $userRepo, EmailConfirmationService $emailConfirmationService)
25 $this->userRepo = $userRepo;
26 $this->emailConfirmationService = $emailConfirmationService;
30 * Check whether or not registrations are allowed in the app settings.
32 * @throws UserRegistrationException
34 public function ensureRegistrationAllowed()
36 if (!$this->registrationAllowed()) {
37 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
42 * Check if standard BookStack User registrations are currently allowed.
43 * Does not prevent external-auth based registration.
45 protected function registrationAllowed(): bool
47 $authMethod = config('auth.method');
48 $authMethodsWithRegistration = ['standard'];
50 return in_array($authMethod, $authMethodsWithRegistration) && setting('registration-enabled');
54 * Attempt to find a user in the system otherwise register them as a new
55 * user. For use with external auth systems since password is auto-generated.
57 * @throws UserRegistrationException
59 public function findOrRegister(string $name, string $email, string $externalId): User
62 ->where('external_auth_id', '=', $externalId)
69 'password' => Str::random(32),
70 'external_auth_id' => $externalId,
73 $user = $this->registerUser($userData, null, false);
80 * The registrations flow for all users.
82 * @throws UserRegistrationException
84 public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false): User
86 $userEmail = $userData['email'];
89 $this->ensureEmailDomainAllowed($userEmail);
91 // Ensure user does not already exist
92 $alreadyUser = !is_null($this->userRepo->getByEmail($userEmail));
94 throw new UserRegistrationException(trans('errors.error_user_exists_different_creds', ['email' => $userEmail]), '/login');
98 $newUser = $this->userRepo->createWithoutActivity($userData, $emailConfirmed);
99 $newUser->attachDefaultRole();
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);