3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Exceptions\ConfirmationEmailException;
6 use BookStack\Exceptions\SocialSignInException;
7 use BookStack\Exceptions\UserRegistrationException;
8 use BookStack\Repos\UserRepo;
9 use BookStack\Services\EmailConfirmationService;
10 use BookStack\Services\SocialAuthService;
13 use Illuminate\Http\Request;
14 use Illuminate\Http\Response;
16 use BookStack\Http\Controllers\Controller;
17 use Illuminate\Foundation\Auth\RegistersUsers;
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 SocialAuthService $socialAuthService
50 * @param EmailConfirmationService $emailConfirmationService
51 * @param UserRepo $userRepo
53 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
55 $this->middleware('guest')->except(['socialCallback', 'detachSocialAccount']);
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|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.
94 public function getRegister()
96 $this->checkRegistrationAllowed();
97 $socialDrivers = $this->socialAuthService->getActiveDrivers();
98 return view('auth.register', ['socialDrivers' => $socialDrivers]);
102 * Handle a registration request for the application.
103 * @param Request|\Illuminate\Http\Request $request
105 * @throws UserRegistrationException
106 * @throws \Illuminate\Foundation\Validation\ValidationException
108 public function postRegister(Request $request)
110 $this->checkRegistrationAllowed();
111 $validator = $this->validator($request->all());
113 if ($validator->fails()) {
114 $this->throwValidationException(
119 $userData = $request->all();
120 return $this->registerUser($userData);
124 * Create a new user instance after a valid registration.
128 protected function create(array $data)
130 return User::create([
131 'name' => $data['name'],
132 'email' => $data['email'],
133 'password' => bcrypt($data['password']),
138 * The registrations flow for all users.
139 * @param array $userData
140 * @param bool|false|SocialAccount $socialAccount
141 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
142 * @throws UserRegistrationException
143 * @throws ConfirmationEmailException
145 protected function registerUser(array $userData, $socialAccount = false)
147 if (setting('registration-restrict')) {
148 $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
149 $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
150 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
151 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), '/register');
155 $newUser = $this->userRepo->registerNew($userData);
156 if ($socialAccount) {
157 $newUser->socialAccounts()->save($socialAccount);
160 if (setting('registration-confirmation') || setting('registration-restrict')) {
164 $this->emailConfirmationService->sendConfirmation($newUser);
165 } catch (Exception $e) {
166 session()->flash('error', trans('auth.email_confirm_send_error'));
169 return redirect('/register/confirm');
172 auth()->login($newUser);
173 session()->flash('success', trans('auth.register_success'));
174 return redirect($this->redirectPath());
178 * Show the page to tell the user to check their email
179 * and confirm their address.
181 public function getRegisterConfirmation()
183 return view('auth/register-confirm');
187 * Confirms an email via a token and logs the user into the system.
189 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
190 * @throws UserRegistrationException
192 public function confirmEmail($token)
194 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
195 $user = $confirmation->user;
196 $user->email_confirmed = true;
198 auth()->login($user);
199 session()->flash('success', trans('auth.email_confirm_success'));
200 $this->emailConfirmationService->deleteConfirmationsByUser($user);
201 return redirect($this->redirectPath);
205 * Shows a notice that a user's email address has not been confirmed,
206 * Also has the option to re-send the confirmation email.
207 * @return \Illuminate\View\View
209 public function showAwaitingConfirmation()
211 return view('auth/user-unconfirmed');
215 * Resend the confirmation email
216 * @param Request $request
217 * @return \Illuminate\View\View
219 public function resendConfirmation(Request $request)
221 $this->validate($request, [
222 'email' => 'required|email|exists:users,email'
224 $user = $this->userRepo->getByEmail($request->get('email'));
227 $this->emailConfirmationService->sendConfirmation($user);
228 } catch (Exception $e) {
229 session()->flash('error', trans('auth.email_confirm_send_error'));
230 return redirect('/register/confirm');
233 $this->emailConfirmationService->sendConfirmation($user);
234 session()->flash('success', trans('auth.email_confirm_resent'));
235 return redirect('/register/confirm');
239 * Redirect to the social site for authentication intended to register.
240 * @param $socialDriver
243 public function socialRegister($socialDriver)
245 $this->checkRegistrationAllowed();
246 session()->put('social-callback', 'register');
247 return $this->socialAuthService->startRegister($socialDriver);
251 * The callback for social login services.
252 * @param $socialDriver
253 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
254 * @throws SocialSignInException
256 public function socialCallback($socialDriver)
258 if (session()->has('social-callback')) {
259 $action = session()->pull('social-callback');
260 if ($action == 'login') {
261 return $this->socialAuthService->handleLoginCallback($socialDriver);
262 } elseif ($action == 'register') {
263 return $this->socialRegisterCallback($socialDriver);
266 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
268 return redirect()->back();
272 * Detach a social account from a user.
273 * @param $socialDriver
274 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
276 public function detachSocialAccount($socialDriver)
278 return $this->socialAuthService->detachSocialAccount($socialDriver);
282 * Register a new user after a registration callback.
283 * @param $socialDriver
284 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
285 * @throws UserRegistrationException
287 protected function socialRegisterCallback($socialDriver)
289 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
290 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
292 // Create an array of the user data to create a new user instance
294 'name' => $socialUser->getName(),
295 'email' => $socialUser->getEmail(),
296 'password' => str_random(30)
298 return $this->registerUser($userData, $socialAccount);