]> BookStack Code Mirror - bookstack/blob - app/Access/Controllers/RegisterController.php
3c653a073a3b7b753c17b6e656565af04ebb2d15
[bookstack] / app / Access / Controllers / RegisterController.php
1 <?php
2
3 namespace BookStack\Access\Controllers;
4
5 use BookStack\Access\LoginService;
6 use BookStack\Access\RegistrationService;
7 use BookStack\Access\SocialAuthService;
8 use BookStack\Exceptions\StoppedAuthenticationException;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Http\Controller;
11 use Illuminate\Contracts\Validation\Validator as ValidatorContract;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\Facades\Validator;
14 use Illuminate\Validation\Rules\Password;
15
16 class RegisterController extends Controller
17 {
18     protected SocialAuthService $socialAuthService;
19     protected RegistrationService $registrationService;
20     protected LoginService $loginService;
21
22     /**
23      * Create a new controller instance.
24      */
25     public function __construct(
26         SocialAuthService $socialAuthService,
27         RegistrationService $registrationService,
28         LoginService $loginService
29     ) {
30         $this->middleware('guest');
31         $this->middleware('guard:standard');
32
33         $this->socialAuthService = $socialAuthService;
34         $this->registrationService = $registrationService;
35         $this->loginService = $loginService;
36     }
37
38     /**
39      * Show the application registration form.
40      *
41      * @throws UserRegistrationException
42      */
43     public function getRegister()
44     {
45         $this->registrationService->ensureRegistrationAllowed();
46         $socialDrivers = $this->socialAuthService->getActiveDrivers();
47
48         return view('auth.register', [
49             'socialDrivers' => $socialDrivers,
50         ]);
51     }
52
53     /**
54      * Handle a registration request for the application.
55      *
56      * @throws UserRegistrationException
57      * @throws StoppedAuthenticationException
58      */
59     public function postRegister(Request $request)
60     {
61         $this->registrationService->ensureRegistrationAllowed();
62         $this->validator($request->all())->validate();
63         $userData = $request->all();
64
65         try {
66             $user = $this->registrationService->registerUser($userData);
67             $this->loginService->login($user, auth()->getDefaultDriver());
68         } catch (UserRegistrationException $exception) {
69             if ($exception->getMessage()) {
70                 $this->showErrorNotification($exception->getMessage());
71             }
72
73             return redirect($exception->redirectLocation);
74         }
75
76         $this->showSuccessNotification(trans('auth.register_success'));
77
78         return redirect('/');
79     }
80
81     /**
82      * Get a validator for an incoming registration request.
83      */
84     protected function validator(array $data): ValidatorContract
85     {
86         return Validator::make($data, [
87             'name'     => ['required', 'min:2', 'max:100'],
88             'email'    => ['required', 'email', 'max:255', 'unique:users'],
89             'password' => ['required', Password::default()],
90         ]);
91     }
92 }