2021-07-18 23:52:31 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exceptions;
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Access\LoginService;
|
|
|
|
use BookStack\Users\Models\User;
|
2021-07-18 23:52:31 +08:00
|
|
|
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
|
|
|
|
{
|
2024-05-21 00:23:15 +08:00
|
|
|
public function __construct(
|
|
|
|
protected User $user,
|
|
|
|
protected LoginService $loginService
|
|
|
|
) {
|
2021-07-18 23:52:31 +08:00
|
|
|
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
|
|
|
}
|