]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
7d7d8732b5f1e37d1177b230f8229b9d65b9c07f
[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      * @throws UserRegistrationException
76      */
77     public function getRegister()
78     {
79         $this->registrationService->ensureRegistrationAllowed();
80         $socialDrivers = $this->socialAuthService->getActiveDrivers();
81         return view('auth.register', [
82             'socialDrivers' => $socialDrivers,
83         ]);
84     }
85
86     /**
87      * Handle a registration request for the application.
88      * @throws UserRegistrationException
89      */
90     public function postRegister(Request $request)
91     {
92         $this->registrationService->ensureRegistrationAllowed();
93         $this->validator($request->all())->validate();
94         $userData = $request->all();
95
96         try {
97             $user = $this->registrationService->registerUser($userData);
98             auth()->login($user);
99             Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
100             $this->logActivity(ActivityType::AUTH_LOGIN, $user);
101         } catch (UserRegistrationException $exception) {
102             if ($exception->getMessage()) {
103                 $this->showErrorNotification($exception->getMessage());
104             }
105             return redirect($exception->redirectLocation);
106         }
107
108         $this->showSuccessNotification(trans('auth.register_success'));
109         return redirect($this->redirectPath());
110     }
111
112     /**
113      * Create a new user instance after a valid registration.
114      * @param  array  $data
115      * @return User
116      */
117     protected function create(array $data)
118     {
119         return User::create([
120             'name' => $data['name'],
121             'email' => $data['email'],
122             'password' => Hash::make($data['password']),
123         ]);
124     }
125 }