BookStack/app/Http/Controllers/Auth/RegisterController.php

137 lines
4.0 KiB
PHP
Raw Normal View History

<?php
2015-07-12 15:01:42 -04:00
namespace BookStack\Http\Controllers\Auth;
use BookStack\Auth\Access\LoginService;
use BookStack\Auth\Access\RegistrationService;
use BookStack\Auth\Access\SocialAuthService;
use BookStack\Auth\User;
use BookStack\Exceptions\StoppedAuthenticationException;
use BookStack\Exceptions\UserRegistrationException;
use BookStack\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
2019-09-06 17:14:39 -04:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
2015-07-12 15:01:42 -04:00
class RegisterController extends Controller
2015-07-12 15:01:42 -04:00
{
/*
|--------------------------------------------------------------------------
| Register Controller
2015-07-12 15:01:42 -04:00
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
2015-07-12 15:01:42 -04:00
|
*/
use RegistersUsers;
protected SocialAuthService $socialAuthService;
protected RegistrationService $registrationService;
protected LoginService $loginService;
2015-08-05 15:59:39 -04:00
2015-07-12 15:01:42 -04:00
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
protected $redirectPath = '/';
/**
* Create a new controller instance.
2015-07-12 15:01:42 -04:00
*/
public function __construct(
SocialAuthService $socialAuthService,
RegistrationService $registrationService,
LoginService $loginService
2021-08-21 10:49:40 -04:00
) {
$this->middleware('guest');
$this->middleware('guard:standard');
$this->socialAuthService = $socialAuthService;
$this->registrationService = $registrationService;
$this->loginService = $loginService;
$this->redirectTo = url('/');
$this->redirectPath = url('/');
2015-07-12 15:01:42 -04:00
}
/**
* Get a validator for an incoming registration request.
*
2015-07-12 15:01:42 -04:00
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'min:2', 'max:100'],
'email' => ['required', 'email', 'max:255', 'unique:users'],
'password' => ['required', Password::default()],
2015-07-12 15:01:42 -04:00
]);
}
2015-09-05 12:42:05 -04:00
/**
* Show the application registration form.
2021-06-26 11:23:15 -04:00
*
* @throws UserRegistrationException
2015-09-05 12:42:05 -04:00
*/
public function getRegister()
{
$this->registrationService->ensureRegistrationAllowed();
2015-09-05 12:42:05 -04:00
$socialDrivers = $this->socialAuthService->getActiveDrivers();
2021-06-26 11:23:15 -04:00
return view('auth.register', [
'socialDrivers' => $socialDrivers,
]);
2015-09-05 12:42:05 -04:00
}
/**
* Handle a registration request for the application.
2021-06-26 11:23:15 -04:00
*
* @throws UserRegistrationException
* @throws StoppedAuthenticationException
*/
public function postRegister(Request $request)
{
$this->registrationService->ensureRegistrationAllowed();
$this->validator($request->all())->validate();
$userData = $request->all();
try {
$user = $this->registrationService->registerUser($userData);
$this->loginService->login($user, auth()->getDefaultDriver());
} catch (UserRegistrationException $exception) {
if ($exception->getMessage()) {
$this->showErrorNotification($exception->getMessage());
}
2021-06-26 11:23:15 -04:00
return redirect($exception->redirectLocation);
}
$this->showSuccessNotification(trans('auth.register_success'));
2021-06-26 11:23:15 -04:00
return redirect($this->redirectPath());
}
/**
* Create a new user instance after a valid registration.
2021-06-26 11:23:15 -04:00
*
* @param array $data
*
* @return User
*/
protected function create(array $data)
{
return User::create([
2021-06-26 11:23:15 -04:00
'name' => $data['name'],
'email' => $data['email'],
2019-09-06 17:14:39 -04:00
'password' => Hash::make($data['password']),
]);
}
}