]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
Started moving MFA and email confirmation to new login flow
[bookstack] / app / Http / Controllers / Auth / RegisterController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Auth\User;
9 use BookStack\Exceptions\UserRegistrationException;
10 use BookStack\Http\Controllers\Controller;
11 use Illuminate\Foundation\Auth\RegistersUsers;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\Facades\Hash;
14 use Validator;
15
16 class RegisterController extends Controller
17 {
18     /*
19     |--------------------------------------------------------------------------
20     | Register Controller
21     |--------------------------------------------------------------------------
22     |
23     | This controller handles the registration of new users as well as their
24     | validation and creation. By default this controller uses a trait to
25     | provide this functionality without requiring any additional code.
26     |
27     */
28
29     use RegistersUsers;
30
31     protected $socialAuthService;
32     protected $registrationService;
33     protected $loginService;
34
35     /**
36      * Where to redirect users after login / registration.
37      *
38      * @var string
39      */
40     protected $redirectTo = '/';
41     protected $redirectPath = '/';
42
43     /**
44      * Create a new controller instance.
45      */
46     public function __construct(
47         SocialAuthService $socialAuthService,
48         RegistrationService $registrationService,
49         LoginService $loginService
50     )
51     {
52         $this->middleware('guest');
53         $this->middleware('guard:standard');
54
55         $this->socialAuthService = $socialAuthService;
56         $this->registrationService = $registrationService;
57         $this->loginService = $loginService;
58
59         $this->redirectTo = url('/');
60         $this->redirectPath = url('/');
61     }
62
63     /**
64      * Get a validator for an incoming registration request.
65      *
66      * @return \Illuminate\Contracts\Validation\Validator
67      */
68     protected function validator(array $data)
69     {
70         return Validator::make($data, [
71             'name'     => 'required|min:2|max:255',
72             'email'    => 'required|email|max:255|unique:users',
73             'password' => 'required|min:8',
74         ]);
75     }
76
77     /**
78      * Show the application registration form.
79      *
80      * @throws UserRegistrationException
81      */
82     public function getRegister()
83     {
84         $this->registrationService->ensureRegistrationAllowed();
85         $socialDrivers = $this->socialAuthService->getActiveDrivers();
86
87         return view('auth.register', [
88             'socialDrivers' => $socialDrivers,
89         ]);
90     }
91
92     /**
93      * Handle a registration request for the application.
94      *
95      * @throws UserRegistrationException
96      */
97     public function postRegister(Request $request)
98     {
99         $this->registrationService->ensureRegistrationAllowed();
100         $this->validator($request->all())->validate();
101         $userData = $request->all();
102
103         try {
104             $user = $this->registrationService->registerUser($userData);
105             $this->loginService->login($user, auth()->getDefaultDriver());
106         } catch (UserRegistrationException $exception) {
107             if ($exception->getMessage()) {
108                 $this->showErrorNotification($exception->getMessage());
109             }
110
111             return redirect($exception->redirectLocation);
112         }
113
114         $this->showSuccessNotification(trans('auth.register_success'));
115
116         return redirect($this->redirectPath());
117     }
118
119     /**
120      * Create a new user instance after a valid registration.
121      *
122      * @param array $data
123      *
124      * @return User
125      */
126     protected function create(array $data)
127     {
128         return User::create([
129             'name'     => $data['name'],
130             'email'    => $data['email'],
131             'password' => Hash::make($data['password']),
132         ]);
133     }
134 }