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