3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\SocialAccount;
6 use BookStack\Auth\User;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Exceptions\SocialSignInAccountNotUsed;
9 use BookStack\Exceptions\SocialSignInException;
10 use BookStack\Exceptions\UserRegistrationException;
11 use BookStack\Http\Controllers\Controller;
13 use Illuminate\Foundation\Auth\RegistersUsers;
14 use Illuminate\Http\Request;
15 use Illuminate\Http\Response;
16 use Laravel\Socialite\Contracts\User as SocialUser;
19 class RegisterController extends Controller
22 |--------------------------------------------------------------------------
24 |--------------------------------------------------------------------------
26 | This controller handles the registration of new users as well as their
27 | validation and creation. By default this controller uses a trait to
28 | provide this functionality without requiring any additional code.
34 protected $socialAuthService;
35 protected $emailConfirmationService;
39 * Where to redirect users after login / registration.
43 protected $redirectTo = '/';
44 protected $redirectPath = '/';
47 * Create a new controller instance.
49 * @param \BookStack\Auth\Access\SocialAuthService $socialAuthService
50 * @param \BookStack\Auth\EmailConfirmationService $emailConfirmationService
51 * @param \BookStack\Auth\UserRepo $userRepo
53 public function __construct(\BookStack\Auth\Access\SocialAuthService $socialAuthService, \BookStack\Auth\Access\EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
55 $this->middleware('guest')->only(['getRegister', 'postRegister', 'socialRegister']);
56 $this->socialAuthService = $socialAuthService;
57 $this->emailConfirmationService = $emailConfirmationService;
58 $this->userRepo = $userRepo;
59 $this->redirectTo = baseUrl('/');
60 $this->redirectPath = baseUrl('/');
61 parent::__construct();
65 * Get a validator for an incoming registration request.
68 * @return \Illuminate\Contracts\Validation\Validator
70 protected function validator(array $data)
72 return Validator::make($data, [
73 'name' => 'required|min:2|max:255',
74 'email' => 'required|email|max:255|unique:users',
75 'password' => 'required|min:6',
80 * Check whether or not registrations are allowed in the app settings.
81 * @throws UserRegistrationException
83 protected function checkRegistrationAllowed()
85 if (!setting('registration-enabled')) {
86 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
91 * Show the application registration form.
93 * @throws UserRegistrationException
95 public function getRegister()
97 $this->checkRegistrationAllowed();
98 $socialDrivers = $this->socialAuthService->getActiveDrivers();
99 return view('auth.register', ['socialDrivers' => $socialDrivers]);
103 * Handle a registration request for the application.
104 * @param Request|\Illuminate\Http\Request $request
105 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
106 * @throws UserRegistrationException
108 public function postRegister(Request $request)
110 $this->checkRegistrationAllowed();
111 $this->validator($request->all())->validate();
113 $userData = $request->all();
114 return $this->registerUser($userData);
118 * Create a new user instance after a valid registration.
120 * @return \BookStack\Auth\User
122 protected function create(array $data)
124 return User::create([
125 'name' => $data['name'],
126 'email' => $data['email'],
127 'password' => bcrypt($data['password']),
132 * The registrations flow for all users.
133 * @param array $userData
134 * @param bool|false|SocialAccount $socialAccount
135 * @param bool $emailVerified
136 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
137 * @throws UserRegistrationException
139 protected function registerUser(array $userData, $socialAccount = false, $emailVerified = false)
141 $registrationRestrict = setting('registration-restrict');
143 if ($registrationRestrict) {
144 $restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
145 $userEmailDomain = $domain = mb_substr(mb_strrchr($userData['email'], "@"), 1);
146 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
147 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), '/register');
151 $newUser = $this->userRepo->registerNew($userData, $emailVerified);
152 if ($socialAccount) {
153 $newUser->socialAccounts()->save($socialAccount);
156 if ((setting('registration-confirmation') || $registrationRestrict) && !$emailVerified) {
160 $this->emailConfirmationService->sendConfirmation($newUser);
161 } catch (Exception $e) {
162 session()->flash('error', trans('auth.email_confirm_send_error'));
165 return redirect('/register/confirm');
168 auth()->login($newUser);
169 session()->flash('success', trans('auth.register_success'));
170 return redirect($this->redirectPath());
174 * Show the page to tell the user to check their email
175 * and confirm their address.
177 public function getRegisterConfirmation()
179 return view('auth.register-confirm');
183 * Confirms an email via a token and logs the user into the system.
185 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
186 * @throws UserRegistrationException
188 public function confirmEmail($token)
190 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
191 $user = $confirmation->user;
192 $user->email_confirmed = true;
194 auth()->login($user);
195 session()->flash('success', trans('auth.email_confirm_success'));
196 $this->emailConfirmationService->deleteConfirmationsByUser($user);
197 return redirect($this->redirectPath);
201 * Shows a notice that a user's email address has not been confirmed,
202 * Also has the option to re-send the confirmation email.
203 * @return \Illuminate\View\View
205 public function showAwaitingConfirmation()
207 return view('auth.user-unconfirmed');
211 * Resend the confirmation email
212 * @param Request $request
213 * @return \Illuminate\View\View
215 public function resendConfirmation(Request $request)
217 $this->validate($request, [
218 'email' => 'required|email|exists:users,email'
220 $user = $this->userRepo->getByEmail($request->get('email'));
223 $this->emailConfirmationService->sendConfirmation($user);
224 } catch (Exception $e) {
225 session()->flash('error', trans('auth.email_confirm_send_error'));
226 return redirect('/register/confirm');
229 session()->flash('success', trans('auth.email_confirm_resent'));
230 return redirect('/register/confirm');
234 * Redirect to the social site for authentication intended to register.
235 * @param $socialDriver
237 * @throws UserRegistrationException
238 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
240 public function socialRegister($socialDriver)
242 $this->checkRegistrationAllowed();
243 session()->put('social-callback', 'register');
244 return $this->socialAuthService->startRegister($socialDriver);
248 * The callback for social login services.
249 * @param $socialDriver
250 * @param Request $request
251 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
252 * @throws SocialSignInException
253 * @throws UserRegistrationException
254 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
256 public function socialCallback($socialDriver, Request $request)
258 if (!session()->has('social-callback')) {
259 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
262 // Check request for error information
263 if ($request->has('error') && $request->has('error_description')) {
264 throw new SocialSignInException(trans('errors.social_login_bad_response', [
265 'socialAccount' => $socialDriver,
266 'error' => $request->get('error_description'),
270 $action = session()->pull('social-callback');
272 // Attempt login or fall-back to register if allowed.
273 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
274 if ($action == 'login') {
276 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
277 } catch (SocialSignInAccountNotUsed $exception) {
278 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
279 return $this->socialRegisterCallback($socialDriver, $socialUser);
285 if ($action == 'register') {
286 return $this->socialRegisterCallback($socialDriver, $socialUser);
289 return redirect()->back();
293 * Detach a social account from a user.
294 * @param $socialDriver
295 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
297 public function detachSocialAccount($socialDriver)
299 return $this->socialAuthService->detachSocialAccount($socialDriver);
303 * Register a new user after a registration callback.
304 * @param string $socialDriver
305 * @param SocialUser $socialUser
306 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
307 * @throws UserRegistrationException
309 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
311 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
312 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
313 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
315 // Create an array of the user data to create a new user instance
317 'name' => $socialUser->getName(),
318 'email' => $socialUser->getEmail(),
319 'password' => str_random(30)
321 return $this->registerUser($userData, $socialAccount, $emailVerified);