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;
11 use BookStack\SocialAccount;
14 use Illuminate\Http\Request;
15 use Illuminate\Http\Response;
17 use BookStack\Http\Controllers\Controller;
18 use Illuminate\Foundation\Auth\RegistersUsers;
20 class RegisterController extends Controller
23 |--------------------------------------------------------------------------
25 |--------------------------------------------------------------------------
27 | This controller handles the registration of new users as well as their
28 | validation and creation. By default this controller uses a trait to
29 | provide this functionality without requiring any additional code.
35 protected $socialAuthService;
36 protected $emailConfirmationService;
40 * Where to redirect users after login / registration.
44 protected $redirectTo = '/';
45 protected $redirectPath = '/';
48 * Create a new controller instance.
50 * @param SocialAuthService $socialAuthService
51 * @param EmailConfirmationService $emailConfirmationService
52 * @param UserRepo $userRepo
54 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
56 $this->middleware('guest')->only(['getRegister', 'postRegister', 'socialRegister']);
57 $this->socialAuthService = $socialAuthService;
58 $this->emailConfirmationService = $emailConfirmationService;
59 $this->userRepo = $userRepo;
60 $this->redirectTo = baseUrl('/');
61 $this->redirectPath = baseUrl('/');
62 parent::__construct();
66 * Get a validator for an incoming registration request.
69 * @return \Illuminate\Contracts\Validation\Validator
71 protected function validator(array $data)
73 return Validator::make($data, [
74 'name' => 'required|max:255',
75 'email' => 'required|email|max:255|unique:users',
76 'password' => 'required|min:6',
81 * Check whether or not registrations are allowed in the app settings.
82 * @throws UserRegistrationException
84 protected function checkRegistrationAllowed()
86 if (!setting('registration-enabled')) {
87 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
92 * Show the application registration form.
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
106 * @throws UserRegistrationException
107 * @throws \Illuminate\Validation\ValidationException
109 public function postRegister(Request $request)
111 $this->checkRegistrationAllowed();
112 $validator = $this->validator($request->all());
114 if ($validator->fails()) {
115 $this->throwValidationException(
121 $userData = $request->all();
122 return $this->registerUser($userData);
126 * Create a new user instance after a valid registration.
130 protected function create(array $data)
132 return User::create([
133 'name' => $data['name'],
134 'email' => $data['email'],
135 'password' => bcrypt($data['password']),
140 * The registrations flow for all users.
141 * @param array $userData
142 * @param bool|false|SocialAccount $socialAccount
143 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
144 * @throws UserRegistrationException
145 * @throws ConfirmationEmailException
147 protected function registerUser(array $userData, $socialAccount = false)
149 if (setting('registration-restrict')) {
150 $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
151 $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
152 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
153 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), '/register');
157 $newUser = $this->userRepo->registerNew($userData);
158 if ($socialAccount) {
159 $newUser->socialAccounts()->save($socialAccount);
162 if (setting('registration-confirmation') || setting('registration-restrict')) {
166 $this->emailConfirmationService->sendConfirmation($newUser);
167 } catch (Exception $e) {
168 session()->flash('error', trans('auth.email_confirm_send_error'));
171 return redirect('/register/confirm');
174 auth()->login($newUser);
175 session()->flash('success', trans('auth.register_success'));
176 return redirect($this->redirectPath());
180 * Show the page to tell the user to check their email
181 * and confirm their address.
183 public function getRegisterConfirmation()
185 return view('auth/register-confirm');
189 * Confirms an email via a token and logs the user into the system.
191 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
192 * @throws UserRegistrationException
194 public function confirmEmail($token)
196 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
197 $user = $confirmation->user;
198 $user->email_confirmed = true;
200 auth()->login($user);
201 session()->flash('success', trans('auth.email_confirm_success'));
202 $this->emailConfirmationService->deleteConfirmationsByUser($user);
203 return redirect($this->redirectPath);
207 * Shows a notice that a user's email address has not been confirmed,
208 * Also has the option to re-send the confirmation email.
209 * @return \Illuminate\View\View
211 public function showAwaitingConfirmation()
213 return view('auth/user-unconfirmed');
217 * Resend the confirmation email
218 * @param Request $request
219 * @return \Illuminate\View\View
221 public function resendConfirmation(Request $request)
223 $this->validate($request, [
224 'email' => 'required|email|exists:users,email'
226 $user = $this->userRepo->getByEmail($request->get('email'));
229 $this->emailConfirmationService->sendConfirmation($user);
230 } catch (Exception $e) {
231 session()->flash('error', trans('auth.email_confirm_send_error'));
232 return redirect('/register/confirm');
235 session()->flash('success', trans('auth.email_confirm_resent'));
236 return redirect('/register/confirm');
240 * Redirect to the social site for authentication intended to register.
241 * @param $socialDriver
244 public function socialRegister($socialDriver)
246 $this->checkRegistrationAllowed();
247 session()->put('social-callback', 'register');
248 return $this->socialAuthService->startRegister($socialDriver);
252 * The callback for social login services.
253 * @param $socialDriver
254 * @param Request $request
255 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
256 * @throws SocialSignInException
257 * @throws UserRegistrationException
258 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
259 * @throws ConfirmationEmailException
261 public function socialCallback($socialDriver, Request $request)
263 if (!session()->has('social-callback')) {
264 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
267 // Check request for error information
268 if ($request->has('error') && $request->has('error_description')) {
269 throw new SocialSignInException(trans('errors.social_login_bad_response', [
270 'socialAccount' => $socialDriver,
271 'error' => $request->get('error_description'),
275 $action = session()->pull('social-callback');
276 if ($action == 'login') {
277 return $this->socialAuthService->handleLoginCallback($socialDriver);
279 if ($action == 'register') {
280 return $this->socialRegisterCallback($socialDriver);
282 return redirect()->back();
286 * Detach a social account from a user.
287 * @param $socialDriver
288 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
290 public function detachSocialAccount($socialDriver)
292 return $this->socialAuthService->detachSocialAccount($socialDriver);
296 * Register a new user after a registration callback.
297 * @param $socialDriver
298 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
299 * @throws ConfirmationEmailException
300 * @throws UserRegistrationException
301 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
303 protected function socialRegisterCallback($socialDriver)
305 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
306 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
308 // Create an array of the user data to create a new user instance
310 'name' => $socialUser->getName(),
311 'email' => $socialUser->getEmail(),
312 'password' => str_random(30)
314 return $this->registerUser($userData, $socialAccount);