3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Exceptions\ConfirmationEmailException;
6 use BookStack\Exceptions\UserRegistrationException;
7 use BookStack\Repos\UserRepo;
8 use BookStack\Services\EmailConfirmationService;
9 use BookStack\Services\SocialAuthService;
12 use Illuminate\Http\Request;
13 use Illuminate\Http\Response;
15 use BookStack\Http\Controllers\Controller;
16 use Illuminate\Foundation\Auth\RegistersUsers;
18 class RegisterController extends Controller
21 |--------------------------------------------------------------------------
23 |--------------------------------------------------------------------------
25 | This controller handles the registration of new users as well as their
26 | validation and creation. By default this controller uses a trait to
27 | provide this functionality without requiring any additional code.
33 protected $socialAuthService;
34 protected $emailConfirmationService;
38 * Where to redirect users after login / registration.
42 protected $redirectTo = '/';
43 protected $redirectPath = '/';
46 * Create a new controller instance.
48 * @param SocialAuthService $socialAuthService
49 * @param EmailConfirmationService $emailConfirmationService
50 * @param UserRepo $userRepo
52 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
54 $this->middleware('guest')->except(['socialCallback', 'detachSocialAccount']);
55 $this->socialAuthService = $socialAuthService;
56 $this->emailConfirmationService = $emailConfirmationService;
57 $this->userRepo = $userRepo;
58 $this->redirectTo = baseUrl('/');
59 $this->redirectPath = baseUrl('/');
60 parent::__construct();
64 * Get a validator for an incoming registration request.
67 * @return \Illuminate\Contracts\Validation\Validator
69 protected function validator(array $data)
71 return Validator::make($data, [
72 'name' => 'required|max:255',
73 'email' => 'required|email|max:255|unique:users',
74 'password' => 'required|min:6',
79 * Check whether or not registrations are allowed in the app settings.
80 * @throws UserRegistrationException
82 protected function checkRegistrationAllowed()
84 if (!setting('registration-enabled')) {
85 throw new UserRegistrationException('Registrations are currently disabled.', '/login');
90 * Show the application registration form.
93 public function getRegister()
95 $this->checkRegistrationAllowed();
96 $socialDrivers = $this->socialAuthService->getActiveDrivers();
97 return view('auth.register', ['socialDrivers' => $socialDrivers]);
101 * Handle a registration request for the application.
102 * @param Request|\Illuminate\Http\Request $request
104 * @throws UserRegistrationException
105 * @throws \Illuminate\Foundation\Validation\ValidationException
107 public function postRegister(Request $request)
109 $this->checkRegistrationAllowed();
110 $validator = $this->validator($request->all());
112 if ($validator->fails()) {
113 $this->throwValidationException(
118 $userData = $request->all();
119 return $this->registerUser($userData);
123 * Create a new user instance after a valid registration.
127 protected function create(array $data)
129 return User::create([
130 'name' => $data['name'],
131 'email' => $data['email'],
132 'password' => bcrypt($data['password']),
137 * The registrations flow for all users.
138 * @param array $userData
139 * @param bool|false|SocialAccount $socialAccount
140 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
141 * @throws UserRegistrationException
142 * @throws ConfirmationEmailException
144 protected function registerUser(array $userData, $socialAccount = false)
146 if (setting('registration-restrict')) {
147 $restrictedEmailDomains = explode(',', str_replace(' ', '', setting('registration-restrict')));
148 $userEmailDomain = $domain = substr(strrchr($userData['email'], "@"), 1);
149 if (!in_array($userEmailDomain, $restrictedEmailDomains)) {
150 throw new UserRegistrationException('That email domain does not have access to this application', '/register');
154 $newUser = $this->userRepo->registerNew($userData);
155 if ($socialAccount) {
156 $newUser->socialAccounts()->save($socialAccount);
159 if (setting('registration-confirmation') || setting('registration-restrict')) {
163 $this->emailConfirmationService->sendConfirmation($newUser);
164 } catch (Exception $e) {
165 session()->flash('error', trans('auth.email_confirm_send_error'));
168 return redirect('/register/confirm');
171 auth()->login($newUser);
172 session()->flash('success', 'Thanks for signing up! You are now registered and signed in.');
173 return redirect($this->redirectPath());
177 * Show the page to tell the user to check their email
178 * and confirm their address.
180 public function getRegisterConfirmation()
182 return view('auth/register-confirm');
186 * Confirms an email via a token and logs the user into the system.
188 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
189 * @throws UserRegistrationException
191 public function confirmEmail($token)
193 $confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
194 $user = $confirmation->user;
195 $user->email_confirmed = true;
197 auth()->login($user);
198 session()->flash('success', trans('auth.email_confirm_success'));
199 $this->emailConfirmationService->deleteConfirmationsByUser($user);
200 return redirect($this->redirectPath);
204 * Shows a notice that a user's email address has not been confirmed,
205 * Also has the option to re-send the confirmation email.
206 * @return \Illuminate\View\View
208 public function showAwaitingConfirmation()
210 return view('auth/user-unconfirmed');
214 * Resend the confirmation email
215 * @param Request $request
216 * @return \Illuminate\View\View
218 public function resendConfirmation(Request $request)
220 $this->validate($request, [
221 'email' => 'required|email|exists:users,email'
223 $user = $this->userRepo->getByEmail($request->get('email'));
226 $this->emailConfirmationService->sendConfirmation($user);
227 } catch (Exception $e) {
228 session()->flash('error', trans('auth.email_confirm_send_error'));
229 return redirect('/register/confirm');
232 $this->emailConfirmationService->sendConfirmation($user);
233 session()->flash('success', trans('auth.email_confirm_resent'));
234 return redirect('/register/confirm');
238 * Redirect to the social site for authentication intended to register.
239 * @param $socialDriver
242 public function socialRegister($socialDriver)
244 $this->checkRegistrationAllowed();
245 session()->put('social-callback', 'register');
246 return $this->socialAuthService->startRegister($socialDriver);
250 * The callback for social login services.
251 * @param $socialDriver
252 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
253 * @throws SocialSignInException
255 public function socialCallback($socialDriver)
257 if (session()->has('social-callback')) {
258 $action = session()->pull('social-callback');
259 if ($action == 'login') {
260 return $this->socialAuthService->handleLoginCallback($socialDriver);
261 } elseif ($action == 'register') {
262 return $this->socialRegisterCallback($socialDriver);
265 throw new SocialSignInException('No action defined', '/login');
267 return redirect()->back();
271 * Detach a social account from a user.
272 * @param $socialDriver
273 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
275 public function detachSocialAccount($socialDriver)
277 return $this->socialAuthService->detachSocialAccount($socialDriver);
281 * Register a new user after a registration callback.
282 * @param $socialDriver
283 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
284 * @throws UserRegistrationException
286 protected function socialRegisterCallback($socialDriver)
288 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver);
289 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
291 // Create an array of the user data to create a new user instance
293 'name' => $socialUser->getName(),
294 'email' => $socialUser->getEmail(),
295 'password' => str_random(30)
297 return $this->registerUser($userData, $socialAccount);