]> 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         parent::__construct();
55     }
56
57     /**
58      * Get a validator for an incoming registration request.
59      *
60      * @return \Illuminate\Contracts\Validation\Validator
61      */
62     protected function validator(array $data)
63     {
64         return Validator::make($data, [
65             'name' => 'required|min:2|max:255',
66             'email' => 'required|email|max:255|unique:users',
67             'password' => 'required|min:8',
68         ]);
69     }
70
71     /**
72      * Show the application registration form.
73      * @throws UserRegistrationException
74      */
75     public function getRegister()
76     {
77         $this->registrationService->ensureRegistrationAllowed();
78         $socialDrivers = $this->socialAuthService->getActiveDrivers();
79         return view('auth.register', [
80             'socialDrivers' => $socialDrivers,
81         ]);
82     }
83
84     /**
85      * Handle a registration request for the application.
86      * @throws UserRegistrationException
87      */
88     public function postRegister(Request $request)
89     {
90         $this->registrationService->ensureRegistrationAllowed();
91         $this->validator($request->all())->validate();
92         $userData = $request->all();
93
94         try {
95             $user = $this->registrationService->registerUser($userData);
96             auth()->login($user);
97         } catch (UserRegistrationException $exception) {
98             if ($exception->getMessage()) {
99                 $this->showErrorNotification($exception->getMessage());
100             }
101             return redirect($exception->redirectLocation);
102         }
103
104         $this->showSuccessNotification(trans('auth.register_success'));
105         return redirect($this->redirectPath());
106     }
107
108     /**
109      * Create a new user instance after a valid registration.
110      * @param  array  $data
111      * @return User
112      */
113     protected function create(array $data)
114     {
115         return User::create([
116             'name' => $data['name'],
117             'email' => $data['email'],
118             'password' => Hash::make($data['password']),
119         ]);
120     }
121
122 }