Skip to content

EspoCRM – Open Source CRM Application

Ref

Yurii7f4e9b9b8e7e
diff --git a/application/Espo/Classes/Cleanup/PasswordChangeRequests.php b/application/Espo/Classes/Cleanup/PasswordChangeRequests.php
index 6802a7c565..01fe90b654 100644
--- a/application/Espo/Classes/Cleanup/PasswordChangeRequests.php
+++ b/application/Espo/Classes/Cleanup/PasswordChangeRequests.php
@@ -30,25 +30,23 @@
namespace Espo\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
+use Espo\Core\Name\Field;
use Espo\Core\Utils\Config;
use Espo\Core\Field\DateTime;
-
use Espo\ORM\EntityManager;
-
use Espo\Entities\PasswordChangeRequest;
+/**
+ * @noinspection PhpUnused
+ */
class PasswordChangeRequests implements Cleanup
{
- private Config $config;
- private EntityManager $entityManager;
-
private string $cleanupPeriod = '30 days';
- public function __construct(Config $config, EntityManager $entityManager)
- {
- $this->config = $config;
- $this->entityManager = $entityManager;
- }
+ public function __construct(
+ private Config $config,
+ private EntityManager $entityManager,
+ ) {}
public function process(): void
{
@@ -63,7 +61,7 @@ class PasswordChangeRequests implements Cleanup
->delete()
->from(PasswordChangeRequest::ENTITY_TYPE)
->where([
- 'createdAt<' => $before,
+ Field::CREATED_AT . '<' => $before,
])
->build();
diff --git a/application/Espo/Entities/PasswordChangeRequest.php b/application/Espo/Entities/PasswordChangeRequest.php
index 2dc33d3a90..f08a1e4214 100644
--- a/application/Espo/Entities/PasswordChangeRequest.php
+++ b/application/Espo/Entities/PasswordChangeRequest.php
@@ -34,21 +34,26 @@ use UnexpectedValueException;
class PasswordChangeRequest extends Entity
{
- public const ENTITY_TYPE = 'PasswordChangeRequest';
+ public const string ENTITY_TYPE = 'PasswordChangeRequest';
+
+ public const string FIELD_REQUEST_ID = 'requestId';
+ private const string FIELD_URL = 'url';
+
+ public const string ATTR_USER_ID = 'userId';
public function getUrl(): ?string
{
- return $this->get('url');
+ return $this->get(self::FIELD_URL);
}
public function getRequestId(): string
{
- return $this->get('requestId');
+ return $this->get(self::FIELD_REQUEST_ID);
}
public function getUserId(): string
{
- $userId = $this->get('userId');
+ $userId = $this->get(self::ATTR_USER_ID);
if ($userId === null) {
throw new UnexpectedValueException();
@@ -56,4 +61,19 @@ class PasswordChangeRequest extends Entity
return $userId;
}
+
+ public function setRequestId(string $requestId): self
+ {
+ return $this->set(self::FIELD_REQUEST_ID, $requestId);
+ }
+
+ public function setUserId(string $userId): self
+ {
+ return $this->set(self::ATTR_USER_ID, $userId);
+ }
+
+ public function setUrl(?string $url): self
+ {
+ return $this->set(self::FIELD_URL, $url);
+ }
}
diff --git a/application/Espo/EntryPoints/ChangePassword.php b/application/Espo/EntryPoints/ChangePassword.php
index 94eb0bfbec..5378b719b2 100644
--- a/application/Espo/EntryPoints/ChangePassword.php
+++ b/application/Espo/EntryPoints/ChangePassword.php
@@ -39,6 +39,9 @@ use Espo\Core\Api\Response;
use Espo\ORM\EntityManager;
use Espo\Tools\UserSecurity\Password\ConfigProvider;
+/**
+ * @noinspection PhpUnused
+ */
class ChangePassword implements EntryPoint
{
use NoAuth;
@@ -58,9 +61,9 @@ class ChangePassword implements EntryPoint
}
$passwordChangeRequest = $this->entityManager
- ->getRDBRepository(PasswordChangeRequest::ENTITY_TYPE)
+ ->getRDBRepositoryByClass(PasswordChangeRequest::class)
->where([
- 'requestId' => $requestId,
+ PasswordChangeRequest::FIELD_REQUEST_ID => $requestId,
])
->findOne();
diff --git a/application/Espo/Tools/UserSecurity/Password/Jobs/RemoveRecoveryRequest.php b/application/Espo/Tools/UserSecurity/Password/Jobs/RemoveRecoveryRequest.php
index 124416c778..3a67cf9794 100644
--- a/application/Espo/Tools/UserSecurity/Password/Jobs/RemoveRecoveryRequest.php
+++ b/application/Espo/Tools/UserSecurity/Password/Jobs/RemoveRecoveryRequest.php
@@ -37,12 +37,9 @@ use RuntimeException;
class RemoveRecoveryRequest implements Job
{
- private EntityManager $entityManager;
-
- public function __construct(EntityManager $entityManager)
- {
- $this->entityManager = $entityManager;
- }
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
public function run(Data $data): void
{
diff --git a/application/Espo/Tools/UserSecurity/Password/RecoveryService.php b/application/Espo/Tools/UserSecurity/Password/RecoveryService.php
index f3d4f81d86..5fed4f660b 100644
--- a/application/Espo/Tools/UserSecurity/Password/RecoveryService.php
+++ b/application/Espo/Tools/UserSecurity/Password/RecoveryService.php
@@ -40,6 +40,7 @@ use Espo\Core\Job\JobSchedulerFactory;
use Espo\Core\Mail\Exceptions\NoSmtp;
use Espo\Core\Mail\Exceptions\SendingError;
use Espo\Core\Mail\SmtpParams;
+use Espo\Core\Name\Field;
use Espo\Core\Utils\Config\ApplicationConfig;
use Espo\Core\Utils\Util;
use Espo\Entities\Email;
@@ -90,26 +91,18 @@ class RecoveryService
*/
public function getRequest(string $id): PasswordChangeRequest
{
- $config = $this->config;
-
- if ($config->get('passwordRecoveryDisabled')) {
- throw new Forbidden("Password recovery: Disabled.");
- }
+ $this->assertNotDisabled();
$request = $this->entityManager
->getRDBRepositoryByClass(PasswordChangeRequest::class)
- ->where(['requestId' => $id])
+ ->where([PasswordChangeRequest::FIELD_REQUEST_ID => $id])
->findOne();
if (!$request) {
- throw new NotFound("Password recovery: Request not found by id.");
+ throw new NotFound("Password recovery: Request not found by ID.");
}
- $userId = $request->get('userId');
-
- if (!$userId) {
- throw new Error();
- }
+ $request->getUserId();
return $request;
}
@@ -118,7 +111,7 @@ class RecoveryService
{
$request = $this->entityManager
->getRDBRepositoryByClass(PasswordChangeRequest::class)
- ->where(['requestId' => $id])
+ ->where([PasswordChangeRequest::FIELD_REQUEST_ID => $id])
->findOne();
if ($request) {
@@ -137,9 +130,7 @@ class RecoveryService
$noExposure = $config->get('passwordRecoveryNoExposure') ?? false;
- if ($config->get('passwordRecoveryDisabled')) {
- throw new Forbidden("Password recovery: Disabled.");
- }
+ $this->assertNotDisabled();
if ($url) {
$this->urlValidator->validate($url);
@@ -148,8 +139,8 @@ class RecoveryService
$user = $this->entityManager
->getRDBRepositoryByClass(User::class)
->where([
- 'userName' => $userName,
- 'emailAddress' => $emailAddress,
+ User::FIELD_USER_NAME => $userName,
+ Field::EMAIL_ADDRESS => $emailAddress,
])
->findOne();
@@ -217,7 +208,7 @@ class RecoveryService
$existingRequest = $this->entityManager
->getRDBRepositoryByClass(PasswordChangeRequest::class)
- ->where(['userId' => $user->getId()])
+ ->where([PasswordChangeRequest::ATTR_USER_ID=> $user->getId()])
->findOne();
if ($existingRequest) {
@@ -266,15 +257,10 @@ class RecoveryService
{
$this->checkUser($user);
- $entity = $this->entityManager->getRDBRepositoryByClass(PasswordChangeRequest::class)->getNew();
-
- $entity->setMultiple([
- 'userId' => $user->getId(),
- 'requestId' => Util::generateCryptId(),
- 'url' => $url,
- ]);
-
- return $entity;
+ return $this->entityManager->getRDBRepositoryByClass(PasswordChangeRequest::class)->getNew()
+ ->setUserId($user->getId())
+ ->setRequestId(Util::generateCryptId())
+ ->setUrl($url);
}
/**
@@ -565,4 +551,14 @@ class RecoveryService
$this->authenticationMethodProvider->get() === LdapLogin::NAME &&
!$this->config->get('ldapPortalUserLdapAuth');
}
+
+ /**
+ * @throws Forbidden
+ */
+ private function assertNotDisabled(): void
+ {
+ if ($this->config->get('passwordRecoveryDisabled')) {
+ throw new Forbidden("Password recovery: Disabled.");
+ }
+ }
}