Skip to content

EspoCRM – Open Source CRM Application

System state

Yurii542cc602e630
diff --git a/application/Espo/Binding.php b/application/Espo/Binding.php
index d3d614f828..127425d96d 100644
--- a/application/Espo/Binding.php
+++ b/application/Espo/Binding.php
@@ -257,6 +257,11 @@ class Binding implements BindingProcessor
'Espo\\Core\\Job\\JobManager',
'jobManager'
);
+
+ $binder->bindService(
+ 'Espo\\Core\\Utils\\System\\SystemState',
+ 'systemState'
+ );
}
private function bindCore(Binder $binder): void
diff --git a/application/Espo/Core/Acl/Cache/Clearer.php b/application/Espo/Core/Acl/Cache/Clearer.php
index 6e2d5af982..a6e75a6507 100644
--- a/application/Espo/Core/Acl/Cache/Clearer.php
+++ b/application/Espo/Core/Acl/Cache/Clearer.php
@@ -30,6 +30,7 @@
namespace Espo\Core\Acl\Cache;
use Espo\Core\Utils\File\Manager as FileManager;
+use Espo\Core\Utils\System\SystemState;
use Espo\Entities\Portal;
use Espo\Entities\User;
use Espo\ORM\EntityManager;
@@ -40,19 +41,26 @@ use Espo\ORM\Name\Attribute;
*/
class Clearer
{
- public function __construct(private FileManager $fileManager, private EntityManager $entityManager)
- {}
+ public function __construct(
+ private FileManager $fileManager,
+ private EntityManager $entityManager,
+ private SystemState $systemState,
+ ) {}
public function clearForAllInternalUsers(): void
{
$this->fileManager->removeInDir('data/cache/application/acl');
$this->fileManager->removeInDir('data/cache/application/aclMap');
+
+ $this->bumpSystemStateVersionNumber();
}
public function clearForAllPortalUsers(): void
{
$this->fileManager->removeInDir('data/cache/application/aclPortal');
$this->fileManager->removeInDir('data/cache/application/aclPortalMap');
+
+ $this->bumpSystemStateVersionNumber();
}
public function clearForUser(User $user): void
@@ -67,6 +75,8 @@ class Clearer
$this->fileManager->remove('data/cache/application/acl/' . $part);
$this->fileManager->remove('data/cache/application/aclMap/' . $part);
+
+ $this->bumpSystemStateVersionNumber();
}
private function clearForPortalUser(User $user): void
@@ -82,5 +92,12 @@ class Clearer
$this->fileManager->remove('data/cache/application/aclPortal/' . $part);
$this->fileManager->remove('data/cache/application/aclPortalMap/' . $part);
}
+
+ $this->bumpSystemStateVersionNumber();
+ }
+
+ private function bumpSystemStateVersionNumber(): void
+ {
+ $this->systemState->bumpVersionNumber();
}
}
diff --git a/application/Espo/Core/Loaders/SystemState.php b/application/Espo/Core/Loaders/SystemState.php
new file mode 100644
index 0000000000..e263346efa
--- /dev/null
+++ b/application/Espo/Core/Loaders/SystemState.php
@@ -0,0 +1,49 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Loaders;
+
+use Espo\Core\Container\Loader;
+use Espo\Core\InjectableFactory;
+use Espo\Core\Utils\System\SystemState as SystemStateService;
+
+/**
+ * @noinspection PhpUnused
+ */
+class SystemState implements Loader
+{
+ public function __construct(
+ private InjectableFactory $injectableFactory,
+ ) {}
+
+ public function load()
+ {
+ return $this->injectableFactory->create(SystemStateService::class);
+ }
+}
diff --git a/application/Espo/Core/ORM/EntityManagerProxy.php b/application/Espo/Core/ORM/EntityManagerProxy.php
index 54802f739e..ad40dd0d73 100644
--- a/application/Espo/Core/ORM/EntityManagerProxy.php
+++ b/application/Espo/Core/ORM/EntityManagerProxy.php
@@ -30,6 +30,7 @@
namespace Espo\Core\ORM;
use Espo\ORM\Entity;
+use Espo\ORM\Executor\QueryExecutor;
use Espo\ORM\Metadata;
use Espo\ORM\Repository\RDBRepository;
use Espo\ORM\Repository\Repository;
@@ -129,4 +130,14 @@ class EntityManagerProxy
{
return $this->getEntityManager()->getRepositoryByClass($className);
}
+
+ /**
+ * Get the query executor.
+ *
+ * @since 10.1.0
+ */
+ public function getQueryExecutor(): QueryExecutor
+ {
+ return $this->getEntityManager()->getQueryExecutor();
+ }
}
diff --git a/application/Espo/Core/Rebuild/Actions/AddSystemState.php b/application/Espo/Core/Rebuild/Actions/AddSystemState.php
new file mode 100644
index 0000000000..05618f9092
--- /dev/null
+++ b/application/Espo/Core/Rebuild/Actions/AddSystemState.php
@@ -0,0 +1,53 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Rebuild\Actions;
+
+use Espo\Core\Rebuild\RebuildAction;
+use Espo\Entities\SystemState;
+use Espo\ORM\EntityManager;
+use Espo\ORM\Name\Attribute;
+
+class AddSystemState implements RebuildAction
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function process(): void
+ {
+ $entity = $this->entityManager->getEntityById(SystemState::ENTITY_TYPE, SystemState::ID_VALUE);
+
+ if ($entity) {
+ return;
+ }
+
+ $this->entityManager->createEntity(SystemState::ENTITY_TYPE, [Attribute::ID => SystemState::ID_VALUE]);
+ }
+}
diff --git a/application/Espo/Core/Utils/EmailFilterManager.php b/application/Espo/Core/Utils/EmailFilterManager.php
index 26059a78d6..ef8acd4247 100644
--- a/application/Espo/Core/Utils/EmailFilterManager.php
+++ b/application/Espo/Core/Utils/EmailFilterManager.php
@@ -32,6 +32,7 @@ namespace Espo\Core\Utils;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Mail\FiltersMatcher;
use Espo\Core\Utils\Config\SystemConfig;
+use Espo\Core\Utils\User\UserStateProvider;
use Espo\Entities\Email;
use Espo\Entities\EmailFilter;
use Espo\Entities\User;
@@ -49,13 +50,17 @@ class EmailFilterManager
private array $data = [];
private bool $useCache;
- private const CACHE_KEY = 'emailFilters';
+ /** @var array<string, int> */
+ private array $cacheVersionMap = [];
+
+ private const string CACHE_KEY = 'emailFilters';
public function __construct(
private EntityManager $entityManager,
private FiltersMatcher $filtersMatcher,
private DataCache $dataCache,
SystemConfig $systemConfig,
+ private UserStateProvider $userStateProvider,
) {
$this->useCache = $systemConfig->useCache();
}
@@ -72,15 +77,19 @@ class EmailFilterManager
*/
private function get(string $userId): array
{
- if (array_key_exists($userId, $this->data)) {
+ if (array_key_exists($userId, $this->data) && $this->cacheIsRelevant($userId)) {
return $this->data[$userId];
}
+ unset($this->data[$userId]);
+
$cacheKey = $this->composeCacheKey($userId);
if ($this->useCache && $this->dataCache->has($cacheKey)) {
$this->data[$userId] = $this->loadFromCache($cacheKey);
+ $this->setCacheVersionNumber($userId);
+
return $this->data[$userId];
}
@@ -90,6 +99,8 @@ class EmailFilterManager
$this->storeToCache($userId);
}
+ $this->setCacheVersionNumber($userId);
+
return $this->data[$userId];
}
@@ -163,4 +174,20 @@ class EmailFilterManager
$this->dataCache->store($cacheKey, $dataList);
}
+
+ private function setCacheVersionNumber(string $userId): void
+ {
+ $this->cacheVersionMap[$userId] = $this->userStateProvider->getEmailFiltersVersionNumber($userId);
+ }
+
+ private function cacheIsRelevant(string $userId): bool
+ {
+ $versionNumber = $this->cacheVersionMap[$userId] ?? null;
+
+ if ($versionNumber === null) {
+ return false;
+ }
+
+ return $versionNumber === $this->userStateProvider->getEmailFiltersVersionNumber($userId);
+ }
}
diff --git a/application/Espo/Core/Utils/System/SystemState.php b/application/Espo/Core/Utils/System/SystemState.php
new file mode 100644
index 0000000000..fee6cc5c70
--- /dev/null
+++ b/application/Espo/Core/Utils/System/SystemState.php
@@ -0,0 +1,70 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\System;
+
+use Espo\Core\ORM\EntityManagerProxy;
+use Espo\Entities\SystemState as State;
+use Espo\ORM\Name\Attribute;
+use Espo\ORM\Query\Part\Expression as Expr;
+use Espo\ORM\Query\UpdateBuilder;
+
+/**
+ * @since 10.1.0
+ */
+class SystemState
+{
+ public function __construct(
+ private EntityManagerProxy $entityManager,
+ ) {}
+
+ public function getVersionNumber(): int
+ {
+ $entity = $this->entityManager
+ ->getRDBRepositoryByClass(State::class)
+ ->getById(State::ID_VALUE);
+
+ return $entity?->getVersionNumber() ?? 0;
+ }
+
+ public function bumpVersionNumber(): void
+ {
+ $query = UpdateBuilder::create()
+ ->in(State::ENTITY_TYPE)
+ ->set([
+ State::FIELD_VERSION_NUMBER => Expr::add(Expr::column(State::FIELD_VERSION_NUMBER), 1),
+ ])
+ ->where([
+ Attribute::ID => State::ID_VALUE,
+ ])
+ ->build();
+
+ $this->entityManager->getQueryExecutor()->execute($query);
+ }
+}
diff --git a/application/Espo/Core/Utils/User/UserStateProvider.php b/application/Espo/Core/Utils/User/UserStateProvider.php
new file mode 100644
index 0000000000..b6ee139824
--- /dev/null
+++ b/application/Espo/Core/Utils/User/UserStateProvider.php
@@ -0,0 +1,106 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Core\Utils\User;
+
+use Espo\Entities\User;
+use Espo\Entities\UserState;
+use Espo\ORM\EntityManager;
+use Espo\ORM\Name\Attribute;
+use Espo\ORM\Query\Part\Expression as Expr;
+use Espo\ORM\Query\UpdateBuilder;
+
+/**
+ * @since 10.1.0
+ * @internal
+ */
+class UserStateProvider
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function getEmailFiltersVersionNumber(string $userId): int
+ {
+ $userState = $this->entityManager
+ ->getRDBRepositoryByClass(UserState::class)
+ ->where([UserState::ATTR_USER_ID => $userId])
+ ->findOne();
+
+ return $userState?->getEmailFiltersVersionNumber() ?? 0;
+ }
+
+ public function bumpEmailFiltersVersionNumber(string $userId): void
+ {
+ $this->prepare($userId);
+
+ $column = UserState::FIELD_EMAIL_FILTERS_VERSION_NUMBER;
+
+ $query = UpdateBuilder::create()
+ ->in(UserState::ENTITY_TYPE)
+ ->set([
+ $column => Expr::add(Expr::column($column), 1),
+ ])
+ ->where([
+ UserState::ATTR_USER_ID => $userId,
+ ])
+ ->build();
+
+ $this->entityManager->getQueryExecutor()->execute($query);
+ }
+
+ private function prepare(string $userId): void
+ {
+ $user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);
+
+ if (!$user) {
+ return;
+ }
+
+ $userState = $this->entityManager
+ ->getRDBRepositoryByClass(UserState::class)
+ ->select(Attribute::ID)
+ ->where([UserState::ATTR_USER_ID => $user->getId()])
+ ->findOne();
+
+ if ($userState) {
+ return;
+ }
+
+ $userState = $this->entityManager->getRDBRepositoryByClass(UserState::class)->getNew();
+
+ $userState->set(UserState::ATTR_USER_ID, $user->getId());
+
+ $this->entityManager
+ ->getMapper()
+ ->insertOnDuplicateUpdate($userState, [UserState::ATTR_USER_ID]);
+
+ $userState->setAsFetched();
+ }
+}
diff --git a/application/Espo/Entities/SystemState.php b/application/Espo/Entities/SystemState.php
new file mode 100644
index 0000000000..dfff37a5c8
--- /dev/null
+++ b/application/Espo/Entities/SystemState.php
@@ -0,0 +1,50 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Entities;
+
+use Espo\Core\ORM\Entity;
+
+/**
+ * @internal
+ * @since 10.1.0
+ */
+class SystemState extends Entity
+{
+ public const string ENTITY_TYPE = 'SystemState';
+
+ public const string ID_VALUE = '1';
+
+ public const string FIELD_VERSION_NUMBER = 'versionNumber';
+
+ public function getVersionNumber(): int
+ {
+ return $this->get(self::FIELD_VERSION_NUMBER) ?? 0;
+ }
+}
diff --git a/application/Espo/Entities/UserState.php b/application/Espo/Entities/UserState.php
new file mode 100644
index 0000000000..41499f560e
--- /dev/null
+++ b/application/Espo/Entities/UserState.php
@@ -0,0 +1,50 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Entities;
+
+use Espo\Core\ORM\Entity;
+
+/**
+ * @internal
+ * @since 10.1.0
+ */
+class UserState extends Entity
+{
+ public const string ENTITY_TYPE = 'UserState';
+
+ public const string FIELD_EMAIL_FILTERS_VERSION_NUMBER = 'emailFiltersVersionNumber';
+
+ public const string ATTR_USER_ID = 'userId';
+
+ public function getEmailFiltersVersionNumber(): int
+ {
+ return $this->get(self::FIELD_EMAIL_FILTERS_VERSION_NUMBER) ?? 0;
+ }
+}
diff --git a/application/Espo/Hooks/EmailFilter/CacheClearing.php b/application/Espo/Hooks/EmailFilter/CacheClearing.php
index 60fd6f9c5c..228fa25102 100644
--- a/application/Espo/Hooks/EmailFilter/CacheClearing.php
+++ b/application/Espo/Hooks/EmailFilter/CacheClearing.php
@@ -32,6 +32,7 @@ namespace Espo\Hooks\EmailFilter;
use Espo\Core\Hook\Hook\AfterRemove;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\Utils\DataCache;
+use Espo\Core\Utils\User\UserStateProvider;
use Espo\Entities\EmailFilter;
use Espo\Entities\User;
use Espo\ORM\Entity;
@@ -44,9 +45,12 @@ use Espo\ORM\Repository\Option\SaveOptions;
*/
class CacheClearing implements AfterSave, AfterRemove
{
- private const CACHE_KEY = 'emailFilters';
+ private const string CACHE_KEY = 'emailFilters';
- public function __construct(private DataCache $dataCache) {}
+ public function __construct(
+ private DataCache $dataCache,
+ private UserStateProvider $userStateProvider,
+ ) {}
/**
* @param EmailFilter $entity
@@ -73,6 +77,8 @@ class CacheClearing implements AfterSave, AfterRemove
$cacheKey = $this->composeCacheKey($entity->getParentId());
$this->dataCache->clear($cacheKey);
+
+ $this->userStateProvider->bumpEmailFiltersVersionNumber($entity->getParentId());
}
private function composeCacheKey(string $userId): string
diff --git a/application/Espo/Hooks/User/DeleteUserState.php b/application/Espo/Hooks/User/DeleteUserState.php
new file mode 100644
index 0000000000..1ff77b66d5
--- /dev/null
+++ b/application/Espo/Hooks/User/DeleteUserState.php
@@ -0,0 +1,60 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+namespace Espo\Hooks\User;
+
+use Espo\Core\Hook\Hook\AfterRemove;
+use Espo\Entities\User;
+use Espo\Entities\UserState;
+use Espo\ORM\Entity;
+use Espo\ORM\EntityManager;
+use Espo\ORM\Query\DeleteBuilder;
+use Espo\ORM\Repository\Option\RemoveOptions;
+
+/**
+ * @implements AfterRemove<User>
+ */
+class DeleteUserState implements AfterRemove
+{
+ public function __construct(
+ private EntityManager $entityManager,
+ ) {}
+
+ public function afterRemove(Entity $entity, RemoveOptions $options): void
+ {
+ $query = DeleteBuilder::create()
+ ->from(UserState::ENTITY_TYPE)
+ ->where([
+ UserState::ATTR_USER_ID => $entity->getId()
+ ])
+ ->build();
+
+ $this->entityManager->getQueryExecutor()->execute($query);
+ }
+}
diff --git a/application/Espo/Resources/metadata/app/rebuild.json b/application/Espo/Resources/metadata/app/rebuild.json
index 0e230586b1..a00b970114 100644
--- a/application/Espo/Resources/metadata/app/rebuild.json
+++ b/application/Espo/Resources/metadata/app/rebuild.json
@@ -1,6 +1,7 @@
{
"actionClassNameList": [
"Espo\\Core\\Rebuild\\Actions\\AddSystemUser",
+ "Espo\\Core\\Rebuild\\Actions\\AddSystemState",
"Espo\\Core\\Rebuild\\Actions\\AddSystemData",
"Espo\\Core\\Rebuild\\Actions\\VersionUpdate",
"Espo\\Core\\Rebuild\\Actions\\ScheduledJobs",
diff --git a/application/Espo/Resources/metadata/entityAcl/SystemState.json b/application/Espo/Resources/metadata/entityAcl/SystemState.json
new file mode 100644
index 0000000000..e96a442720
--- /dev/null
+++ b/application/Espo/Resources/metadata/entityAcl/SystemState.json
@@ -0,0 +1,3 @@
+{
+ "systemWriteForbidden": true
+}
diff --git a/application/Espo/Resources/metadata/entityAcl/UserState.json b/application/Espo/Resources/metadata/entityAcl/UserState.json
new file mode 100644
index 0000000000..e96a442720
--- /dev/null
+++ b/application/Espo/Resources/metadata/entityAcl/UserState.json
@@ -0,0 +1,3 @@
+{
+ "systemWriteForbidden": true
+}
diff --git a/application/Espo/Resources/metadata/entityDefs/SystemState.json b/application/Espo/Resources/metadata/entityDefs/SystemState.json
new file mode 100644
index 0000000000..d98c2d4cb8
--- /dev/null
+++ b/application/Espo/Resources/metadata/entityDefs/SystemState.json
@@ -0,0 +1,15 @@
+{
+ "fields": {
+ "id": {
+ "type": "id",
+ "dbType": "string",
+ "maxLength": 1
+ },
+ "versionNumber": {
+ "type": "int",
+ "dbType": "bigint",
+ "default": 0
+ }
+ },
+ "noDeletedAttribute": true
+}
diff --git a/application/Espo/Resources/metadata/entityDefs/UserState.json b/application/Espo/Resources/metadata/entityDefs/UserState.json
new file mode 100644
index 0000000000..fb87d1b809
--- /dev/null
+++ b/application/Espo/Resources/metadata/entityDefs/UserState.json
@@ -0,0 +1,32 @@
+{
+ "fields": {
+ "id": {
+ "type": "id",
+ "dbType": "integer",
+ "autoincrement": true
+ },
+ "user": {
+ "type": "link"
+ },
+ "emailFiltersVersionNumber": {
+ "type": "int",
+ "dbType": "bigint",
+ "default": 0
+ }
+ },
+ "links": {
+ "user": {
+ "type": "belongsTo",
+ "entity": "User"
+ }
+ },
+ "indexes": {
+ "userId": {
+ "unique": true,
+ "columns": [
+ "userId"
+ ]
+ }
+ },
+ "noDeletedAttribute": true
+}
diff --git a/application/Espo/Tools/Currency/SyncManager.php b/application/Espo/Tools/Currency/SyncManager.php
index 6442b90c9a..efa8d26b11 100644
--- a/application/Espo/Tools/Currency/SyncManager.php
+++ b/application/Espo/Tools/Currency/SyncManager.php
@@ -33,6 +33,7 @@ use Espo\Core\Currency\ConfigDataProvider;
use Espo\Core\Utils\Config\ConfigWriter;
use Espo\Core\Utils\Config\SystemConfig;
use Espo\Core\Utils\DataCache;
+use Espo\Core\Utils\System\SystemState;
use Espo\Entities\CurrencyRecord;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
@@ -54,6 +55,7 @@ class SyncManager
private RateEntryProvider $rateEntryProvider,
private DataCache $dataCache,
private SystemConfig $systemConfig,
+ private SystemState $systemState,
) {}
public function sync(): void
@@ -127,6 +129,7 @@ class SyncManager
$this->syncToConfigInTransaction();
});
+ $this->getBumpVersionNumber();
$this->clearCache();
}
@@ -154,6 +157,7 @@ class SyncManager
$this->configWriter->set('currencyRates', $rates);
$this->configWriter->save();
+ $this->getBumpVersionNumber();
$this->clearCache();
}
@@ -165,4 +169,9 @@ class SyncManager
$this->dataCache->clear($this->cacheKey);
}
+
+ private function getBumpVersionNumber(): void
+ {
+ $this->systemState->bumpVersionNumber();
+ }
}