]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
Moved socal auth routes to their own controller
[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')->only(['getRegister', 'postRegister']);
47
48         $this->socialAuthService = $socialAuthService;
49         $this->registrationService = $registrationService;
50
51         $this->redirectTo = url('/');
52         $this->redirectPath = url('/');
53         parent::__construct();
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->checkRegistrationAllowed();
77         $socialDrivers = $this->socialAuthService->getActiveDrivers();
78         $samlEnabled = (config('saml2.enabled') === true) && (config('saml2.auto_register') === true);
79         return view('auth.register', [
80             'socialDrivers' => $socialDrivers,
81             'samlEnabled' => $samlEnabled,
82         ]);
83     }
84
85     /**
86      * Handle a registration request for the application.
87      * @throws UserRegistrationException
88      */
89     public function postRegister(Request $request)
90     {
91         $this->registrationService->checkRegistrationAllowed();
92         $this->validator($request->all())->validate();
93         $userData = $request->all();
94
95         try {
96             $this->registrationService->registerUser($userData);
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 }