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