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

123 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2015-07-12 15:01:42 -04:00
namespace BookStack\Http\Controllers\Auth;
use BookStack\Auth\Access\RegistrationService;
use BookStack\Auth\Access\SocialAuthService;
use BookStack\Auth\User;
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 Validator;
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;
protected $registrationService;
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)
2015-07-12 15:01:42 -04:00
{
$this->middleware('guest');
$this->middleware('guard:standard');
$this->socialAuthService = $socialAuthService;
$this->registrationService = $registrationService;
$this->redirectTo = url('/');
$this->redirectPath = url('/');
parent::__construct();
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:255',
'email' => 'required|email|max:255|unique:users',
2019-09-13 18:58:40 -04:00
'password' => 'required|min:8',
2015-07-12 15:01:42 -04:00
]);
}
2015-09-05 12:42:05 -04:00
/**
* Show the application registration form.
* @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();
return view('auth.register', [
'socialDrivers' => $socialDrivers,
]);
2015-09-05 12:42:05 -04:00
}
/**
* Handle a registration request for the application.
* @throws UserRegistrationException
*/
public function postRegister(Request $request)
{
$this->registrationService->ensureRegistrationAllowed();
$this->validator($request->all())->validate();
$userData = $request->all();
try {
$user = $this->registrationService->registerUser($userData);
auth()->login($user);
} catch (UserRegistrationException $exception) {
if ($exception->getMessage()) {
$this->showErrorNotification($exception->getMessage());
}
return redirect($exception->redirectLocation);
}
$this->showSuccessNotification(trans('auth.register_success'));
return redirect($this->redirectPath());
}
/**
* Create a new user instance after a valid registration.
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
2019-09-06 17:14:39 -04:00
'password' => Hash::make($data['password']),
]);
}
}