2021-07-18 23:52:31 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
|
|
|
|
|
|
|
use BookStack\Auth\Access\LoginService;
|
|
|
|
use BookStack\Auth\User;
|
|
|
|
use Illuminate\Contracts\Support\Responsable;
|
2021-08-08 04:18:59 +08:00
|
|
|
use Illuminate\Http\Request;
|
2021-07-18 23:52:31 +08: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-27 05:04:18 +08:00
|
|
|
* {@inheritdoc}
|
2021-07-18 23:52:31 +08:00
|
|
|
*/
|
|
|
|
public function toResponse($request)
|
|
|
|
{
|
|
|
|
$redirect = '/login';
|
|
|
|
|
|
|
|
if ($this->loginService->awaitingEmailConfirmation($this->user)) {
|
2021-08-08 04:18:59 +08:00
|
|
|
return $this->awaitingEmailConfirmationResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->loginService->needsMfaVerification($this->user)) {
|
2021-07-18 23:52:31 +08:00
|
|
|
$redirect = '/mfa/verify';
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect($redirect);
|
|
|
|
}
|
2021-08-08 04:18:59 +08: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 22:49:40 +08:00
|
|
|
'code' => 401,
|
2021-08-08 04:18:59 +08:00
|
|
|
'message' => trans('errors.email_confirmation_awaiting'),
|
|
|
|
],
|
|
|
|
], 401);
|
|
|
|
}
|
|
|
|
|
2021-09-18 06:44:54 +08:00
|
|
|
if (session()->pull('sent-email-confirmation') === true) {
|
2021-08-08 04:18:59 +08:00
|
|
|
return redirect('/register/confirm');
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect('/register/confirm/awaiting');
|
|
|
|
}
|
2021-08-21 22:49:40 +08:00
|
|
|
}
|