This commit is contained in:
Yuri Kuznetsov
2021-09-13 15:44:08 +03:00
parent bf72c8a4cc
commit 9822bb94a1
2 changed files with 20 additions and 2 deletions
+20 -1
View File
@@ -32,6 +32,7 @@ namespace Espo\Services;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Utils\Config;
use Espo\Core\Authentication\TwoFactor\Email\Util;
use Espo\ORM\EntityManager;
@@ -46,11 +47,14 @@ class TwoFactorEmail
private $entityManager;
public function __construct(Util $util, User $user, EntityManager $entityManager)
private $config;
public function __construct(Util $util, User $user, EntityManager $entityManager, Config $config)
{
$this->util = $util;
$this->user = $user;
$this->entityManager = $entityManager;
$this->config = $config;
}
public function sendCode(string $userId, string $emailAddress): void
@@ -59,6 +63,8 @@ class TwoFactorEmail
throw new Forbidden();
}
$this->checkAllowed();
$user = $this->entityManager->getEntity(User::ENTITY_TYPE, $userId);
if (!$user) {
@@ -68,4 +74,17 @@ class TwoFactorEmail
$this->util->sendCode($user, $emailAddress);
$this->util->storeEmailAddress($user, $emailAddress);
}
private function checkAllowed(): void
{
if (!$this->config->get('auth2FA')) {
throw new Forbidden("2FA is not enabled.");
}
$methodList = $this->config->get('auth2FAMethodList') ?? [];
if (!in_array('Email', $methodList)) {
throw new Forbidden("Email 2FA is not allowed.");
}
}
}
@@ -32,7 +32,6 @@ namespace Espo\Services;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error;
use Espo\ORM\EntityManager;