]> BookStack Code Mirror - bookstack/blob - app/Access/Controllers/RegisterController.php
Adapt tests with displayName array
[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\SocialDriverManager;
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     public function __construct(
19         protected SocialDriverManager $socialDriverManager,
20         protected RegistrationService $registrationService,
21         protected LoginService $loginService
22     ) {
23         $this->middleware('guest');
24         $this->middleware('guard:standard');
25     }
26
27     /**
28      * Show the application registration form.
29      *
30      * @throws UserRegistrationException
31      */
32     public function getRegister()
33     {
34         $this->registrationService->ensureRegistrationAllowed();
35         $socialDrivers = $this->socialDriverManager->getActive();
36
37         return view('auth.register', [
38             'socialDrivers' => $socialDrivers,
39         ]);
40     }
41
42     /**
43      * Handle a registration request for the application.
44      *
45      * @throws UserRegistrationException
46      * @throws StoppedAuthenticationException
47      */
48     public function postRegister(Request $request)
49     {
50         $this->registrationService->ensureRegistrationAllowed();
51         $this->validator($request->all())->validate();
52         $userData = $request->all();
53
54         try {
55             $user = $this->registrationService->registerUser($userData);
56             $this->loginService->login($user, auth()->getDefaultDriver());
57         } catch (UserRegistrationException $exception) {
58             if ($exception->getMessage()) {
59                 $this->showErrorNotification($exception->getMessage());
60             }
61
62             return redirect($exception->redirectLocation);
63         }
64
65         $this->showSuccessNotification(trans('auth.register_success'));
66
67         return redirect('/');
68     }
69
70     /**
71      * Get a validator for an incoming registration request.
72      */
73     protected function validator(array $data): ValidatorContract
74     {
75         return Validator::make($data, [
76             'name'     => ['required', 'min:2', 'max:100'],
77             'email'    => ['required', 'email', 'max:255', 'unique:users'],
78             'password' => ['required', Password::default()],
79             // Basic honey for bots that must not be filled in
80             'username' => ['prohibited'],
81         ]);
82     }
83 }