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

93 lines
2.8 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\Exceptions\StoppedAuthenticationException;
use BookStack\Exceptions\UserRegistrationException;
use BookStack\Http\Controllers\Controller;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Http\Request;
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
{
protected SocialAuthService $socialAuthService;
protected RegistrationService $registrationService;
protected LoginService $loginService;
2015-08-05 15:59:39 -04:00
/**
* 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;
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('/');
}
/**
* Get a validator for an incoming registration request.
*/
protected function validator(array $data): ValidatorContract
{
return Validator::make($data, [
'name' => ['required', 'min:2', 'max:100'],
'email' => ['required', 'email', 'max:255', 'unique:users'],
'password' => ['required', Password::default()],
]);
}
}