From 1f88b60ccab3fc00b0cd4f7663a4aa6f35bbebc1 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 18 Oct 2022 21:19:43 +0300 Subject: [PATCH] ref --- .../Espo/Controllers/TwoFactorEmail.php | 19 +++++++++++++++---- application/Espo/Controllers/TwoFactorSms.php | 19 +++++++++++++++---- .../EmailService.php} | 14 ++++++++++++-- .../SmsService.php} | 14 ++++++++++++-- 4 files changed, 54 insertions(+), 12 deletions(-) rename application/Espo/Tools/UserSecurity/{TwoFactorEmailService.php => TwoFactor/EmailService.php} (91%) rename application/Espo/Tools/UserSecurity/{TwoFactorSmsService.php => TwoFactor/SmsService.php} (91%) diff --git a/application/Espo/Controllers/TwoFactorEmail.php b/application/Espo/Controllers/TwoFactorEmail.php index efe02816ee..152844698e 100644 --- a/application/Espo/Controllers/TwoFactorEmail.php +++ b/application/Espo/Controllers/TwoFactorEmail.php @@ -29,21 +29,25 @@ namespace Espo\Controllers; +use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Api\Request; -use Espo\Tools\UserSecurity\TwoFactorEmailService as Service; +use Espo\Core\Exceptions\NotFound; +use Espo\Tools\UserSecurity\TwoFactor\EmailService as Service; use Espo\Entities\User; class TwoFactorEmail { - private $service; - - private $user; + private Service $service; + private User $user; + /** + * @throws Forbidden + */ public function __construct(Service $service, User $user) { $this->service = $service; @@ -56,6 +60,13 @@ class TwoFactorEmail throw new Forbidden(); } } + + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + * @throws NotFound + */ public function postActionSendCode(Request $request): bool { $data = $request->getParsedBody(); diff --git a/application/Espo/Controllers/TwoFactorSms.php b/application/Espo/Controllers/TwoFactorSms.php index 0a4f7d0e3f..85e5d92ef6 100644 --- a/application/Espo/Controllers/TwoFactorSms.php +++ b/application/Espo/Controllers/TwoFactorSms.php @@ -29,21 +29,25 @@ namespace Espo\Controllers; +use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Api\Request; -use Espo\Tools\UserSecurity\TwoFactorSmsService as Service; +use Espo\Core\Exceptions\NotFound; +use Espo\Tools\UserSecurity\TwoFactor\SmsService as Service; use Espo\Entities\User; class TwoFactorSms { - private $service; - - private $user; + private Service $service; + private User $user; + /** + * @throws Forbidden + */ public function __construct(Service $service, User $user) { $this->service = $service; @@ -56,6 +60,13 @@ class TwoFactorSms throw new Forbidden(); } } + + /** + * @throws BadRequest + * @throws Forbidden + * @throws Error + * @throws NotFound + */ public function postActionSendCode(Request $request): bool { $data = $request->getParsedBody(); diff --git a/application/Espo/Tools/UserSecurity/TwoFactorEmailService.php b/application/Espo/Tools/UserSecurity/TwoFactor/EmailService.php similarity index 91% rename from application/Espo/Tools/UserSecurity/TwoFactorEmailService.php rename to application/Espo/Tools/UserSecurity/TwoFactor/EmailService.php index 077a9eb04c..20221a850e 100644 --- a/application/Espo/Tools/UserSecurity/TwoFactorEmailService.php +++ b/application/Espo/Tools/UserSecurity/TwoFactor/EmailService.php @@ -27,8 +27,9 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -namespace Espo\Tools\UserSecurity; +namespace Espo\Tools\UserSecurity\TwoFactor; +use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\NotFound; @@ -39,7 +40,7 @@ use Espo\ORM\EntityManager; use Espo\Entities\User; -class TwoFactorEmailService +class EmailService { private Util $util; private User $user; @@ -58,6 +59,11 @@ class TwoFactorEmailService $this->config = $config; } + /** + * @throws Forbidden + * @throws NotFound + * @throws Error + */ public function sendCode(string $userId, string $emailAddress): void { if (!$this->user->isAdmin() && $userId !== $this->user->getId()) { @@ -66,6 +72,7 @@ class TwoFactorEmailService $this->checkAllowed(); + /** @var ?User $user */ $user = $this->entityManager->getEntity(User::ENTITY_TYPE, $userId); if (!$user) { @@ -76,6 +83,9 @@ class TwoFactorEmailService $this->util->storeEmailAddress($user, $emailAddress); } + /** + * @throws Forbidden + */ private function checkAllowed(): void { if (!$this->config->get('auth2FA')) { diff --git a/application/Espo/Tools/UserSecurity/TwoFactorSmsService.php b/application/Espo/Tools/UserSecurity/TwoFactor/SmsService.php similarity index 91% rename from application/Espo/Tools/UserSecurity/TwoFactorSmsService.php rename to application/Espo/Tools/UserSecurity/TwoFactor/SmsService.php index c0fdc836a2..1d72a1aba6 100644 --- a/application/Espo/Tools/UserSecurity/TwoFactorSmsService.php +++ b/application/Espo/Tools/UserSecurity/TwoFactor/SmsService.php @@ -27,8 +27,9 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -namespace Espo\Tools\UserSecurity; +namespace Espo\Tools\UserSecurity\TwoFactor; +use Espo\Core\Exceptions\Error; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\NotFound; @@ -39,7 +40,7 @@ use Espo\ORM\EntityManager; use Espo\Entities\User; -class TwoFactorSmsService +class SmsService { private Util $util; private User $user; @@ -58,6 +59,11 @@ class TwoFactorSmsService $this->config = $config; } + /** + * @throws Forbidden + * @throws NotFound + * @throws Error + */ public function sendCode(string $userId, string $phoneNumber): void { if (!$this->user->isAdmin() && $userId !== $this->user->getId()) { @@ -66,6 +72,7 @@ class TwoFactorSmsService $this->checkAllowed(); + /** @var ?User $user */ $user = $this->entityManager->getEntity(User::ENTITY_TYPE, $userId); if (!$user) { @@ -76,6 +83,9 @@ class TwoFactorSmsService $this->util->storePhoneNumber($user, $phoneNumber); } + /** + * @throws Forbidden + */ private function checkAllowed(): void { if (!$this->config->get('auth2FA')) {