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