This commit is contained in:
Yuri Kuznetsov
2022-10-18 21:19:43 +03:00
parent 09d7c73449
commit 1f88b60cca
4 changed files with 54 additions and 12 deletions

View File

@@ -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();

View File

@@ -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();

View File

@@ -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')) {

View File

@@ -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')) {