3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\EmailConfirmationService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Auth\SocialAccount;
8 use BookStack\Auth\User;
9 use BookStack\Auth\UserRepo;
10 use BookStack\Exceptions\SocialDriverNotConfigured;
11 use BookStack\Exceptions\SocialSignInAccountNotUsed;
12 use BookStack\Exceptions\SocialSignInException;
13 use BookStack\Exceptions\UserRegistrationException;
14 use BookStack\Http\Controllers\Controller;
16 use Illuminate\Foundation\Auth\RegistersUsers;
17 use Illuminate\Http\RedirectResponse;
18 use Illuminate\Http\Request;
19 use Illuminate\Http\Response;
20 use Illuminate\Routing\Redirector;
21 use Illuminate\Support\Facades\Hash;
22 use Illuminate\Support\Str;
23 use Laravel\Socialite\Contracts\User as SocialUser;
26 class RegisterController extends Controller
29 |--------------------------------------------------------------------------
31 |--------------------------------------------------------------------------
33 | This controller handles the registration of new users as well as their
34 | validation and creation. By default this controller uses a trait to
35 | provide this functionality without requiring any additional code.
41 protected $socialAuthService;
42 protected $emailConfirmationService;
46 * Where to redirect users after login / registration.
50 protected $redirectTo = '/';
51 protected $redirectPath = '/';
54 * Create a new controller instance.
56 * @param SocialAuthService $socialAuthService
57 * @param EmailConfirmationService $emailConfirmationService
58 * @param UserRepo $userRepo
60 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
62 $this->middleware('guest')->only(['getRegister', 'postRegister', 'socialRegister']);
63 $this->socialAuthService = $socialAuthService;
64 $this->emailConfirmationService = $emailConfirmationService;
65 $this->userRepo = $userRepo;
66 $this->redirectTo = url('/');
67 $this->redirectPath = url('/');
68 parent::__construct();
72 * Get a validator for an incoming registration request.
75 * @return \Illuminate\Contracts\Validation\Validator
77 protected function validator(array $data)
79 return Validator::make($data, [
80 'name' => 'required|min:2|max:255',
81 'email' => 'required|email|max:255|unique:users',
82 'password' => 'required|min:8',
87 * Check whether or not registrations are allowed in the app settings.
88 * @throws UserRegistrationException
90 protected function checkRegistrationAllowed()
92 if (!setting('registration-enabled')) {
93 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
98 * Show the application registration form.
100 * @throws UserRegistrationException
102 public function getRegister()
104 $this->checkRegistrationAllowed();
105 $socialDrivers = $this->socialAuthService->getActiveDrivers();
106 return view('auth.register', ['socialDrivers' => $socialDrivers]);
110 * Handle a registration request for the application.
111 * @param Request|Request $request
112 * @return RedirectResponse|Redirector
113 * @throws UserRegistrationException
115 public function postRegister(Request $request)
117 $this->checkRegistrationAllowed();
118 $this->validator($request->all())->validate();
120 $userData = $request->all();
121 return $this->registerUser($userData);
125 * Create a new user instance after a valid registration.
129 protected function create(array $data)
131 return User::create([
132 'name' => $data['name'],
133 'email' => $data['email'],
134 'password' => Hash::make($data['password']),
139 * The registrations flow for all users.
140 * @param array $userData
141 * @param bool|false|SocialAccount $socialAccount
142 * @param bool $emailVerified
143 * @return RedirectResponse|Redirector
144 * @throws UserRegistrationException
146 protected function registerUser(array $userData, $socialAccount = false, $emailVerified = false)
148 $registrationRestrict = setting('registration-restrict');
150 if ($registrationRestrict) {
151 $restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
152 $userEmailDomain = $domain = mb_substr(mb_strrchr($userData['email'], "@"), 1);
153 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
154 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), '/register');
158 $newUser = $this->userRepo->registerNew($userData, $emailVerified);
159 if ($socialAccount) {
160 $newUser->socialAccounts()->save($socialAccount);
163 if ($this->emailConfirmationService->confirmationRequired() && !$emailVerified) {
167 $this->emailConfirmationService->sendConfirmation($newUser);
168 } catch (Exception $e) {
169 session()->flash('error', trans('auth.email_confirm_send_error'));
172 return redirect('/register/confirm');
175 auth()->login($newUser);
176 session()->flash('success', trans('auth.register_success'));
177 return redirect($this->redirectPath());
181 * Redirect to the social site for authentication intended to register.
182 * @param $socialDriver
184 * @throws UserRegistrationException
185 * @throws SocialDriverNotConfigured
187 public function socialRegister($socialDriver)
189 $this->checkRegistrationAllowed();
190 session()->put('social-callback', 'register');
191 return $this->socialAuthService->startRegister($socialDriver);
195 * The callback for social login services.
196 * @param $socialDriver
197 * @param Request $request
198 * @return RedirectResponse|Redirector
199 * @throws SocialSignInException
200 * @throws UserRegistrationException
201 * @throws SocialDriverNotConfigured
203 public function socialCallback($socialDriver, Request $request)
205 if (!session()->has('social-callback')) {
206 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
209 // Check request for error information
210 if ($request->has('error') && $request->has('error_description')) {
211 throw new SocialSignInException(trans('errors.social_login_bad_response', [
212 'socialAccount' => $socialDriver,
213 'error' => $request->get('error_description'),
217 $action = session()->pull('social-callback');
219 // Attempt login or fall-back to register if allowed.
220 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
221 if ($action == 'login') {
223 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
224 } catch (SocialSignInAccountNotUsed $exception) {
225 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
226 return $this->socialRegisterCallback($socialDriver, $socialUser);
232 if ($action == 'register') {
233 return $this->socialRegisterCallback($socialDriver, $socialUser);
236 return redirect()->back();
240 * Detach a social account from a user.
241 * @param $socialDriver
242 * @return RedirectResponse|Redirector
244 public function detachSocialAccount($socialDriver)
246 return $this->socialAuthService->detachSocialAccount($socialDriver);
250 * Register a new user after a registration callback.
251 * @param string $socialDriver
252 * @param SocialUser $socialUser
253 * @return RedirectResponse|Redirector
254 * @throws UserRegistrationException
256 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
258 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
259 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
260 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
262 // Create an array of the user data to create a new user instance
264 'name' => $socialUser->getName(),
265 'email' => $socialUser->getEmail(),
266 'password' => Str::random(30)
268 return $this->registerUser($userData, $socialAccount, $emailVerified);