3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\EmailConfirmationService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Auth\SocialAccount;
8 use BookStack\Auth\User;
9 use BookStack\Auth\UserRepo;
10 use BookStack\Exceptions\SocialDriverNotConfigured;
11 use BookStack\Exceptions\SocialSignInAccountNotUsed;
12 use BookStack\Exceptions\SocialSignInException;
13 use BookStack\Exceptions\UserRegistrationException;
14 use BookStack\Http\Controllers\Controller;
16 use Illuminate\Foundation\Auth\RegistersUsers;
17 use Illuminate\Http\RedirectResponse;
18 use Illuminate\Http\Request;
19 use Illuminate\Http\Response;
20 use Illuminate\Routing\Redirector;
21 use Illuminate\Support\Facades\Hash;
22 use Laravel\Socialite\Contracts\User as SocialUser;
25 class RegisterController extends Controller
28 |--------------------------------------------------------------------------
30 |--------------------------------------------------------------------------
32 | This controller handles the registration of new users as well as their
33 | validation and creation. By default this controller uses a trait to
34 | provide this functionality without requiring any additional code.
40 protected $socialAuthService;
41 protected $emailConfirmationService;
45 * Where to redirect users after login / registration.
49 protected $redirectTo = '/';
50 protected $redirectPath = '/';
53 * Create a new controller instance.
55 * @param SocialAuthService $socialAuthService
56 * @param EmailConfirmationService $emailConfirmationService
57 * @param UserRepo $userRepo
59 public function __construct(SocialAuthService $socialAuthService, EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
61 $this->middleware('guest')->only(['getRegister', 'postRegister', 'socialRegister']);
62 $this->socialAuthService = $socialAuthService;
63 $this->emailConfirmationService = $emailConfirmationService;
64 $this->userRepo = $userRepo;
65 $this->redirectTo = url('/');
66 $this->redirectPath = url('/');
67 parent::__construct();
71 * Get a validator for an incoming registration request.
74 * @return \Illuminate\Contracts\Validation\Validator
76 protected function validator(array $data)
78 return Validator::make($data, [
79 'name' => 'required|min:2|max:255',
80 'email' => 'required|email|max:255|unique:users',
81 'password' => 'required|min:6',
86 * Check whether or not registrations are allowed in the app settings.
87 * @throws UserRegistrationException
89 protected function checkRegistrationAllowed()
91 if (!setting('registration-enabled')) {
92 throw new UserRegistrationException(trans('auth.registrations_disabled'), '/login');
97 * Show the application registration form.
99 * @throws UserRegistrationException
101 public function getRegister()
103 $this->checkRegistrationAllowed();
104 $socialDrivers = $this->socialAuthService->getActiveDrivers();
105 return view('auth.register', ['socialDrivers' => $socialDrivers]);
109 * Handle a registration request for the application.
110 * @param Request|Request $request
111 * @return RedirectResponse|Redirector
112 * @throws UserRegistrationException
114 public function postRegister(Request $request)
116 $this->checkRegistrationAllowed();
117 $this->validator($request->all())->validate();
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' => Hash::make($data['password']),
138 * The registrations flow for all users.
139 * @param array $userData
140 * @param bool|false|SocialAccount $socialAccount
141 * @param bool $emailVerified
142 * @return RedirectResponse|Redirector
143 * @throws UserRegistrationException
145 protected function registerUser(array $userData, $socialAccount = false, $emailVerified = false)
147 $registrationRestrict = setting('registration-restrict');
149 if ($registrationRestrict) {
150 $restrictedEmailDomains = explode(',', str_replace(' ', '', $registrationRestrict));
151 $userEmailDomain = $domain = mb_substr(mb_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, $emailVerified);
158 if ($socialAccount) {
159 $newUser->socialAccounts()->save($socialAccount);
162 if ($this->emailConfirmationService->confirmationRequired() && !$emailVerified) {
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 * Redirect to the social site for authentication intended to register.
181 * @param $socialDriver
183 * @throws UserRegistrationException
184 * @throws SocialDriverNotConfigured
186 public function socialRegister($socialDriver)
188 $this->checkRegistrationAllowed();
189 session()->put('social-callback', 'register');
190 return $this->socialAuthService->startRegister($socialDriver);
194 * The callback for social login services.
195 * @param $socialDriver
196 * @param Request $request
197 * @return RedirectResponse|Redirector
198 * @throws SocialSignInException
199 * @throws UserRegistrationException
200 * @throws SocialDriverNotConfigured
202 public function socialCallback($socialDriver, Request $request)
204 if (!session()->has('social-callback')) {
205 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
208 // Check request for error information
209 if ($request->has('error') && $request->has('error_description')) {
210 throw new SocialSignInException(trans('errors.social_login_bad_response', [
211 'socialAccount' => $socialDriver,
212 'error' => $request->get('error_description'),
216 $action = session()->pull('social-callback');
218 // Attempt login or fall-back to register if allowed.
219 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
220 if ($action == 'login') {
222 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
223 } catch (SocialSignInAccountNotUsed $exception) {
224 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
225 return $this->socialRegisterCallback($socialDriver, $socialUser);
231 if ($action == 'register') {
232 return $this->socialRegisterCallback($socialDriver, $socialUser);
235 return redirect()->back();
239 * Detach a social account from a user.
240 * @param $socialDriver
241 * @return RedirectResponse|Redirector
243 public function detachSocialAccount($socialDriver)
245 return $this->socialAuthService->detachSocialAccount($socialDriver);
249 * Register a new user after a registration callback.
250 * @param string $socialDriver
251 * @param SocialUser $socialUser
252 * @return RedirectResponse|Redirector
253 * @throws UserRegistrationException
255 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
257 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
258 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
259 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
261 // Create an array of the user data to create a new user instance
263 'name' => $socialUser->getName(),
264 'email' => $socialUser->getEmail(),
265 'password' => str_random(30)
267 return $this->registerUser($userData, $socialAccount, $emailVerified);