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(
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' => bcrypt($data['password']),
139 * The registrations flow for all users.
140 * @param array $userData
141 * @param bool|false|SocialAccount $socialAccount
142 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
143 * @throws UserRegistrationException
144 * @throws ConfirmationEmailException
146 protected function registerUser(array $userData, $socialAccount = false)
148 if (setting('registration-restrict')) {
149 $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
150 $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
151 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
152 throw new UserRegistrationException(trans('auth.registration_email_domain_invalid'), '/register');
156 $newUser = $this->userRepo->registerNew($userData);
157 if ($socialAccount) {
158 $newUser->socialAccounts()->save($socialAccount);
161 if (setting('registration-confirmation') || setting('registration-restrict')) {
165 $this->emailConfirmationService->sendConfirmation($newUser);
166 } catch (Exception $e) {
167 session()->flash('error', trans('auth.email_confirm_send_error'));
170 return redirect('/register/confirm');
173 auth()->login($newUser);
174 session()->flash('success', trans('auth.register_success'));
175 return redirect($this->redirectPath());
179 * Show the page to tell the user to check their email
180 * and confirm their address.
182 public function getRegisterConfirmation()
184 return view('auth/register-confirm');
188 * Confirms an email via a token and logs the user into the system.
190 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
191 * @throws UserRegistrationException
193 public function confirmEmail($token)
195 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
196 $user = $confirmation->user;
197 $user->email_confirmed = true;
199 auth()->login($user);
200 session()->flash('success', trans('auth.email_confirm_success'));
201 $this->emailConfirmationService->deleteConfirmationsByUser($user);
202 return redirect($this->redirectPath);
206 * Shows a notice that a user's email address has not been confirmed,
207 * Also has the option to re-send the confirmation email.
208 * @return \Illuminate\View\View
210 public function showAwaitingConfirmation()
212 return view('auth/user-unconfirmed');
216 * Resend the confirmation email
217 * @param Request $request
218 * @return \Illuminate\View\View
220 public function resendConfirmation(Request $request)
222 $this->validate($request, [
223 'email' => 'required|email|exists:users,email'
225 $user = $this->userRepo->getByEmail($request->get('email'));
228 $this->emailConfirmationService->sendConfirmation($user);
229 } catch (Exception $e) {
230 session()->flash('error', trans('auth.email_confirm_send_error'));
231 return redirect('/register/confirm');
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 * @param Request $request
254 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
255 * @throws SocialSignInException
256 * @throws UserRegistrationException
257 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
258 * @throws ConfirmationEmailException
260 public function socialCallback($socialDriver, Request $request)
262 if (!session()->has('social-callback')) {
263 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
266 // Check request for error information
267 if ($request->has('error') && $request->has('error_description')) {
268 throw new SocialSignInException(trans('errors.social_login_bad_response', [
269 'socialAccount' => $socialDriver,
270 'error' => $request->get('error_description'),
274 $action = session()->pull('social-callback');
275 if ($action == 'login') return $this->socialAuthService->handleLoginCallback($socialDriver);
276 if ($action == 'register') return $this->socialRegisterCallback($socialDriver);
277 return redirect()->back();
281 * Detach a social account from a user.
282 * @param $socialDriver
283 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
285 public function detachSocialAccount($socialDriver)
287 return $this->socialAuthService->detachSocialAccount($socialDriver);
291 * Register a new user after a registration callback.
292 * @param $socialDriver
293 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
294 * @throws ConfirmationEmailException
295 * @throws UserRegistrationException
296 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
298 protected function socialRegisterCallback($socialDriver)
300 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
301 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
303 // Create an array of the user data to create a new user instance
305 'name' => $socialUser->getName(),
306 'email' => $socialUser->getEmail(),
307 'password' => str_random(30)
309 return $this->registerUser($userData, $socialAccount);