2021-07-18 11:52:31 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
|
|
|
|
|
|
|
use BookStack\Auth\Access\LoginService;
|
|
|
|
use BookStack\Auth\User;
|
|
|
|
use Illuminate\Contracts\Support\Responsable;
|
2021-08-07 16:18:59 -04:00
|
|
|
use Illuminate\Http\Request;
|
2021-07-18 11:52:31 -04:00
|
|
|
|
|
|
|
class StoppedAuthenticationException extends \Exception implements Responsable
|
|
|
|
{
|
|
|
|
protected $user;
|
|
|
|
protected $loginService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* StoppedAuthenticationException constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(User $user, LoginService $loginService)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->loginService = $loginService;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function toResponse($request)
|
|
|
|
{
|
|
|
|
$redirect = '/login';
|
|
|
|
|
|
|
|
if ($this->loginService->awaitingEmailConfirmation($this->user)) {
|
2021-08-07 16:18:59 -04:00
|
|
|
return $this->awaitingEmailConfirmationResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->loginService->needsMfaVerification($this->user)) {
|
2021-07-18 11:52:31 -04:00
|
|
|
$redirect = '/mfa/verify';
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($redirect);
|
|
|
|
}
|
2021-08-07 16:18:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide an error response for when the current user's email is not confirmed
|
|
|
|
* in a system which requires it.
|
|
|
|
*/
|
|
|
|
protected function awaitingEmailConfirmationResponse(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->wantsJson()) {
|
|
|
|
return response()->json([
|
|
|
|
'error' => [
|
2021-08-21 10:49:40 -04:00
|
|
|
'code' => 401,
|
2021-08-07 16:18:59 -04:00
|
|
|
'message' => trans('errors.email_confirmation_awaiting'),
|
|
|
|
],
|
|
|
|
], 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session()->get('sent-email-confirmation') === true) {
|
|
|
|
return redirect('/register/confirm');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/register/confirm/awaiting');
|
|
|
|
}
|
2021-08-21 10:49:40 -04:00
|
|
|
}
|