]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
Merge pull request #2890 from BookStackApp/analysis-kabPRy
[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         $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      * @throws StoppedAuthenticationException
97      */
98     public function postRegister(Request $request)
99     {
100         $this->registrationService->ensureRegistrationAllowed();
101         $this->validator($request->all())->validate();
102         $userData = $request->all();
103
104         try {
105             $user = $this->registrationService->registerUser($userData);
106             $this->loginService->login($user, auth()->getDefaultDriver());
107         } catch (UserRegistrationException $exception) {
108             if ($exception->getMessage()) {
109                 $this->showErrorNotification($exception->getMessage());
110             }
111
112             return redirect($exception->redirectLocation);
113         }
114
115         $this->showSuccessNotification(trans('auth.register_success'));
116
117         return redirect($this->redirectPath());
118     }
119
120     /**
121      * Create a new user instance after a valid registration.
122      *
123      * @param array $data
124      *
125      * @return User
126      */
127     protected function create(array $data)
128     {
129         return User::create([
130             'name'     => $data['name'],
131             'email'    => $data['email'],
132             'password' => Hash::make($data['password']),
133         ]);
134     }
135 }