diff --git a/src/Authentication/Actions/Email2FA.php b/src/Authentication/Actions/Email2FA.php index 0cd66e98e..5ce92fb8f 100644 --- a/src/Authentication/Actions/Email2FA.php +++ b/src/Authentication/Actions/Email2FA.php @@ -4,6 +4,7 @@ use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\Shield\Exceptions\RuntimeException; use CodeIgniter\Shield\Models\UserIdentityModel; /** @@ -58,6 +59,10 @@ public function handle(IncomingRequest $request) return redirect()->route('auth-action-show')->with('error', lang('Auth.invalidEmail')); } + if ($user === null) { + throw new RuntimeException('Cannot get the User.'); + } + /** @var UserIdentityModel $identityModel */ $identityModel = model(UserIdentityModel::class); @@ -69,13 +74,16 @@ public function handle(IncomingRequest $request) // Send the user an email with the code helper('email'); - $email = emailer(); - $email->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $return = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.email2FASubject')) ->setMessage(view(setting('Auth.views')['action_email_2fa_email'], ['code' => $identity->secret])) ->send(); + if ($return === false) { + throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); + } + return view(setting('Auth.views')['action_email_2fa_verify']); } diff --git a/src/Authentication/Actions/EmailActivator.php b/src/Authentication/Actions/EmailActivator.php index ad1f6625d..3f9cd0a0e 100644 --- a/src/Authentication/Actions/EmailActivator.php +++ b/src/Authentication/Actions/EmailActivator.php @@ -5,6 +5,7 @@ use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\Shield\Exceptions\RuntimeException; use CodeIgniter\Shield\Models\UserIdentityModel; class EmailActivator implements ActionInterface @@ -18,6 +19,10 @@ public function show(): string { $user = auth()->user(); + if ($user === null) { + throw new RuntimeException('Cannot get the User.'); + } + /** @var UserIdentityModel $identityModel */ $identityModel = model(UserIdentityModel::class); @@ -38,13 +43,16 @@ public function show(): string // Send the email helper('email'); - $email = emailer(); - $email->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') + $return = emailer()->setFrom(setting('Email.fromEmail'), setting('Email.fromName') ?? '') ->setTo($user->getAuthEmail()) ->setSubject(lang('Auth.emailActivateSubject')) ->setMessage(view(setting('Auth.views')['action_email_activate_email'], ['code' => $code])) ->send(); + if ($return === false) { + throw new RuntimeException('Cannot send email for user: ' . $user->getAuthEmail()); + } + // Display the info page return view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]); } diff --git a/src/Exceptions/LogicException.php b/src/Exceptions/LogicException.php new file mode 100644 index 000000000..c59b59096 --- /dev/null +++ b/src/Exceptions/LogicException.php @@ -0,0 +1,7 @@ +