2021-07-18 11:52:31 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Access\LoginService;
|
|
|
|
use BookStack\Users\Models\User;
|
2021-07-18 11:52:31 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-26 17:04:18 -04:00
|
|
|
* {@inheritdoc}
|
2021-07-18 11:52:31 -04:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:44:54 -04:00
|
|
|
if (session()->pull('sent-email-confirmation') === true) {
|
2021-08-07 16:18:59 -04:00
|
|
|
return redirect('/register/confirm');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/register/confirm/awaiting');
|
|
|
|
}
|
2021-08-21 10:49:40 -04:00
|
|
|
}
|