2016-09-17 17:22:04 +00:00
|
|
|
<?php
|
2015-07-12 19:01:42 +00:00
|
|
|
|
2016-09-17 17:22:04 +00:00
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2021-07-17 16:45:00 +00:00
|
|
|
use BookStack\Auth\Access\LoginService;
|
2020-01-26 14:42:50 +00:00
|
|
|
use BookStack\Auth\Access\RegistrationService;
|
2019-08-04 13:26:39 +00:00
|
|
|
use BookStack\Auth\Access\SocialAuthService;
|
2021-08-07 20:18:59 +00:00
|
|
|
use BookStack\Exceptions\StoppedAuthenticationException;
|
2015-09-10 18:31:09 +00:00
|
|
|
use BookStack\Exceptions\UserRegistrationException;
|
2018-09-25 15:58:03 +00:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
2022-09-22 15:54:27 +00:00
|
|
|
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
|
2016-09-17 17:22:04 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-09-26 14:37:55 +00:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2021-12-18 16:31:48 +00:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2015-07-12 19:01:42 +00:00
|
|
|
|
2016-09-17 17:22:04 +00:00
|
|
|
class RegisterController extends Controller
|
2015-07-12 19:01:42 +00:00
|
|
|
{
|
2022-08-09 11:40:59 +00:00
|
|
|
protected SocialAuthService $socialAuthService;
|
|
|
|
protected RegistrationService $registrationService;
|
|
|
|
protected LoginService $loginService;
|
2015-08-05 19:59:39 +00:00
|
|
|
|
2016-09-17 17:22:04 +00:00
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
2015-07-12 19:01:42 +00:00
|
|
|
*/
|
2021-07-17 16:45:00 +00:00
|
|
|
public function __construct(
|
|
|
|
SocialAuthService $socialAuthService,
|
|
|
|
RegistrationService $registrationService,
|
|
|
|
LoginService $loginService
|
2021-08-21 14:49:40 +00:00
|
|
|
) {
|
2020-02-02 13:10:21 +00:00
|
|
|
$this->middleware('guest');
|
|
|
|
$this->middleware('guard:standard');
|
2020-01-26 14:42:50 +00:00
|
|
|
|
2015-09-04 16:50:52 +00:00
|
|
|
$this->socialAuthService = $socialAuthService;
|
2020-01-26 14:42:50 +00:00
|
|
|
$this->registrationService = $registrationService;
|
2021-07-17 16:45:00 +00:00
|
|
|
$this->loginService = $loginService;
|
2015-07-12 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 16:42:05 +00:00
|
|
|
/**
|
|
|
|
* Show the application registration form.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2018-01-28 17:15:30 +00:00
|
|
|
* @throws UserRegistrationException
|
2015-09-05 16:42:05 +00:00
|
|
|
*/
|
|
|
|
public function getRegister()
|
|
|
|
{
|
2020-02-02 17:31:00 +00:00
|
|
|
$this->registrationService->ensureRegistrationAllowed();
|
2015-09-05 16:42:05 +00:00
|
|
|
$socialDrivers = $this->socialAuthService->getActiveDrivers();
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2019-11-17 16:07:06 +00:00
|
|
|
return view('auth.register', [
|
|
|
|
'socialDrivers' => $socialDrivers,
|
|
|
|
]);
|
2015-09-05 16:42:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 19:25:57 +00:00
|
|
|
/**
|
|
|
|
* Handle a registration request for the application.
|
2021-06-26 15:23:15 +00:00
|
|
|
*
|
2015-09-05 19:25:57 +00:00
|
|
|
* @throws UserRegistrationException
|
2021-08-07 20:18:59 +00:00
|
|
|
* @throws StoppedAuthenticationException
|
2015-09-05 19:25:57 +00:00
|
|
|
*/
|
|
|
|
public function postRegister(Request $request)
|
|
|
|
{
|
2020-02-02 17:31:00 +00:00
|
|
|
$this->registrationService->ensureRegistrationAllowed();
|
2018-01-28 17:15:30 +00:00
|
|
|
$this->validator($request->all())->validate();
|
2015-09-06 11:14:32 +00:00
|
|
|
$userData = $request->all();
|
2020-01-26 14:42:50 +00:00
|
|
|
|
|
|
|
try {
|
2020-02-02 17:31:00 +00:00
|
|
|
$user = $this->registrationService->registerUser($userData);
|
2021-07-17 16:45:00 +00:00
|
|
|
$this->loginService->login($user, auth()->getDefaultDriver());
|
2020-01-26 14:42:50 +00:00
|
|
|
} catch (UserRegistrationException $exception) {
|
|
|
|
if ($exception->getMessage()) {
|
|
|
|
$this->showErrorNotification($exception->getMessage());
|
|
|
|
}
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2020-01-26 14:42:50 +00:00
|
|
|
return redirect($exception->redirectLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->showSuccessNotification(trans('auth.register_success'));
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2022-09-22 15:54:27 +00:00
|
|
|
return redirect('/');
|
2015-09-06 11:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-22 15:54:27 +00:00
|
|
|
* Get a validator for an incoming registration request.
|
2015-09-06 11:14:32 +00:00
|
|
|
*/
|
2022-09-22 15:54:27 +00:00
|
|
|
protected function validator(array $data): ValidatorContract
|
2015-09-06 11:14:32 +00:00
|
|
|
{
|
2022-09-22 15:54:27 +00:00
|
|
|
return Validator::make($data, [
|
|
|
|
'name' => ['required', 'min:2', 'max:100'],
|
|
|
|
'email' => ['required', 'email', 'max:255', 'unique:users'],
|
|
|
|
'password' => ['required', Password::default()],
|
2016-09-17 17:22:04 +00:00
|
|
|
]);
|
2015-09-06 11:14:32 +00:00
|
|
|
}
|
2018-01-28 16:58:52 +00:00
|
|
|
}
|