]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/RegisterController.php
Completion of Persian translation 2022-08-10
[bookstack] / app / Http / Controllers / Auth / RegisterController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Auth\User;
9 use BookStack\Exceptions\StoppedAuthenticationException;
10 use BookStack\Exceptions\UserRegistrationException;
11 use BookStack\Http\Controllers\Controller;
12 use Illuminate\Foundation\Auth\RegistersUsers;
13 use Illuminate\Http\Request;
14 use Illuminate\Support\Facades\Hash;
15 use Illuminate\Support\Facades\Validator;
16 use Illuminate\Validation\Rules\Password;
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     protected $loginService;
36
37     /**
38      * Where to redirect users after login / registration.
39      *
40      * @var string
41      */
42     protected $redirectTo = '/';
43     protected $redirectPath = '/';
44
45     /**
46      * Create a new controller instance.
47      */
48     public function __construct(
49         SocialAuthService $socialAuthService,
50         RegistrationService $registrationService,
51         LoginService $loginService
52     ) {
53         $this->middleware('guest');
54         $this->middleware('guard:standard');
55
56         $this->socialAuthService = $socialAuthService;
57         $this->registrationService = $registrationService;
58         $this->loginService = $loginService;
59
60         $this->redirectTo = url('/');
61         $this->redirectPath = url('/');
62     }
63
64     /**
65      * Get a validator for an incoming registration request.
66      *
67      * @return \Illuminate\Contracts\Validation\Validator
68      */
69     protected function validator(array $data)
70     {
71         return Validator::make($data, [
72             'name'     => ['required', 'min:2', 'max:255'],
73             'email'    => ['required', 'email', 'max:255', 'unique:users'],
74             'password' => ['required', Password::default()],
75         ]);
76     }
77
78     /**
79      * Show the application registration form.
80      *
81      * @throws UserRegistrationException
82      */
83     public function getRegister()
84     {
85         $this->registrationService->ensureRegistrationAllowed();
86         $socialDrivers = $this->socialAuthService->getActiveDrivers();
87
88         return view('auth.register', [
89             'socialDrivers' => $socialDrivers,
90         ]);
91     }
92
93     /**
94      * Handle a registration request for the application.
95      *
96      * @throws UserRegistrationException
97      * @throws StoppedAuthenticationException
98      */
99     public function postRegister(Request $request)
100     {
101         $this->registrationService->ensureRegistrationAllowed();
102         $this->validator($request->all())->validate();
103         $userData = $request->all();
104
105         try {
106             $user = $this->registrationService->registerUser($userData);
107             $this->loginService->login($user, auth()->getDefaultDriver());
108         } catch (UserRegistrationException $exception) {
109             if ($exception->getMessage()) {
110                 $this->showErrorNotification($exception->getMessage());
111             }
112
113             return redirect($exception->redirectLocation);
114         }
115
116         $this->showSuccessNotification(trans('auth.register_success'));
117
118         return redirect($this->redirectPath());
119     }
120
121     /**
122      * Create a new user instance after a valid registration.
123      *
124      * @param array $data
125      *
126      * @return User
127      */
128     protected function create(array $data)
129     {
130         return User::create([
131             'name'     => $data['name'],
132             'email'    => $data['email'],
133             'password' => Hash::make($data['password']),
134         ]);
135     }
136 }