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