Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,372 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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;
use Espo\Core\Binding\Binder;
use Espo\Core\Binding\BindingProcessor;
use Espo\Core\Binding\Key\NamedClassKey;
/**
* Default binding for the dependency injection framework. Custom binding should be set up in
* `Espo\Modules\{ModuleName}\Binding` or `Espo\Custom\Binding`.
*
* @link https://docs.espocrm.com/development/di/#binding.
*/
class Binding implements BindingProcessor
{
public function process(Binder $binder): void
{
$this->bindServices($binder);
$this->bindCore($binder);
$this->bindMisc($binder);
$this->bindAcl($binder);
$this->bindWebSocket($binder);
$this->bindEmailAccount($binder);
}
private function bindServices(Binder $binder): void
{
$binder->bindService(
'Espo\\Core\\Application\\ApplicationParams',
'applicationParams'
);
$binder->bindService(
'Espo\\Core\\InjectableFactory',
'injectableFactory'
);
$binder->bindService(
'Espo\\Core\\Container',
'container'
);
$binder->bindService(
'Psr\\Container\\ContainerInterface',
'container'
);
$binder->bindService(
'Espo\\Core\\Utils\\Module',
'module'
);
$binder->bindService(
'Espo\\Core\\Utils\\Config',
'config'
);
$binder->bindService(
'Espo\\Core\\Utils\\File\\Manager',
'fileManager'
);
$binder->bindService(
'Espo\\ORM\\EntityManager',
'entityManager'
);
$binder->bindService(
'Espo\\Core\\ORM\\EntityManager',
'entityManager'
);
$binder->bindService(
'Espo\\ORM\\Defs',
'ormDefs'
);
$binder->bindService(
'Espo\\Core\\DataManager',
'dataManager'
);
$binder->bindService(
'Espo\\Core\\Utils\\Metadata',
'metadata'
);
$binder->bindService(
'Espo\\Core\\Utils\\Log',
'log'
);
$binder->bindService(
'Espo\\Core\\ApplicationState',
'applicationState'
);
$binder->bindService(
'Espo\\Core\\ApplicationUser',
'applicationUser'
);
$binder->bindService(
'Espo\\Core\\Authentication\\AuthToken\\Manager',
'authTokenManager'
);
$binder->bindService(
'Espo\\Core\\ServiceFactory',
'serviceFactory'
);
$binder->bindService(
'Espo\\Core\\Record\\ServiceContainer',
'recordServiceContainer'
);
$binder->bindService(
'Espo\\Core\\HookManager',
'hookManager'
);
$binder->bindService(
'Espo\\Core\\Utils\\NumberUtil',
'number'
);
$binder->bindService(
'Espo\\Core\\Utils\\DateTime',
'dateTime'
);
$binder->bindService(
'Espo\\Core\\Utils\\FieldUtil',
'fieldUtil'
);
$binder->bindService(
'Espo\\Core\\Mail\\EmailSender',
'emailSender'
);
$binder->bindService(
NamedClassKey::create('Espo\\Core\\Utils\\Language', 'baseLanguage'),
'baseLanguage'
);
$binder->bindService(
NamedClassKey::create('Espo\\Core\\Utils\\Language', 'defaultLanguage'),
'defaultLanguage'
);
$binder->bindService(
'Espo\\Core\\Utils\\Language',
'language'
);
$binder->bindService(
'Espo\\Core\\Formula\\Manager',
'formulaManager'
);
$binder->bindService(
NamedClassKey::create('Espo\\Core\\AclManager', 'internalAclManager'),
'internalAclManager'
);
$binder->bindService(
'Espo\\Core\\AclManager',
'aclManager'
);
$binder->bindService(
'Espo\\Core\\Acl',
'acl'
);
$binder->bindService(
'Espo\\Entities\\Preferences',
'preferences'
);
$binder->bindService(
'Espo\\Entities\\User',
'user'
);
$binder->bindService(
'Espo\\Core\\Utils\\ClientManager',
'clientManager'
);
$binder->bindService(
'Espo\\Core\\ExternalAccount\\ClientManager',
'externalAccountClientManager'
);
$binder->bindService(
'Espo\\Core\\WebSocket\\Submission',
'webSocketSubmission'
);
$binder->bindService(
'Espo\\Tools\\Stream\\Service',
'streamService'
);
$binder->bindService(
'Espo\\Core\\Utils\\Config\\SystemConfig',
'systemConfig'
);
$binder->bindService(
'Espo\\Core\\Utils\\Config\\ApplicationConfig',
'applicationConfig'
);
}
private function bindCore(Binder $binder): void
{
$binder->bindImplementation(
'Espo\\ORM\\PDO\\PDOProvider',
'Espo\\ORM\\PDO\\DefaultPDOProvider'
);
$binder->bindImplementation(
'Espo\\Core\\Utils\\Database\\ConfigDataProvider',
'Espo\\Core\\Utils\\Database\\DefaultConfigDataProvider'
);
$binder->bindImplementation(
'Espo\\Core\\Job\\JobScheduler\\Creator',
'Espo\\Core\\Job\\JobScheduler\\Creators\\EntityCreator',
);
}
private function bindMisc(Binder $binder): void
{
$binder->bindImplementation(
'Espo\\Core\\Utils\\Id\\RecordIdGenerator',
'Espo\\Core\\Utils\\Id\\DefaultRecordIdGenerator'
);
$binder->bindFactory(
'Espo\\Core\\Sms\\Sender',
'Espo\\Core\\Sms\\SenderFactory'
);
$binder->bindImplementation(
'Espo\\Core\\Authentication\\Jwt\\KeyFactory',
'Espo\\Core\\Authentication\\Jwt\\DefaultKeyFactory'
);
$binder
->for('Espo\\Core\\Authentication\\Oidc\\TokenValidator')
->bindImplementation(
'Espo\\Core\\Authentication\\Jwt\\SignatureVerifierFactory',
'Espo\\Core\\Authentication\\Oidc\\DefaultSignatureVerifierFactory'
);
$binder
->for('Espo\\Core\\Authentication\\Oidc\\Login')
->bindImplementation(
'Espo\\Core\\Authentication\\Oidc\\UserProvider',
'Espo\\Core\\Authentication\\Oidc\\UserProvider\\DefaultUserProvider'
);
$binder->bindImplementation(
'Espo\\Core\\Mail\\Importer\\ParentFinder',
'Espo\\Core\\Mail\\Importer\\DefaultParentFinder'
);
$binder->bindImplementation(
'Espo\\Core\\Mail\\Importer\\DuplicateFinder',
'Espo\\Core\\Mail\\Importer\\DefaultDuplicateFinder'
);
$binder->bindImplementation(
'Espo\\Tools\\Api\\Cors\\Helper',
'Espo\\Tools\\Api\\Cors\\DefaultHelper'
);
$binder->bindImplementation(
'Espo\\Core\\Record\\ActionHistory\\ActionLogger',
'Espo\\Core\\Record\\ActionHistory\\DefaultActionLogger'
);
$binder->bindImplementation(
'Espo\\Core\\Mail\\Importer',
'Espo\\Core\\Mail\\Importer\\DefaultImporter'
);
$binder->bindImplementation(
'Espo\\Core\\Mail\\Importer\\AutoReplyDetector',
'Espo\\Core\\Mail\\Importer\\DefaultAutoReplyDetector'
);
}
private function bindAcl(Binder $binder): void
{
$binder->bindImplementation(
'Espo\\Core\\Acl\\Table\\TableFactory',
'Espo\\Core\\Acl\\Table\\DefaultTableFactory'
);
}
private function bindWebSocket(Binder $binder): void
{
$binder->bindFactory(
'Espo\\Core\\WebSocket\\Subscriber',
'Espo\\Core\\WebSocket\\SubscriberFactory'
);
$binder->bindFactory(
'Espo\\Core\\WebSocket\\Sender',
'Espo\\Core\\WebSocket\\SenderFactory'
);
}
private function bindEmailAccount(Binder $binder): void
{
$binder
->for('Espo\\Core\\Mail\\Account\\PersonalAccount\\Service')
->bindFactory(
'Espo\\Core\\Mail\\Account\\Fetcher',
'Espo\\Core\\Mail\\Account\\PersonalAccount\\FetcherFactory'
)
->bindImplementation(
'Espo\\Core\\Mail\\Account\\StorageFactory',
'Espo\\Core\\Mail\\Account\\PersonalAccount\\StorageFactory'
);
$binder
->for('Espo\\Core\\Mail\\Account\\GroupAccount\\Service')
->bindFactory(
'Espo\\Core\\Mail\\Account\\Fetcher',
'Espo\\Core\\Mail\\Account\\GroupAccount\\FetcherFactory'
)
->bindImplementation(
'Espo\\Core\\Mail\\Account\\StorageFactory',
'Espo\\Core\\Mail\\Account\\GroupAccount\\StorageFactory'
);
}
}

View File

@@ -0,0 +1,46 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\ActionHistoryRecord;
use Espo\Entities\ActionHistoryRecord;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<ActionHistoryRecord>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
return $entity->get('userId') === $user->getId();
}
}

View File

@@ -0,0 +1,158 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Attachment;
use Espo\Core\Name\Field;
use Espo\Entities\Attachment;
use Espo\Entities\Note;
use Espo\Entities\Settings;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\AclManager;
use Espo\Core\ORM\EntityManager;
/**
* @implements AccessEntityCREDChecker<Attachment>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
private AclManager $aclManager,
private EntityManager $entityManager
) {
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->getParentType() === Settings::ENTITY_TYPE) {
// Allow the logo.
return true;
}
$parent = null;
$parentType = $entity->getParentType();
$parentId = $entity->getParent()?->getId();
$relatedType = $entity->getRelatedType();
$relatedId = $entity->getRelated()?->getId();
if ($parentId && $parentType) {
$parent = $this->entityManager->getEntityById($parentType, $parentId);
} else if ($relatedId && $relatedType) {
$parent = $this->entityManager->getEntityById($relatedType, $relatedId);
}
if (!$parent) {
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
return false;
}
if ($parent->getEntityType() === Note::ENTITY_TYPE) {
/** @var Note $parent */
$result = $this->checkEntityReadNoteParent($user, $parent);
if ($result !== null) {
return $result;
}
} else if ($this->aclManager->checkEntity($user, $parent)) {
if (
$entity->getTargetField() &&
!$this->aclManager->checkField($user, $parent->getEntityType(), $entity->getTargetField())
) {
return false;
}
return true;
}
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
return false;
}
private function checkEntityReadNoteParent(User $user, Note $note): ?bool
{
if ($note->getTargetType() === Note::TARGET_TEAMS) {
$intersect = array_intersect(
$note->getLinkMultipleIdList(Field::TEAMS),
$user->getLinkMultipleIdList(Field::TEAMS)
);
if (count($intersect)) {
return true;
}
return null;
}
if ($note->getTargetType() === Note::TARGET_USERS) {
$isRelated = $this->entityManager
->getRDBRepository(Note::ENTITY_TYPE)
->getRelation($note, 'users')
->isRelated($user);
if ($isRelated) {
return true;
}
return null;
}
if ($note->getTargetType() === Note::TARGET_ALL) {
return true;
}
if (!$note->getParentId() || !$note->getParentType()) {
return null;
}
$parent = $this->entityManager->getEntityById($note->getParentType(), $note->getParentId());
if ($parent && $this->aclManager->checkEntity($user, $parent)) {
return true;
}
return null;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Attachment;
use Espo\Entities\Attachment;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Attachment>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
private const ATTR_CREATED_BY_ID = 'createdById';
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get(self::ATTR_CREATED_BY_ID)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\AuthToken;
use Espo\Entities\AuthToken;
use Espo\Entities\User;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
/**
* @implements AccessEntityCREDChecker<AuthToken>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
public function __construct(DefaultAccessChecker $defaultAccessChecker)
{
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function checkCreate(User $user, ScopeData $data): bool
{
return false;
}
}

View File

@@ -0,0 +1,170 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Email;
use Espo\Core\Name\Field;
use Espo\Entities\User;
use Espo\Entities\Email;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDSChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Table;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
/**
* @implements AccessEntityCREDSChecker<Email>
*/
class AccessChecker implements AccessEntityCREDSChecker
{
use DefaultAccessCheckerDependency;
public function __construct(DefaultAccessChecker $defaultAccessChecker)
{
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
/** @var Email $entity */
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
if ($data->isFalse()) {
return false;
}
if ($data->getRead() === Table::LEVEL_NO) {
return false;
}
if (!$entity->has('usersIds')) {
$entity->loadLinkMultipleField('users');
}
$userIdList = $entity->get('usersIds');
if (is_array($userIdList) && in_array($user->getId(), $userIdList)) {
return true;
}
return false;
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
/** @var Email $entity */
if ($user->isAdmin()) {
return true;
}
if ($data->isFalse()) {
return false;
}
if ($data->getDelete() === Table::LEVEL_OWN) {
if ($user->getId() === $entity->get('assignedUserId')) {
return true;
}
if ($user->getId() === $entity->get('createdById')) {
return true;
}
$assignedUserIdList = $entity->getLinkMultipleIdList(Field::ASSIGNED_USERS);
if (
count($assignedUserIdList) === 1 &&
$entity->hasLinkMultipleId(Field::ASSIGNED_USERS, $user->getId())
) {
return true;
}
return false;
}
if ($this->defaultAccessChecker->checkEntityDelete($user, $entity, $data)) {
return true;
}
if ($data->getEdit() === Table::LEVEL_NO && $data->getCreate() === Table::LEVEL_NO) {
return false;
}
if ($entity->get('createdById') !== $user->getId()) {
return false;
}
if (
$entity->getStatus() !== Email::STATUS_SENT &&
$entity->getStatus() !== Email::STATUS_ARCHIVED
) {
return true;
}
return false;
}
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
/** @var Email $entity */
if (
$entity->getStatus() === Email::STATUS_DRAFT &&
$entity->getCreatedBy() &&
$entity->getCreatedBy()->getId() === $user->getId()
) {
return true;
}
return $this->defaultAccessChecker->checkEntityEdit($user, $entity, $data);
}
public function checkEdit(User $user, ScopeData $data): bool
{
if ($data->getCreate() === Table::LEVEL_YES) {
return true;
}
return $this->defaultAccessChecker->checkEdit($user, $data);
}
public function checkDelete(User $user, ScopeData $data): bool
{
if ($data->getCreate() === Table::LEVEL_YES) {
return true;
}
return $this->defaultAccessChecker->checkDelete($user, $data);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Email;
use Espo\Core\Acl\AssignmentChecker as AssignmentCheckerInterface;
use Espo\Entities\Email;
use Espo\Entities\User;
use Espo\ORM\Entity;
/**
* @implements AssignmentCheckerInterface<Email>
*/
class AssignmentChecker implements AssignmentCheckerInterface
{
public function __construct(
private AssignmentCheckerInterface\Helper $helper,
) {}
public function check(User $user, Entity $entity): bool
{
if ($entity->getAssignedUser() && !$this->helper->checkAssignedUser($user, $entity)) {
return false;
}
if ($entity->getTeams()->getIdList() !== [] && !$this->helper->checkTeams($user, $entity)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Email\LinkCheckers;
use Espo\Core\Acl\LinkChecker;
use Espo\Core\AclManager;
use Espo\Entities\Email;
use Espo\Entities\User;
use Espo\ORM\Entity;
/**
* @implements LinkChecker<Email, Entity>
* @noinspection PhpUnused
*/
class ParentLinkChecker implements LinkChecker
{
public function __construct(
private AclManager $aclManager
) {}
public function check(User $user, Entity $entity, Entity $foreignEntity): bool
{
if ($this->aclManager->checkEntityRead($user, $foreignEntity)) {
return true;
}
$replied = $entity->getReplied();
if (!$replied) {
return false;
}
$parent = $replied->getParent();
if (
!$parent ||
$parent->getId() !== $foreignEntity->getId() ||
$parent->getEntityType() !== $foreignEntity->getEntityType()
) {
return false;
}
return $this->aclManager->checkEntityRead($user, $replied);
}
}

View File

@@ -0,0 +1,67 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Email\LinkCheckers;
use Espo\Core\Acl\LinkChecker;
use Espo\Core\AclManager;
use Espo\Entities\Email;
use Espo\Entities\Team;
use Espo\Entities\User;
use Espo\ORM\Entity;
/**
* @implements LinkChecker<Email, Team>
* @noinspection PhpUnused
*/
class TeamsLinkChecker implements LinkChecker
{
public function __construct(
private AclManager $aclManager
) {}
public function check(User $user, Entity $entity, Entity $foreignEntity): bool
{
if ($this->aclManager->checkEntityRead($user, $foreignEntity)) {
return true;
}
$replied = $entity->getReplied();
if (!$replied) {
return false;
}
if ($replied->getTeams()->hasId($foreignEntity->getId())) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Email;
use Espo\Entities\User;
use Espo\Entities\Email;
use Espo\ORM\Entity;
use Espo\Core\Acl\DefaultOwnershipChecker;
use Espo\Core\Acl\OwnershipOwnChecker;
use Espo\Core\Acl\OwnershipTeamChecker;
/**
* @implements OwnershipOwnChecker<Email>
* @implements OwnershipTeamChecker<Email>
*/
class OwnershipChecker implements OwnershipOwnChecker, OwnershipTeamChecker
{
public function __construct(private DefaultOwnershipChecker $defaultOwnershipChecker)
{}
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->getAssignedUser()?->getId()) {
return true;
}
if ($user->getId() === $entity->getCreatedBy()?->getId()) {
return true;
}
if ($entity->getAssignedUsers()->hasId($user->getId())) {
return true;
}
return false;
}
public function checkTeam(User $user, Entity $entity): bool
{
return $this->defaultOwnershipChecker->checkTeam($user, $entity);
}
}

View File

@@ -0,0 +1,87 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\EmailFilter;
use Espo\Entities\EmailAccount;
use Espo\Entities\User;
use Espo\Entities\EmailFilter;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
use Espo\Core\ORM\EntityManager;
/**
* @implements OwnershipOwnChecker<EmailFilter>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @param EmailFilter $entity
*/
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->isGlobal()) {
return false;
}
$parentType = $entity->getParentType();
$parentId = $entity->getParentId();
if (!$parentType || !$parentId) {
return false;
}
$parent = $this->entityManager->getEntityById($parentType, $parentId);
if (!$parent) {
return false;
}
if ($parent->getEntityType() === User::ENTITY_TYPE) {
return $parent->getId() === $user->getId();
}
if (
$parent instanceof EmailAccount &&
$parent->has('assignedUserId') &&
$parent->get('assignedUserId') === $user->getId()
) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Import;
use Espo\Entities\Import;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityDeleteChecker;
use Espo\Core\Acl\AccessEntityReadChecker;
use Espo\Core\Acl\ScopeData;
/**
* @implements AccessEntityReadChecker<Import>
* @implements AccessEntityDeleteChecker<Import>
*/
class AccessChecker implements AccessEntityReadChecker, AccessEntityDeleteChecker
{
public function check(User $user, ScopeData $data): bool
{
return $data->isTrue();
}
public function checkRead(User $user, ScopeData $data): bool
{
return $data->isTrue();
}
public function checkDelete(User $user, ScopeData $data): bool
{
return $data->isTrue();
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\ImportEml;
use Espo\Core\Acl\AccessCreateChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Entities\User;
class AccessChecker implements AccessCreateChecker
{
public function check(User $user, ScopeData $data): bool
{
return $data->isTrue();
}
public function checkCreate(User $user, ScopeData $data): bool
{
return $data->isTrue();
}
}

View File

@@ -0,0 +1,235 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Note;
use Espo\Core\Acl\Permission;
use Espo\Core\Acl\Table;
use Espo\Core\Name\Field;
use Espo\Entities\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\AclManager;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\Config;
use DateTime;
use Exception;
/**
* @implements AccessEntityCREDChecker<Note>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
private const EDIT_PERIOD = '7 days';
private const DELETE_PERIOD = '1 month';
private DefaultAccessChecker $defaultAccessChecker;
private AclManager $aclManager;
private EntityManager $entityManager;
private Config $config;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager,
EntityManager $entityManager,
Config $config
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
$this->config = $config;
}
/**
* @param Note $entity
*/
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
$parentId = $entity->get('parentId');
$parentType = $entity->get('parentType');
if (!$parentId || !$parentType) {
return true;
}
$parent = $this->entityManager->getEntityById($parentType, $parentId);
if ($parent && $this->aclManager->checkEntityStream($user, $parent)) {
return true;
}
return false;
}
/**
* @param Note $entity
*/
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
$parentId = $entity->getParentId();
$parentType = $entity->getParentType();
if ($parentId && $parentType) {
$parent = $this->entityManager->getEntityById($parentType, $parentId);
if (!$parent) {
return false;
}
return $this->aclManager->checkEntityStream($user, $parent);
}
if ($entity->getType() !== Note::TYPE_POST) {
return false;
}
if ($entity->getCreatedById() === $user->getId()) {
return true;
}
if ($entity->getTargetType() === Note::TARGET_ALL) {
return true;
}
if ($entity->getTargetType() === Note::TARGET_TEAMS) {
$targetTeamIdList = $entity->getLinkMultipleIdList(Field::TEAMS);
foreach ($user->getTeamIdList() as $teamId) {
if (in_array($teamId, $targetTeamIdList)) {
return true;
}
}
return false;
}
if ($entity->getTargetType() === Note::TARGET_USERS) {
return in_array($user->getId(), $entity->getLinkMultipleIdList('users'));
}
if ($entity->getTargetType() === Note::TARGET_PORTALS) {
return $this->aclManager->getPermissionLevel($user, Permission::PORTAL) === Table::LEVEL_YES;
}
return false;
}
/**
* @param Note $entity
*/
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if (!$this->defaultAccessChecker->checkEntityEdit($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return false;
}
$createdAt = $entity->get(Field::CREATED_AT);
if (!$createdAt) {
return true;
}
$noteEditThresholdPeriod =
'-' . $this->config->get('noteEditThresholdPeriod', self::EDIT_PERIOD);
$dt = new DateTime();
$dt->modify($noteEditThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
/**
* @param Note $entity
*/
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if (!$this->defaultAccessChecker->checkEntityDelete($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return false;
}
$createdAt = $entity->get(Field::CREATED_AT);
if (!$createdAt) {
return true;
}
$deleteThresholdPeriod =
'-' . $this->config->get('noteDeleteThresholdPeriod', self::DELETE_PERIOD);
$dt = new DateTime();
$dt->modify($deleteThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Note;
use Espo\Entities\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Note>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
/**
* @param Note $entity
*/
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->getType() === Note::TYPE_POST && $user->getId() === $entity->getCreatedById()) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Notification;
use Espo\Entities\Notification;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Notification>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get('userId')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Portal;
use Espo\Core\Acl\Permission;
use Espo\Entities\Portal;
use Espo\Entities\User;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Table;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\AclManager;
/**
* @implements AccessEntityCREDChecker<Portal>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
public function __construct(private DefaultAccessChecker $defaultAccessChecker, private AclManager $aclManager)
{}
public function check(User $user, ScopeData $data): bool
{
$level = $this->aclManager->getPermissionLevel($user, Permission::PORTAL);
return $level === Table::LEVEL_YES;
}
}

View File

@@ -0,0 +1,89 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\ScheduledJob;
use Espo\Entities\ScheduledJob;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
/**
* @implements AccessEntityCREDChecker<ScheduledJob>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
private DefaultAccessChecker $defaultAccessChecker;
public function __construct(DefaultAccessChecker $defaultAccessChecker)
{
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('isInternal')) {
return false;
}
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('isInternal')) {
return false;
}
return $this->defaultAccessChecker->checkEntityRead($user, $entity, $data);
}
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('isInternal')) {
return false;
}
return $this->defaultAccessChecker->checkEntityEdit($user, $entity, $data);
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->get('isInternal')) {
return false;
}
return $this->defaultAccessChecker->checkEntityDelete($user, $entity, $data);
}
}

View File

@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Team;
use Espo\Core\Name\Field;
use Espo\Entities\Team;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Team>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
$userTeamIdList = $user->getLinkMultipleIdList(Field::TEAMS);
return in_array($entity->getId(), $userTeamIdList);
}
}

View File

@@ -0,0 +1,129 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\User;
use Espo\Core\Acl\Permission;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDSChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Table;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\AclManager;
/**
* @implements AccessEntityCREDSChecker<User>
*/
class AccessChecker implements AccessEntityCREDSChecker
{
use DefaultAccessCheckerDependency;
public function __construct(
private DefaultAccessChecker $defaultAccessChecker,
private AclManager $aclManager,
) {}
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
if (!$user->isAdmin()) {
return false;
}
if ($entity->isSuperAdmin() && !$user->isSuperAdmin()) {
return false;
}
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if (!$user->isAdmin() && !$entity->isActive()) {
return false;
}
if ($entity->isSuperAdmin() && !$user->isSuperAdmin()) {
return false;
}
if ($entity->isSystem()) {
return false;
}
if ($entity->isPortal()) {
return $this->aclManager->getPermissionLevel($user, Permission::PORTAL) === Table::LEVEL_YES;
}
return $this->defaultAccessChecker->checkEntityRead($user, $entity, $data);
}
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
if ($entity->isSystem()) {
return false;
}
if (!$user->isAdmin()) {
if ($user->getId() !== $entity->getId()) {
return false;
}
}
if ($entity->isSuperAdmin() && !$user->isSuperAdmin()) {
return false;
}
return $this->defaultAccessChecker->checkEntityEdit($user, $entity, $data);
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if (!$user->isAdmin()) {
return false;
}
if ($entity->isSystem()) {
return false;
}
if ($entity->isSuperAdmin() && !$user->isSuperAdmin()) {
return false;
}
return $this->defaultAccessChecker->checkEntityDelete($user, $entity, $data);
}
public function checkEntityStream(User $user, Entity $entity, ScopeData $data): bool
{
/** @noinspection PhpRedundantOptionalArgumentInspection */
return $this->aclManager->checkUserPermission($user, $entity, Permission::USER);
}
}

View File

@@ -0,0 +1,65 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\User;
use Espo\Core\Name\Field;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\Acl\OwnershipOwnChecker;
use Espo\Core\Acl\OwnershipTeamChecker;
/**
* @implements OwnershipOwnChecker<User>
* @implements OwnershipTeamChecker<User>
*/
class OwnershipChecker implements OwnershipOwnChecker, OwnershipTeamChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
return $user->getId() === $entity->getId();
}
public function checkTeam(User $user, Entity $entity): bool
{
assert($entity instanceof CoreEntity);
$intersect = array_intersect(
$user->getLinkMultipleIdList(Field::TEAMS),
$entity->getLinkMultipleIdList(Field::TEAMS)
);
if (count($intersect)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,105 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Webhook;
use Espo\Entities\User;
use Espo\Entities\Webhook;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\DefaultAccessChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Traits\DefaultAccessCheckerDependency;
/**
* @implements AccessEntityCREDChecker<Webhook>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
public function __construct(DefaultAccessChecker $defaultAccessChecker)
{
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function check(User $user, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if (!$user->isApi()) {
return false;
}
if ($data->isFalse()) {
return false;
}
return true;
}
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
return $this->checkEntityInternal($user, $entity, $data);
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
return $this->checkEntityInternal($user, $entity, $data);
}
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
return $this->checkEntityInternal($user, $entity, $data);
}
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
return $this->checkEntityInternal($user, $entity, $data);
}
private function checkEntityInternal(User $user, Entity $entity, ScopeData $data): bool
{
if ($user->isAdmin()) {
return true;
}
if ($data->isFalse()) {
return false;
}
if ($user->isApi() && $user->getId() === $entity->get('userId')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,46 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\Webhook;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<\Espo\Entities\Webhook>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
return $user->getId() === $entity->get('userId') && $user->isApi();
}
}

View File

@@ -0,0 +1,90 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Acl\WorkingTimeRange;
use Espo\Core\Acl\AssignmentChecker as AssignmentCheckerInterface;
use Espo\Core\Acl\DefaultAssignmentChecker;
use Espo\Core\AclManager;
use Espo\Entities\User;
use Espo\Entities\WorkingTimeRange;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
/**
* @implements AssignmentCheckerInterface<WorkingTimeRange>
*/
class AssignmentChecker implements AssignmentCheckerInterface
{
private DefaultAssignmentChecker $defaultAssignmentChecker;
private AclManager $aclManager;
private EntityManager $entityManager;
public function __construct(
DefaultAssignmentChecker $defaultAssignmentChecker,
AclManager $aclManager,
EntityManager $entityManager
) {
$this->defaultAssignmentChecker = $defaultAssignmentChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
}
/**
* @param WorkingTimeRange $entity
*/
public function check(User $user, Entity $entity): bool
{
$result = $this->defaultAssignmentChecker->check($user, $entity);
if (!$result) {
return false;
}
if (!$entity->isAttributeChanged('usersIds')) {
return true;
}
$users = $this->entityManager
->getRDBRepositoryByClass(User::class)
->where([Attribute::ID => $entity->getUsers()->getIdList()])
->find();
foreach ($users as $targetUser) {
$accessToUser = $this->aclManager->check($user, $targetUser);
if (!$accessToUser) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,165 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Attachment;
use Espo\Entities\Attachment;
use Espo\Entities\Note;
use Espo\Entities\Settings;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Portal\Acl\DefaultAccessChecker;
use Espo\Core\Portal\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\Portal\AclManager;
/**
* @implements AccessEntityCREDChecker<Attachment>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
private DefaultAccessChecker $defaultAccessChecker;
private AclManager $aclManager;
private EntityManager $entityManager;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager,
EntityManager $entityManager
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
/** @var Attachment $entity */
if ($entity->get('parentType') === Settings::ENTITY_TYPE) {
// Allow the logo.
return true;
}
$parent = null;
$parentType = $entity->get('parentType');
$parentId = $entity->get('parentId');
$relatedType = $entity->get('relatedType');
$relatedId = $entity->get('relatedId');
if ($parentId && $parentType) {
$parent = $this->entityManager->getEntityById($parentType, $parentId);
} else if ($relatedId && $relatedType) {
$parent = $this->entityManager->getEntityById($relatedType, $relatedId);
}
if (!$parent) {
if ($entity->get('createdById') === $user->getId()) {
return true;
}
return false;
}
if ($parent->getEntityType() === Note::ENTITY_TYPE) {
/** @var Note $parent */
$result = $this->checkEntityReadNoteParent($user, $parent);
if ($result !== null) {
return $result;
}
} else if ($this->aclManager->checkEntity($user, $parent)) {
if (
$entity->getTargetField() &&
!$this->aclManager->checkField($user, $parent->getEntityType(), $entity->getTargetField())
) {
return false;
}
return true;
}
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
return false;
}
private function checkEntityReadNoteParent(User $user, Note $note): ?bool
{
if ($note->isInternal()) {
return false;
}
if ($note->getTargetType() === Note::TARGET_PORTALS) {
$intersect = array_intersect(
$note->getLinkMultipleIdList('portals'),
$user->getLinkMultipleIdList('portals')
);
if (count($intersect)) {
return true;
}
return false;
}
if ($note->getTargetType() === Note::TARGET_USERS) {
$isRelated = $this->entityManager
->getRDBRepository(Note::ENTITY_TYPE)
->getRelation($note, 'users')
->isRelated($user);
if ($isRelated) {
return true;
}
return false;
}
if (!$note->getParentId() || !$note->getParentType()) {
return null;
}
$parent = $this->entityManager->getEntityById($note->getParentType(), $note->getParentId());
if ($parent && $this->aclManager->checkEntity($user, $parent)) {
return true;
}
return null;
}
}

View File

@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Attachment;
use Espo\Entities\Attachment;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Attachment>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
private const ATTR_CREATED_BY_ID = 'createdById';
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get(self::ATTR_CREATED_BY_ID)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,79 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Email;
use Espo\Entities\Email;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\Acl\AccessEntityCREDSChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\Acl\Table;
use Espo\Core\Portal\Acl\DefaultAccessChecker;
use Espo\Core\Portal\Acl\Traits\DefaultAccessCheckerDependency;
/**
* @implements AccessEntityCREDSChecker<Email>
*/
class AccessChecker implements AccessEntityCREDSChecker
{
use DefaultAccessCheckerDependency;
public function __construct(
DefaultAccessChecker $defaultAccessChecker
) {
$this->defaultAccessChecker = $defaultAccessChecker;
}
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
if ($this->defaultAccessChecker->checkEntityRead($user, $entity, $data)) {
return true;
}
if ($data->isFalse()) {
return false;
}
if ($data->getRead() === Table::LEVEL_NO) {
return false;
}
assert($entity instanceof CoreEntity);
$userIdList = $entity->getLinkMultipleIdList('users');
if (in_array($user->getId(), $userIdList)) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Email;
use Espo\Entities\Email;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Email>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get('createdById')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,201 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Note;
use Espo\Core\Name\Field;
use Espo\Entities\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\AccessEntityCREDChecker;
use Espo\Core\Acl\ScopeData;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Portal\Acl\DefaultAccessChecker;
use Espo\Core\Portal\Acl\Traits\DefaultAccessCheckerDependency;
use Espo\Core\Portal\AclManager;
use Espo\Core\Utils\Config;
use DateTime;
use Exception;
/**
* @implements AccessEntityCREDChecker<Note>
*/
class AccessChecker implements AccessEntityCREDChecker
{
use DefaultAccessCheckerDependency;
private const EDIT_PERIOD = '7 days';
private const DELETE_PERIOD = '1 month';
private DefaultAccessChecker $defaultAccessChecker;
private AclManager $aclManager;
private EntityManager $entityManager;
private Config $config;
public function __construct(
DefaultAccessChecker $defaultAccessChecker,
AclManager $aclManager,
EntityManager $entityManager,
Config $config
) {
$this->defaultAccessChecker = $defaultAccessChecker;
$this->aclManager = $aclManager;
$this->entityManager = $entityManager;
$this->config = $config;
}
/**
* @param Note $entity
*/
public function checkEntityCreate(User $user, Entity $entity, ScopeData $data): bool
{
$parentId = $entity->getParentId();
$parentType = $entity->getParentType();
if (!$parentId || !$parentType) {
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
$parent = $this->entityManager->getEntityById($parentType, $parentId);
if ($parent && $this->aclManager->checkEntityStream($user, $parent)) {
return true;
}
return $this->defaultAccessChecker->checkEntityCreate($user, $entity, $data);
}
/**
* @param Note $entity
*/
public function checkEntityRead(User $user, Entity $entity, ScopeData $data): bool
{
$parentId = $entity->getParentId();
$parentType = $entity->getParentType();
if ($parentId && $parentType) {
$parent = $this->entityManager->getEntityById($parentType, $parentId);
if (!$parent) {
return false;
}
return $this->aclManager->checkEntityStream($user, $parent);
}
if ($entity->getType() !== Note::TYPE_POST) {
return false;
}
if ($entity->getCreatedById() === $user->getId()) {
return true;
}
if ($entity->getTargetType() === Note::TARGET_PORTALS) {
return in_array($user->getPortalId(), $entity->getLinkMultipleIdList('portals'));
}
return false;
}
/**
* @param Note $entity
*/
public function checkEntityEdit(User $user, Entity $entity, ScopeData $data): bool
{
if (!$this->defaultAccessChecker->checkEntityEdit($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return false;
}
$createdAt = $entity->get(Field::CREATED_AT);
if (!$createdAt) {
return true;
}
$noteEditThresholdPeriod =
'-' . $this->config->get('noteEditThresholdPeriod', self::EDIT_PERIOD);
$dt = new DateTime();
$dt->modify($noteEditThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
/**
* @param Note $entity
*/
public function checkEntityDelete(User $user, Entity $entity, ScopeData $data): bool
{
if (!$this->defaultAccessChecker->checkEntityDelete($user, $entity, $data)) {
return false;
}
if (!$this->aclManager->checkOwnershipOwn($user, $entity)) {
return false;
}
$createdAt = $entity->get(Field::CREATED_AT);
if (!$createdAt) {
return true;
}
$deleteThresholdPeriod =
'-' . $this->config->get('noteDeleteThresholdPeriod', self::DELETE_PERIOD);
$dt = new DateTime();
$dt->modify($deleteThresholdPeriod);
try {
if ($dt->format('U') > (new DateTime($createdAt))->format('U')) {
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Note;
use Espo\Entities\Note;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Note>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
/**
* @param Note $entity
*/
public function checkOwn(User $user, Entity $entity): bool
{
if ($entity->getType() === Note::TYPE_POST && $user->getId() === $entity->getCreatedById()) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\Notification;
use Espo\Entities\Notification;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<Notification>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
if ($user->getId() === $entity->get('userId')) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,45 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AclPortal\User;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl\OwnershipOwnChecker;
/**
* @implements OwnershipOwnChecker<User>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
public function checkOwn(User $user, Entity $entity): bool
{
return $user->getId() === $entity->getId();
}
}

View File

@@ -0,0 +1,87 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AddressFormatters;
use Espo\Core\Field\Address;
use Espo\Core\Field\Address\AddressFormatter;
class Formatter1 implements AddressFormatter
{
public function format(Address $address): string
{
$result = '';
$street = $address->getStreet();
$city = $address->getCity();
$country = $address->getCountry();
$state = $address->getState();
$postalCode = $address->getPostalCode();
if ($street) {
$result .= $street;
}
if ($city || $state || $postalCode) {
if ($result) {
$result .= "\n";
}
if ($city) {
$result .= $city;
}
if ($state && $city) {
$result .= ', ';
}
if ($state) {
$result .= $state;
}
if ($postalCode && ($state || $city)) {
$result .= ' ';
}
if ($postalCode) {
$result .= $postalCode;
}
}
if ($country) {
if ($result) {
$result .= "\n";
}
$result .= $country;
}
return $result;
}
}

View File

@@ -0,0 +1,89 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AddressFormatters;
use Espo\Core\Field\Address;
use Espo\Core\Field\Address\AddressFormatter;
class Formatter2 implements AddressFormatter
{
public function format(Address $address): string
{
$result = '';
$street = $address->getStreet();
$city = $address->getCity();
$country = $address->getCountry();
$state = $address->getState();
$postalCode = $address->getPostalCode();
if ($street) {
$result .= $street;
}
if ($city || $postalCode) {
if ($result) {
$result .= "\n";
}
if ($postalCode) {
$result .= $postalCode;
}
if ($postalCode && $city) {
$result .= ' ';
}
if ($city) {
$result .= $city;
}
}
if ($state || $country) {
if ($result) {
$result .= "\n";
}
if ($state) {
$result .= $state;
}
if ($state && $country) {
$result .= ' ';
}
if ($country) {
$result .= $country;
}
}
return $result;
}
}

View File

@@ -0,0 +1,87 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AddressFormatters;
use Espo\Core\Field\Address;
use Espo\Core\Field\Address\AddressFormatter;
class Formatter3 implements AddressFormatter
{
public function format(Address $address): string
{
$result = '';
$street = $address->getStreet();
$city = $address->getCity();
$country = $address->getCountry();
$state = $address->getState();
$postalCode = $address->getPostalCode();
if ($country) {
$result .= $country;
}
if ($city || $state || $postalCode) {
if ($result) {
$result .= "\n";
}
if ($state) {
$result .= $state;
}
if ($state && $postalCode) {
$result .= ' ';
}
if ($postalCode) {
$result .= $postalCode;
}
if ($city && ($state || $postalCode)) {
$result .= ' ';
}
if ($city) {
$result .= $city;
}
}
if ($street) {
if ($result) {
$result .= "\n";
}
$result .= $street;
}
return $result;
}
}

View File

@@ -0,0 +1,87 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AddressFormatters;
use Espo\Core\Field\Address;
use Espo\Core\Field\Address\AddressFormatter;
class Formatter4 implements AddressFormatter
{
public function format(Address $address): string
{
$result = '';
$street = $address->getStreet();
$city = $address->getCity();
$country = $address->getCountry();
$state = $address->getState();
$postalCode = $address->getPostalCode();
if ($street) {
$result .= $street;
}
if ($city) {
if ($result) {
$result .= "\n";
}
$result .= $city;
}
if ($country || $state || $postalCode) {
if ($result) {
$result .= "\n";
}
if ($country) {
$result .= $country;
}
if ($state && $country) {
$result .= ' - ';
}
if ($state) {
$result .= $state;
}
if ($postalCode && ($state || $country)) {
$result .= ' ';
}
if ($postalCode) {
$result .= $postalCode;
}
}
return $result;
}
}

View File

@@ -0,0 +1,118 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppInfo;
use Espo\Core\Binding\Binding as BindingItem;
use Espo\Core\Binding\EspoBindingLoader;
use Espo\Core\Console\Command\Params;
use Espo\Core\Utils\Module;
class Binding
{
private Module $module;
public function __construct(Module $module)
{
$this->module = $module;
}
public function process(Params $params): string
{
$result = '';
$bindingLoader = new EspoBindingLoader($this->module);
$data = $bindingLoader->load();
$keyList = $data->getGlobalKeyList();
$result .= "Global:\n\n";
foreach ($keyList as $key) {
$result .= $this->printItem($key, $data->getGlobal($key));
}
$contextList = $data->getContextList();
foreach ($contextList as $context) {
$result .= "Context: {$context}\n\n";
$keyList = $data->getContextKeyList($context);
foreach ($keyList as $key) {
$result .= $this->printItem($key, $data->getContext($context, $key));
}
}
return $result;
}
private function printItem(string $key, BindingItem $binding): string
{
$result = '';
$tab = ' ';
$result .= $tab . "Key: {$key}\n";
$type = $binding->getType();
$value = $binding->getValue();
$typeString = [
BindingItem::IMPLEMENTATION_CLASS_NAME => 'Implementation',
BindingItem::CONTAINER_SERVICE => 'Service',
BindingItem::VALUE => 'Value',
BindingItem::CALLBACK => 'Callback',
BindingItem::FACTORY_CLASS_NAME => 'Factory',
][$type];
$result .= $tab . "Type: {$typeString}\n";
if ($type == BindingItem::IMPLEMENTATION_CLASS_NAME || $type == BindingItem::CONTAINER_SERVICE) {
$result .= $tab . "Value: {$value}\n";
}
if ($type == BindingItem::VALUE) {
if (is_string($value) || is_int($value) || is_float($value)) {
$result .= $tab . "Value: {$value}\n";
}
if (is_bool($value)) {
$valueString = $value ? 'true' : 'false';
$result .= $tab . "Value: {$valueString}\n";
}
}
$result .= "\n";
return $result;
}
}

View File

@@ -0,0 +1,101 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppInfo;
use Espo\Core\Console\Command\Params;
use Espo\Core\Container as ContainerService;
use Espo\Core\Utils\Metadata;
class Container
{
public function __construct(private ContainerService $container, private Metadata $metadata)
{}
public function process(Params $params): string
{
$nameOnly = $params->hasFlag('nameOnly');
$result = '';
$serviceList = [
'injectableFactory',
'config',
'log',
'fileManager',
'dataManager',
'metadata',
'user',
];
/** @var string[] $fileList */
$fileList = scandir('application/Espo/Core/Loaders');
if (file_exists('custom/Espo/Custom/Core/Loaders')) {
$fileList = array_merge($fileList, scandir('custom/Espo/Custom/Core/Loaders') ?: []);
}
foreach ($fileList as $file) {
if (substr($file, -4) === '.php') {
$name = lcfirst(substr($file, 0, -4));
if (!in_array($name, $serviceList) && $this->container->has($name)) {
$serviceList[] = $name;
}
}
}
foreach ($this->metadata->get(['app', 'containerServices']) ?? [] as $name => $data) {
if (!in_array($name, $serviceList)) {
$serviceList[] = $name;
}
}
sort($serviceList);
if ($nameOnly) {
foreach ($serviceList as $name) {
$result .= $name . "\n";
}
return $result;
}
foreach ($serviceList as $name) {
$result .= $name . "\n";
$obj = $this->container->get($name);
$result .= get_class($obj) . "\n";
$result .= "\n";
}
return $result;
}
}

View File

@@ -0,0 +1,68 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppInfo;
use Espo\Core\Console\Command\Params;
use Espo\Core\Utils\ClassFinder;
use Espo\Core\Job\MetadataProvider;
class Jobs
{
private $classFinder;
private $metadataProvider;
public function __construct(ClassFinder $classFinder, MetadataProvider $metadataProvider)
{
$this->classFinder = $classFinder;
$this->metadataProvider = $metadataProvider;
}
public function process(Params $params): string
{
$result = "Available jobs:\n\n";
$list = array_map(
function ($item) {
return ' ' . $item;
},
array_unique(
array_merge(
array_keys($this->classFinder->getMap('Jobs')),
$this->metadataProvider->getScheduledJobNameList()
)
)
);
asort($list);
return $result . implode("\n", $list) . "\n";
}
}

View File

@@ -0,0 +1,48 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppParams;
use Espo\Core\Utils\Address\CountryDataProvider;
use Espo\Tools\App\AppParam;
class AddressCountryData implements AppParam
{
public function __construct(
private CountryDataProvider $provider
) {}
/**
* @return array{list: string[], preferredList: string[]}
*/
public function get(): array
{
return $this->provider->get();
}
}

View File

@@ -0,0 +1,92 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppParams;
use Espo\Entities\Extension;
use Espo\Entities\User;
use Espo\ORM\EntityManager;
use Espo\Tools\App\AppParam;
use stdClass;
class Extensions implements AppParam
{
private User $user;
private EntityManager $entityManager;
public function __construct(
User $user,
EntityManager $entityManager
) {
$this->user = $user;
$this->entityManager = $entityManager;
}
/**
* @return stdClass[]
*/
public function get(): array
{
if (!$this->user->isRegular() && !$this->user->isAdmin()) {
return [];
}
$extensionList = $this->entityManager
->getRDBRepositoryByClass(Extension::class)
->where([
'licenseStatus' => [
Extension::LICENSE_STATUS_INVALID,
Extension::LICENSE_STATUS_EXPIRED,
Extension::LICENSE_STATUS_SOFT_EXPIRED,
],
])
->find();
$list = [];
foreach ($extensionList as $extension) {
$list[] = (object) [
'name' => $extension->getName(),
'version' => $extension->getVersion(),
'licenseStatus' => $extension->getLicenseStatus(),
'licenseStatusMessage' => $extension->getLicenseStatusMessage(),
'isInstalled' => $extension->isInstalled(),
'notify' => in_array(
$extension->getLicenseStatus(),
[
Extension::LICENSE_STATUS_INVALID,
Extension::LICENSE_STATUS_EXPIRED,
]
)
];
}
return $list;
}
}

View File

@@ -0,0 +1,91 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AppParams;
use Espo\Core\Acl;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Select\SelectBuilderFactory;
use Espo\Entities\Template;
use Espo\Tools\App\AppParam;
use RuntimeException;
/**
* Returns a list of entity types for which a PDF template exists.
*
* @noinspection PhpUnused
*/
class TemplateEntityTypeList implements AppParam
{
public function __construct(
private Acl $acl,
private SelectBuilderFactory $selectBuilderFactory,
private EntityManager $entityManager,
) {}
/**
* @return string[]
*/
public function get(): array
{
if (!$this->acl->checkScope(Template::ENTITY_TYPE)) {
return [];
}
$list = [];
try {
$query = $this->selectBuilderFactory
->create()
->from(Template::ENTITY_TYPE)
->withAccessControlFilter()
->buildQueryBuilder()
->select(['entityType'])
->where(['status' => Template::STATUS_ACTIVE])
->group(['entityType'])
->build();
} catch (BadRequest|Forbidden $e) {
throw new RuntimeException('', 0, $e);
}
$templateCollection = $this->entityManager
->getRDBRepositoryByClass(Template::class)
->clone($query)
->find();
foreach ($templateCollection as $template) {
$list[] = $template->getTargetEntityType();
}
return $list;
}
}

View File

@@ -0,0 +1,289 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\AssignmentNotificators;
use Espo\Core\Field\DateTime;
use Espo\Core\Field\LinkParent;
use Espo\Core\Name\Field;
use Espo\Core\Notification\DefaultAssignmentNotificator;
use Espo\Entities\EmailAddress;
use Espo\Entities\EmailFolder;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\Name\Attribute;
use Espo\Tools\Stream\Service as StreamService;
use Espo\Core\Notification\AssignmentNotificator;
use Espo\Core\Notification\AssignmentNotificator\Params;
use Espo\Core\Notification\UserEnabledChecker;
use Espo\Core\AclManager;
use Espo\ORM\EntityManager;
use Espo\ORM\Entity;
use Espo\Entities\User;
use Espo\Entities\Notification;
use Espo\Entities\Email as EmailEntity;
use Espo\Repositories\Email as EmailRepository;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Tools\Email\Util;
/**
* @implements AssignmentNotificator<EmailEntity>
*/
class Email implements AssignmentNotificator
{
private const DAYS_THRESHOLD = 2;
public function __construct(
private User $user,
private EntityManager $entityManager,
private UserEnabledChecker $userChecker,
private AclManager $aclManager,
private StreamService $streamService,
private DefaultAssignmentNotificator $defaultAssignmentNotificator,
) {}
/**
* @param EmailEntity $entity
*/
public function process(Entity $entity, Params $params): void
{
if (
!in_array(
$entity->getStatus(),
[
EmailEntity::STATUS_ARCHIVED,
EmailEntity::STATUS_SENT,
EmailEntity::STATUS_BEING_IMPORTED,
]
)
) {
return;
}
if (
$entity->getStatus() !== EmailEntity::STATUS_BEING_IMPORTED &&
!$this->streamService->checkIsEnabled(EmailEntity::ENTITY_TYPE)
) {
$this->defaultAssignmentNotificator->process(
$entity,
$params->withOption(DefaultAssignmentNotificator::OPTION_FORCE_ASSIGNED_USER, true)
);
}
if ($params->getOption(EmailEntity::SAVE_OPTION_IS_JUST_SENT)) {
$previousUserIdList = [];
} else {
$previousUserIdList = $entity->getFetched('usersIds');
if (!is_array($previousUserIdList)) {
$previousUserIdList = [];
}
}
$dateSent = $entity->getDateSent();
if (!$dateSent) {
return;
}
if ($dateSent->diff(DateTime::createNow())->days > self::DAYS_THRESHOLD) {
return;
}
$emailUserIdList = $entity->get('usersIds');
if (!is_array($emailUserIdList)) {
return;
}
$userIdList = [];
foreach ($emailUserIdList as $userId) {
if (
!in_array($userId, $userIdList) &&
!in_array($userId, $previousUserIdList) &&
$userId !== $this->user->getId()
) {
$userIdList[] = $userId;
}
}
$data = [
'emailId' => $entity->getId(),
'emailName' => $entity->getSubject(),
];
/** @var EmailRepository $emailRepository */
$emailRepository = $this->entityManager->getRepository(EmailEntity::ENTITY_TYPE);
/** @var EmailAddressRepository $emailAddressRepository */
$emailAddressRepository = $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
if (!$entity->has('from')) {
$emailRepository->loadFromField($entity);
}
if (!$entity->has('to')) {
$emailRepository->loadToField($entity);
}
$person = null;
$from = $entity->get('from');
if ($from) {
$person = $emailAddressRepository->getEntityByAddress($from, null, [
User::ENTITY_TYPE,
Contact::ENTITY_TYPE,
Lead::ENTITY_TYPE,
]);
if ($person) {
$data['personEntityType'] = $person->getEntityType();
$data['personEntityName'] = $person->get(Field::NAME);
$data['personEntityId'] = $person->getId();
}
}
$userIdFrom = null;
if ($person && $person->getEntityType() === User::ENTITY_TYPE) {
$userIdFrom = $person->getId();
}
if (empty($data['personEntityId'])) {
$data['fromString'] = Util::parseFromName($entity->getFromString() ?? '');
if (empty($data['fromString']) && $from) {
$data['fromString'] = $from;
}
}
$parent = $entity->getParent();
$account = $entity->getAccount();
foreach ($userIdList as $userId) {
if ($userIdFrom === $userId) {
continue;
}
if (
$entity->getUserColumnInTrash($userId) ||
$entity->getUserColumnIsRead($userId) ||
$entity->getUserSkipNotification($userId)
) {
continue;
}
if (!$this->userChecker->checkAssignment(EmailEntity::ENTITY_TYPE, $userId)) {
continue;
}
if (
$params->getOption(EmailEntity::SAVE_OPTION_IS_BEING_IMPORTED) ||
$params->getOption(EmailEntity::SAVE_OPTION_IS_JUST_SENT)
) {
$folderId = $entity->getUserColumnFolderId($userId);
if (
$folderId &&
$this->entityManager
->getRDBRepositoryByClass(EmailFolder::class)
->where([
'id' => $folderId,
'skipNotifications' => true,
])
->count()
) {
continue;
}
}
$user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);
if (!$user) {
continue;
}
if ($user->isPortal()) {
continue;
}
if (!$this->aclManager->checkScope($user, EmailEntity::ENTITY_TYPE)) {
continue;
}
$isArchivedOrBeingImported =
$entity->getStatus() === EmailEntity::STATUS_ARCHIVED ||
$params->getOption(EmailEntity::SAVE_OPTION_IS_BEING_IMPORTED);
if (
$isArchivedOrBeingImported &&
$parent &&
$this->streamService->checkIsFollowed($parent, $userId)
) {
continue;
}
if (
$isArchivedOrBeingImported &&
$account &&
$this->streamService->checkIsFollowed($account, $userId)
) {
continue;
}
$existing = $this->entityManager
->getRDBRepository(Notification::ENTITY_TYPE)
->where([
'type' => Notification::TYPE_EMAIL_RECEIVED,
'userId' => $userId,
'relatedId' => $entity->getId(),
'relatedType' => EmailEntity::ENTITY_TYPE,
])
->select([Attribute::ID])
->findOne();
if ($existing) {
continue;
}
$notification = $this->entityManager->getRDBRepositoryByClass(Notification::class)->getNew();
$notification
->setType(Notification::TYPE_EMAIL_RECEIVED)
->setUserId($userId)
->setData($data)
->setRelated(LinkParent::createFromEntity($entity))
->setActionId($params->getActionId());
$this->entityManager->saveEntity($notification);
}
}
}

View File

@@ -0,0 +1,69 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Field\DateTime;
use Espo\Core\Utils\Config;
use Espo\Entities\AppLogRecord;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\DeleteBuilder;
class AppLog implements Cleanup
{
private const PERIOD = '30 days';
public function __construct(
private EntityManager $entityManager,
private Config $config
) {}
public function process(): void
{
if (!$this->config->get('cleanupAppLog')) {
return;
}
$query = DeleteBuilder::create()
->from(AppLogRecord::ENTITY_TYPE)
->where(['createdAt<' => $this->getBefore()->toString()])
->build();
$this->entityManager->getQueryExecutor()->execute($query);
}
private function getBefore(): DateTime
{
/** @var string $period */
$period = $this->config->get('cleanupAppLogPeriod') ?? self::PERIOD;
return DateTime::createNow()->modify('-' . $period);
}
}

View File

@@ -0,0 +1,105 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Field\DateTime;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\Entities\Note;
use Espo\ORM\EntityManager;
/**
* @noinspection PhpUnused
*/
class Audit implements Cleanup
{
private const PERIOD = '3 months';
public function __construct(
private Metadata $metadata,
private EntityManager $entityManager,
private Config $config
) {}
public function process(): void
{
if (!$this->config->get('cleanupAudit')) {
return;
}
$entityTypeList = $this->getEntityTypeList();
foreach ($entityTypeList as $scope) {
$this->processEntityType($scope);
}
}
private function processEntityType(string $entityType): void
{
$query = $this->entityManager
->getQueryBuilder()
->delete()
->from(Note::ENTITY_TYPE)
->where([
'parentType' => $entityType,
'createdAt<' => $this->getBefore()->toString(),
'type' => [Note::TYPE_UPDATE],
])
->build();
$this->entityManager->getQueryExecutor()->execute($query);
}
/**
* @return string[]
*/
private function getEntityTypeList(): array
{
/** @var string[] $scopeList */
$scopeList = array_keys($this->metadata->get(['scopes']) ?? []);
$scopeList = array_filter($scopeList, function ($item) {
return $this->metadata->get("scopes.$item.entity") &&
!$this->metadata->get("scopes.$item.preserveAuditLog") &&
!$this->metadata->get("scopes.$item.stream");
});
return array_values($scopeList);
}
private function getBefore(): DateTime
{
/** @var string $period */
$period = $this->config->get('cleanupAuditPeriod') ?? self::PERIOD;
return DateTime::createNow()->modify('-' . $period);
}
}

View File

@@ -0,0 +1,73 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\ORM\EntityManager;
use Espo\Core\Field\DateTime;
use Espo\Entities\Export;
class Exports implements Cleanup
{
private $config;
private $entityManager;
private string $cleanupPeriod = '2 days';
public function __construct(Config $config, EntityManager $entityManager)
{
$this->config = $config;
$this->entityManager = $entityManager;
}
public function process(): void
{
$period = '-' . $this->config->get('cleanupExportsPeriod', $this->cleanupPeriod);
$before = DateTime::createNow()
->modify($period)
->toString();
$delete = $this->entityManager
->getQueryBuilder()
->delete()
->from(Export::ENTITY_TYPE)
->where([
'createdAt<' => $before,
])
->build();
$this->entityManager->getQueryExecutor()->execute($delete);
}
}

View File

@@ -0,0 +1,71 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\ORM\EntityManager;
use Espo\Core\Field\DateTime;
class MassActions implements Cleanup
{
private $config;
private $entityManager;
private string $cleanupPeriod = '14 days';
public function __construct(Config $config, EntityManager $entityManager)
{
$this->config = $config;
$this->entityManager = $entityManager;
}
public function process(): void
{
$period = '-' . $this->config->get('cleanupMassActionsPeriod', $this->cleanupPeriod);
$before = DateTime::createNow()
->modify($period)
->toString();
$delete = $this->entityManager
->getQueryBuilder()
->delete()
->from('MassAction')
->where([
'createdAt<' => $before,
])
->build();
$this->entityManager->getQueryExecutor()->execute($delete);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\Core\Field\DateTime;
use Espo\ORM\EntityManager;
use Espo\Entities\PasswordChangeRequest;
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 process(): void
{
$period = '-' . $this->config->get('cleanupPasswordChangeRequestsPeriod', $this->cleanupPeriod);
$before = DateTime::createNow()
->modify($period)
->toString();
$delete = $this->entityManager
->getQueryBuilder()
->delete()
->from(PasswordChangeRequest::ENTITY_TYPE)
->where([
'createdAt<' => $before,
])
->build();
$this->entityManager->getQueryExecutor()->execute($delete);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Modules\Crm\Entities\Reminder;
use Espo\ORM\EntityManager;
use DateTime;
class Reminders implements Cleanup
{
private string $cleanupRemindersPeriod = '15 days';
private Config $config;
private EntityManager $entityManager;
public function __construct(Config $config, EntityManager $entityManager)
{
$this->config = $config;
$this->entityManager = $entityManager;
}
public function process(): void
{
$period = '-' . $this->config->get('cleanupRemindersPeriod', $this->cleanupRemindersPeriod);
$dt = new DateTime();
$dt->modify($period);
$delete = $this->entityManager
->getQueryBuilder()
->delete()
->from(Reminder::ENTITY_TYPE)
->where([
'remindAt<' => $dt->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT),
])
->build();
$this->entityManager->getQueryExecutor()->execute($delete);
}
}

View File

@@ -0,0 +1,137 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Acl\UserAclManagerProvider;
use Espo\Entities\StarSubscription;
use Espo\Entities\User;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\DeleteBuilder;
use Espo\Tools\Stars\StarService;
/**
* @noinspection PhpUnused
*/
class Stars implements Cleanup
{
public function __construct(
private EntityManager $entityManager,
private UserAclManagerProvider $userAclManagerProvider,
private StarService $service
) {}
public function process(): void
{
foreach ($this->getEntityTypeList() as $entityType) {
$this->processEntityType($entityType);
}
}
/**
* @return string[]
*/
private function getEntityTypeList(): array
{
$groups = $this->entityManager->getRDBRepositoryByClass(StarSubscription::class)
->group('entityType')
->select('entityType')
->find();
$list = [];
foreach ($groups as $group) {
$list[] = $group->get('entityType');
}
return $list;
}
private function processEntityType(string $entityType): void
{
if (
!$this->service->isEnabled($entityType) ||
!$this->entityManager->hasRepository($entityType)
) {
$deleteQuery = DeleteBuilder::create()
->from(StarSubscription::ENTITY_TYPE)
->where(['entityType' => $entityType])
->build();
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
return;
}
$stars = $this->entityManager
->getRDBRepositoryByClass(StarSubscription::class)
->where(['entityType' => $entityType])
->sth()
->find();
foreach ($stars as $star) {
$entityId = $star->get('entityId');
$userId = $star->get('userId');
if ($userId === null || $entityId === null) {
continue;
}
$entity = $this->entityManager->getEntityById($entityType, $entityId);
$user = $this->entityManager->getRDBRepositoryByClass(User::class)->getById($userId);
if (!$entity || !$user) {
$this->unstar($userId, $entityType, $entityId);
continue;
}
$aclManager = $this->userAclManagerProvider->get($user);
if (!$aclManager->checkEntityRead($user, $entity)) {
$this->unstar($userId, $entityType, $entityId);
}
}
}
private function unstar(string $userId, string $entityType, string $entityId): void
{
$deleteQuery = DeleteBuilder::create()
->from(StarSubscription::ENTITY_TYPE)
->where([
'userId' => $userId,
'entityType' => $entityType,
'entityId' => $entityId,
])
->build();
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
}
}

View File

@@ -0,0 +1,130 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Field\DateTime;
use Espo\Core\Name\Field;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\Entities\StreamSubscription;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\Part\Condition as Cond;
class Subscribers implements Cleanup
{
private const PERIOD = '2 months';
public function __construct(
private Metadata $metadata,
private EntityManager $entityManager,
private Config $config
) {}
public function process(): void
{
if (!$this->config->get('cleanupSubscribers')) {
return;
}
/** @var string[] $scopeList */
$scopeList = array_keys($this->metadata->get(['scopes']) ?? []);
/** @var string[] $scopeList */
$scopeList = array_values(array_filter(
$scopeList,
fn ($item) => (bool) $this->metadata->get(['scopes', $item, 'stream'])
));
foreach ($scopeList as $scope) {
$this->processEntityType($scope);
}
}
private function processEntityType(string $entityType): void
{
/** @var ?array<string, mixed> $data */
$data = $this->metadata->get(['streamDefs', $entityType, 'subscribersCleanup']);
if (!($data['enabled'] ?? false)) {
return;
}
/** @var string $dateField */
$dateField = $data['dateField'] ?? Field::CREATED_AT;
/** @var ?string[] $statusList */
$statusList = $data['statusList'] ?? null;
/** @var ?string $statusField */
$statusField = $this->metadata->get(['scopes', $entityType, 'statusField']);
if ($statusList === null || $statusField === null) {
return;
}
/** @var string $period */
$period = $this->metadata->get(['streamDefs', $entityType, 'subscribersCleanup', 'period']) ??
$this->config->get('cleanupSubscribersPeriod') ??
self::PERIOD;
$before = DateTime::createNow()->modify('-' . $period);
$query = $this->entityManager
->getQueryBuilder()
->delete()
->from(StreamSubscription::ENTITY_TYPE, 'subscription')
->join(
$entityType,
'entity',
Cond::equal(
Cond::column('entity.id'),
Cond::column('entityId')
)
)
->where(
Cond::and(
Cond::equal(
Cond::column('entityType'),
$entityType
),
Cond::less(
Cond::column('entity.' . $dateField),
$before->toString()
),
Cond::in(
Cond::column('entity.' . $statusField),
$statusList
)
)
)
->build();
$this->entityManager->getQueryExecutor()->execute($query);
}
}

View File

@@ -0,0 +1,77 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\ORM\EntityManager;
use Espo\Entities\TwoFactorCode;
use DateTime;
class TwoFactorCodes implements Cleanup
{
private const PERIOD = '5 days';
private $config;
private $entityManager;
public function __construct(Config $config, EntityManager $entityManager)
{
$this->config = $config;
$this->entityManager = $entityManager;
}
public function process(): void
{
$period = '-' . $this->config->get('cleanupTwoFactorCodesPeriod', self::PERIOD);
$from = (new DateTime())
->modify($period)
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$query = $this->entityManager
->getQueryBuilder()
->delete()
->from(TwoFactorCode::ENTITY_TYPE)
->where([
'createdAt<' => $from,
])
->build();
$this->entityManager
->getQueryExecutor()
->execute($query);
}
}

View File

@@ -0,0 +1,89 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\Cleanup;
use Espo\Core\Cleanup\Cleanup;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Entities\WebhookEventQueueItem;
use Espo\Entities\WebhookQueueItem;
use Espo\ORM\EntityManager;
use DateTime;
use Espo\ORM\Name\Attribute;
/**
* @noinspection PhpUnused
*/
class WebhookQueue implements Cleanup
{
private string $cleanupWebhookQueuePeriod = '10 days';
public function __construct(private Config $config, private EntityManager $entityManager)
{}
public function process(): void
{
$period = '-' . $this->config->get('cleanupWebhookQueuePeriod', $this->cleanupWebhookQueuePeriod);
$datetime = new DateTime();
$datetime->modify($period);
$from = $datetime->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$query1 = $this->entityManager
->getQueryBuilder()
->delete()
->from(WebhookQueueItem::ENTITY_TYPE)
->where([
'DATE:(createdAt)<' => $from,
'OR' => [
'status!=' => WebhookQueueItem::STATUS_PENDING,
Attribute::DELETED => true,
],
])
->build();
$this->entityManager->getQueryExecutor()->execute($query1);
$query2 = $this->entityManager
->getQueryBuilder()
->delete()
->from(WebhookEventQueueItem::ENTITY_TYPE)
->where([
'DATE:(createdAt)<' => $from,
'OR' => [
'isProcessed' => true,
Attribute::DELETED => true,
],
])
->build();
$this->entityManager->getQueryExecutor()->execute($query2);
}
}

View File

@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\IO;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\System;
use Espo\Core\Utils\Util;
/**
* @noinspection PhpUnused
*/
class CheckFilePermissions implements Command
{
public function __construct(
private FileManager $fileManager,
private System $system
) {}
public function run(Params $params, IO $io): void
{
$io->writeLine("\nNote: Run this command under the web server user.\n");
$io->writeLine('Writable:');
$io->writeLine('');
foreach ($this->fileManager->getPermissionUtils()->getWritableList() as $path) {
$fullPath = Util::concatPath($this->system->getRootDir(), $path);
$isWritable = $this->fileManager->isWritable($fullPath);
$msg = " " . ($isWritable ? "OK" : "FAIL") . " : $path";
$io->writeLine($msg);
}
}
}

View File

@@ -0,0 +1,103 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\IO;
use Espo\Core\Name\Field;
use Espo\Core\Utils\Config;
use Espo\Entities\User;
use Espo\ORM\EntityManager;
use RuntimeException;
class CreateAdminUser implements Command
{
public function __construct(
private EntityManager $entityManager,
private Config $config
) {}
public function run(Params $params, IO $io): void
{
$userName = $params->getArgument(0);
if (!$userName) {
$io->writeLine("A username must be specified as the first argument.");
$io->setExitStatus(1);
return;
}
/** @var ?string $regExp */
$regExp = $this->config->get('userNameRegularExpression');
if (!$regExp) {
throw new RuntimeException("No `userNameRegularExpression` in config.");
}
if (
str_contains($userName, ' ') ||
preg_replace("/{$regExp}/", '_', $userName) !== $userName
) {
$io->writeLine("Not allowed username.");
$io->setExitStatus(1);
return;
}
$repository = $this->entityManager->getRDBRepositoryByClass(User::class);
$existingUser = $repository
->where(['userName' => $userName])
->findOne();
if ($existingUser) {
$io->writeLine("A user with the same username already exists.");
$io->setExitStatus(1);
return;
}
$user = $repository->getNew();
$user->set('userName', $userName);
$user->set('type', User::TYPE_ADMIN);
$user->set(Field::NAME, $userName);
$repository->save($user);
$message = "The user '{$userName}' has been created. " .
"Set password with the command: `bin/command set-password {$userName}`.";
$io->writeLine($message);
}
}

View File

@@ -0,0 +1,137 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Tools\Import\Service;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\IO;
use Throwable;
class Import implements Command
{
public function __construct(private Service $service, private FileManager $fileManager)
{}
public function run(Params $params, IO $io) : void
{
$id = $params->getOption('id');
$filePath = $params->getOption('file');
$paramsId = $params->getOption('paramsId');
$forceResume = $params->hasFlag('resume');
$revert = $params->hasFlag('revert');
if (!$id && $filePath) {
if (!$paramsId) {
$io->writeLine("You need to specify --params-id option.");
return;
}
if (!$this->fileManager->isFile($filePath)) {
$io->writeLine("File not found.");
return;
}
$contents = $this->fileManager->getContents($filePath);
try {
$result = $this->service->importContentsWithParamsId($contents, $paramsId);
$resultId = $result->getId();
$countCreated = $result->getCountCreated();
$countUpdated = $result->getCountUpdated();
$countError = $result->getCountError();
$countDuplicate = $result->getCountDuplicate();
} catch (Throwable $e) {
$io->writeLine("Error occurred: " . $e->getMessage());
return;
}
$io->writeLine("Finished.");
$io->writeLine(" Import ID: {$resultId}");
$io->writeLine(" Created: {$countCreated}");
$io->writeLine(" Updated: {$countUpdated}");
$io->writeLine(" Duplicates: {$countDuplicate}");
$io->writeLine(" Errors: {$countError}");
return;
}
if ($id && $revert) {
$io->writeLine("Reverting import...");
try {
$this->service->revert($id);
} catch (Throwable $e) {
$io->writeLine("Error occurred: " . $e->getMessage());
return;
}
$io->writeLine("Finished.");
return;
}
if ($id) {
$io->writeLine("Running import, this may take a while...");
try {
$result = $this->service->importById($id, true, $forceResume);
} catch (Throwable $e) {
$io->writeLine("Error occurred: " . $e->getMessage());
return;
}
$countCreated = $result->getCountCreated();
$countUpdated = $result->getCountUpdated();
$countError = $result->getCountError();
$countDuplicate = $result->getCountDuplicate();
$io->writeLine("Finished.");
$io->writeLine(" Created: {$countCreated}");
$io->writeLine(" Updated: {$countUpdated}");
$io->writeLine(" Duplicates: {$countDuplicate}");
$io->writeLine(" Errors: {$countError}");
return;
}
$io->writeLine("Not enough params passed.");
}
}

View File

@@ -0,0 +1,108 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\IO;
use Espo\Core\Exceptions\Error;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Entities\ArrayValue;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\ArrayValue as ArrayValueRepository;
class PopulateArrayValues implements Command
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @throws Error
*/
public function run(Params $params, IO $io): void
{
$entityType = $params->getArgument(0);
$field = $params->getArgument(1);
if (!$entityType || !$field) {
throw new Error("Entity type and field should be passed as arguments.");
}
if (!$this->entityManager->hasRepository($entityType)) {
throw new Error("Bad entity type.");
}
$defs = $this->entityManager->getDefs()->getEntity($entityType);
if (!$defs->hasAttribute($field)) {
throw new Error("Bad field.");
}
if ($defs->getAttribute($field)->getType() !== Entity::JSON_ARRAY) {
throw new Error("Non-array field.");
}
if ($defs->getAttribute($field)->isNotStorable()) {
throw new Error("Not-storable field.");
}
if (!$defs->getAttribute($field)->getParam('storeArrayValues')) {
throw new Error("Array values disabled for the field..");
}
$collection = $this->entityManager
->getRDBRepository($entityType)
->sth()
->find();
/** @var ArrayValueRepository $repository */
$repository = $this->entityManager->getRepository(ArrayValue::ENTITY_TYPE);
foreach ($collection as $i => $entity) {
if (!$entity instanceof CoreEntity) {
throw new Error();
}
$repository->storeEntityAttribute($entity, $field);
if ($i % 1000 === 0) {
$io->write('.');
}
}
$io->writeLine('');
$io->writeLine('Done.');
}
}

View File

@@ -0,0 +1,116 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\Exceptions\ArgumentNotSpecified;
use Espo\Core\Console\Exceptions\InvalidArgument;
use Espo\Core\Console\IO;
use Espo\Core\Exceptions\Error;
use Espo\Core\FieldProcessing\NextNumber\BeforeSaveProcessor;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\Part\Order;
class PopulateNumbers implements Command
{
private BeforeSaveProcessor $beforeSaveProcessor;
private EntityManager $entityManager;
public function __construct(
BeforeSaveProcessor $beforeSaveProcessor,
EntityManager $entityManager
) {
$this->beforeSaveProcessor = $beforeSaveProcessor;
$this->entityManager = $entityManager;
}
/**
* @throws Error
*/
public function run(Params $params, IO $io): void
{
$entityType = $params->getArgument(0);
$field = $params->getArgument(1);
$orderBy = $params->getOption('orderBy') ?? Field::CREATED_AT;
$order = strtoupper($params->getOption('order') ?? Order::ASC);
if (!$entityType) {
throw new ArgumentNotSpecified("No entity type argument.");
}
if (!$field) {
throw new ArgumentNotSpecified("No field argument.");
}
if ($order !== Order::ASC && $order !== Order::DESC) {
throw new InvalidArgument("Bad order option.");
}
$fieldType = $this->entityManager
->getDefs()
->getEntity($entityType)
->getField($field)
->getType();
if ($fieldType !== 'number') {
throw new InvalidArgument("Field `{$field}` is not of `number` type.");
}
$collection = $this->entityManager
->getRDBRepository($entityType)
->where([
$field => null,
])
->order($orderBy, $order)
->sth()
->find();
foreach ($collection as $i => $entity) {
if (!$entity instanceof CoreEntity) {
throw new Error();
}
$this->beforeSaveProcessor->processPopulate($entity, $field);
$this->entityManager->saveEntity($entity, [SaveOption::IMPORT => true]);
if ($i % 1000 === 0) {
$io->write('.');
}
}
$io->writeLine('');
$io->writeLine('Done.');
}
}

View File

@@ -0,0 +1,69 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\ConsoleCommands;
use Espo\Core\Console\Command;
use Espo\Core\Console\Command\Params;
use Espo\Core\Console\IO;
use Espo\Tools\CategoryTree\RebuildPaths;
use Exception;
class RebuildCategoryPaths implements Command
{
private RebuildPaths $rebuildPaths;
public function __construct(RebuildPaths $rebuildPaths)
{
$this->rebuildPaths = $rebuildPaths;
}
public function run(Params $params, IO $io): void
{
$entityType = $params->getArgument(0);
if (!$entityType) {
$io->setExitStatus(1);
$io->writeLine("Error: No entity type. Should be specified as the first argument.");
return;
}
try {
$this->rebuildPaths->run($entityType);
} catch (Exception $e) {
$io->setExitStatus(1);
$io->writeLine("Error: " . $e->getMessage());
return;
}
$io->writeLine("Done.");
}
}

View File

@@ -0,0 +1,85 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\DefaultLayouts;
use Espo\Core\Name\Field;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Metadata;
use Espo\Entities\Team;
use Espo\Entities\User;
use Espo\ORM\Defs\Params\FieldParam;
use Espo\ORM\Defs\Params\RelationParam;
use stdClass;
class DefaultSidePanelType
{
public function __construct(private Metadata $metadata)
{}
/**
* @return stdClass[]
*/
public function get(string $scope): array
{
$list = [];
if (
$this->metadata->get(['entityDefs', $scope, 'fields', Field::ASSIGNED_USER, FieldParam::TYPE]) ===
FieldType::LINK &&
$this->metadata->get(['entityDefs', $scope, 'links', Field::ASSIGNED_USER, RelationParam::ENTITY]) ===
User::ENTITY_TYPE
||
$this->metadata->get(['entityDefs', $scope, 'fields', Field::ASSIGNED_USERS, FieldParam::TYPE]) ===
FieldType::LINK_MULTIPLE &&
$this->metadata->get(['entityDefs', $scope, 'links', Field::ASSIGNED_USERS, RelationParam::ENTITY]) ===
User::ENTITY_TYPE
) {
$list[] = (object) ['name' => ':assignedUser'];
}
if (
$this->metadata->get(['entityDefs', $scope, 'fields', Field::TEAMS, FieldParam::TYPE]) ===
FieldType::LINK_MULTIPLE &&
$this->metadata->get(['entityDefs', $scope, 'links', Field::TEAMS, RelationParam::ENTITY]) ===
Team::ENTITY_TYPE
) {
$list[] = (object) ['name' => Field::TEAMS];
}
if (
$this->metadata->get("entityDefs.$scope.fields.collaborators.type") === FieldType::LINK_MULTIPLE &&
$this->metadata->get("entityDefs.$scope.links.collaborators.entity") === User::ENTITY_TYPE
) {
$list[] = (object) ['name' => Field::COLLABORATORS];
}
return $list;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\DuplicateWhereBuilders;
class Company extends General
{}

View File

@@ -0,0 +1,275 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\DuplicateWhereBuilders;
use Espo\Core\Duplicate\WhereBuilder;
use Espo\Core\Field\EmailAddressGroup;
use Espo\Core\Field\PhoneNumberGroup;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\ORM\Type\FieldType;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs;
use Espo\ORM\Entity;
use Espo\ORM\Query\Part\Condition as Cond;
use Espo\ORM\Query\Part\Where\OrGroup;
use Espo\ORM\Query\Part\Where\OrGroupBuilder;
use Espo\ORM\Query\Part\WhereItem;
use Espo\ORM\Type\AttributeType;
/**
* @implements WhereBuilder<CoreEntity>
*/
class General implements WhereBuilder
{
public function __construct(
private Metadata $metadata,
private Defs $ormDefs,
private Config $config
) {}
/**
* @param CoreEntity $entity
*/
public function build(Entity $entity): ?WhereItem
{
/** @var string[] $fieldList */
$fieldList = $this->metadata->get(['scopes', $entity->getEntityType(), 'duplicateCheckFieldList']) ?? [];
$orBuilder = OrGroup::createBuilder();
$toCheck = false;
foreach ($fieldList as $field) {
$toCheckItem = $this->applyField($field, $entity, $orBuilder);
if ($toCheckItem) {
$toCheck = true;
}
}
if (!$toCheck) {
return null;
}
return $orBuilder->build();
}
private function applyField(
string $field,
CoreEntity $entity,
OrGroupBuilder $orBuilder
): bool {
$type = $this->ormDefs
->getEntity($entity->getEntityType())
->tryGetField($field)
?->getType();
if ($type === FieldType::PERSON_NAME) {
return $this->applyFieldPersonName($field, $entity, $orBuilder);
}
if ($type === FieldType::EMAIL) {
return $this->applyFieldEmail($field, $entity, $orBuilder);
}
if ($type === FieldType::PHONE) {
return $this->applyFieldPhone($field, $entity, $orBuilder);
}
if ($entity->getAttributeType($field) === AttributeType::VARCHAR) {
return $this->applyFieldVarchar($field, $entity, $orBuilder);
}
return false;
}
private function applyFieldPersonName(
string $field,
CoreEntity $entity,
OrGroupBuilder $orBuilder
): bool {
$first = 'first' . ucfirst($field);
$last = 'last' . ucfirst($field);
if (!$entity->get($first) && !$entity->get($last)) {
return false;
}
$orBuilder->add(
Cond::and(
Cond::equal(
Cond::column($first),
$entity->get($first)
),
Cond::equal(
Cond::column($last),
$entity->get($last)
)
)
);
return true;
}
private function applyFieldEmail(
string $field,
CoreEntity $entity,
OrGroupBuilder $orBuilder
): bool {
$toCheck = false;
if (
($entity->get($field) || $entity->get($field . 'Data')) &&
(
$entity->isNew() ||
$entity->isAttributeChanged($field) ||
$entity->isAttributeChanged($field . 'Data')
)
) {
foreach ($this->getEmailAddressList($entity) as $emailAddress) {
$orBuilder->add(
Cond::equal(
Cond::column($field),
$emailAddress
)
);
$toCheck = true;
}
}
return $toCheck;
}
private function applyFieldPhone(
string $field,
CoreEntity $entity,
OrGroupBuilder $orBuilder
): bool {
$toCheck = false;
$isNumeric = $this->config->get('phoneNumberNumericSearch');
$column = $isNumeric ?
$field . 'Numeric' :
$field;
if (
($entity->get($field) || $entity->get($field . 'Data')) &&
(
$entity->isNew() ||
$entity->isAttributeChanged($field) ||
$entity->isAttributeChanged($field . 'Data')
)
) {
foreach ($this->getPhoneNumberList($entity) as $number) {
if ($isNumeric) {
$number = preg_replace('/[^0-9]/', '', $number);
}
$orBuilder->add(
Cond::equal(
Cond::column($column),
$number
)
);
$toCheck = true;
}
}
return $toCheck;
}
private function applyFieldVarchar(
string $field,
CoreEntity $entity,
OrGroupBuilder $orBuilder
): bool {
if (!$entity->get($field)) {
return false;
}
$orBuilder->add(
Cond::equal(
Cond::column($field),
$entity->get($field)
),
);
return true;
}
/**
* @return string[]
*/
private function getEmailAddressList(CoreEntity $entity): array
{
if ($entity->get('emailAddressData')) {
/** @var EmailAddressGroup $eaGroup */
$eaGroup = $entity->getValueObject('emailAddress');
return $eaGroup->getAddressList();
}
if ($entity->get('emailAddress')) {
return [
$entity->get('emailAddress')
];
}
return [];
}
/**
* @return string[]
*/
private function getPhoneNumberList(CoreEntity $entity): array
{
if ($entity->get('phoneNumberData')) {
/** @var PhoneNumberGroup $eaGroup */
$eaGroup = $entity->getValueObject('phoneNumber');
return $eaGroup->getNumberList();
}
if ($entity->get('phoneNumber')) {
return [$entity->get('phoneNumber')];
}
return [];
}
}

View File

@@ -0,0 +1,55 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\DuplicateWhereBuilders;
use Espo\Core\Duplicate\WhereBuilder;
use Espo\Core\Name\Field;
use Espo\ORM\Entity;
use Espo\ORM\Query\Part\Condition as Cond;
use Espo\ORM\Query\Part\WhereItem;
/**
* @implements WhereBuilder<Entity>
*/
class Name implements WhereBuilder
{
public function build(Entity $entity): ?WhereItem
{
if ($entity->get(Field::NAME)) {
return Cond::equal(
Cond::column(Field::NAME),
$entity->get(Field::NAME)
);
}
return null;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\DuplicateWhereBuilders;
class Person extends General
{}

View File

@@ -0,0 +1,201 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldConverters;
use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs;
use Espo\Core\Utils\Database\Orm\Defs\EntityDefs;
use Espo\Core\Utils\Database\Orm\FieldConverter;
use Espo\ORM\Defs\FieldDefs;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Type\AttributeType;
use RuntimeException;
class RelationshipRole implements FieldConverter
{
public function convert(FieldDefs $fieldDefs, string $entityType): EntityDefs
{
$name = $fieldDefs->getName();
$attributeDefs = AttributeDefs::create($name)
->withType(AttributeType::VARCHAR)
->withNotStorable();
$attributeDefs = $this->addWhere($attributeDefs, $fieldDefs, $entityType);
return EntityDefs::create()
->withAttribute($attributeDefs);
}
private function addWhere(AttributeDefs $attributeDefs, FieldDefs $fieldDefs, string $entityType): AttributeDefs
{
$data = $fieldDefs->getParam('converterData');
if (!is_array($data)) {
throw new RuntimeException("No `converterData` in field defs.");
}
/** @var ?string $column */
$column = $data['column'] ?? null;
/** @var ?string $link */
$link = $data['link'] ?? null;
/** @var ?string $relationName */
$relationName = $data[RelationParam::RELATION_NAME] ?? null;
/** @var ?string $nearKey */
$nearKey = $data['nearKey'] ?? null;
if (!$column || !$link || !$relationName || !$nearKey) {
throw new RuntimeException("Bad `converterData`.");
}
$midTable = ucfirst($relationName);
return $attributeDefs->withParamsMerged([
'where' => [
'=' => [
'whereClause' => [
'id=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
$column => '{value}',
],
],
],
],
'<>' => [
'whereClause' => [
'id!=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
$column => '{value}',
],
],
],
],
'IN' => [
'whereClause' => [
'id=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
$column => '{value}',
],
],
],
],
'NOT IN' => [
'whereClause' => [
'id!=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
$column => '{value}',
],
],
],
],
'LIKE' => [
'whereClause' => [
'id=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
"$column*" => '{value}',
],
],
],
],
'NOT LIKE' => [
'whereClause' => [
'id!=s' => [
'from' => $midTable,
'select' => [$nearKey],
'whereClause' => [
Attribute::DELETED => false,
"$column*" => '{value}',
],
],
],
],
'IS NULL' => [
'whereClause' => [
'NOT' => [
'EXISTS' => [
'from' => $entityType,
'fromAlias' => 'sq',
'select' => [Attribute::ID],
'leftJoins' => [
[
$link,
'm',
null,
['onlyMiddle' => true]
]
],
'whereClause' => [
"m.$column!=" => null,
'sq.id:' => lcfirst($entityType) . '.id',
],
],
],
],
],
'IS NOT NULL' => [
'whereClause' => [
'EXISTS' => [
'from' => $entityType,
'fromAlias' => 'sq',
'select' => [Attribute::ID],
'leftJoins' => [
[
$link,
'm',
null,
['onlyMiddle' => true]
]
],
'whereClause' => [
"m.$column!=" => null,
'sq.id:' => lcfirst($entityType) . '.id',
],
],
],
],
],
]);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldDuplicators;
use Espo\Core\Record\Duplicator\FieldDuplicator;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\Attachment as AttachmentRepository;
use Espo\Entities\Attachment;
use stdClass;
class AttachmentMultiple implements FieldDuplicator
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function duplicate(Entity $entity, string $field): stdClass
{
$valueMap = (object) [];
/** @var \Espo\ORM\Collection<Attachment> $attachmentList */
$attachmentList = $this->entityManager
->getRDBRepository($entity->getEntityType())
->getRelation($entity, $field)
->find();
if (is_countable($attachmentList) && !count($attachmentList)) {
return $valueMap;
}
$idList = [];
$nameHash = (object) [];
$typeHash = (object) [];
/** @var AttachmentRepository $attachmentRepository */
$attachmentRepository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
foreach ($attachmentList as $attachment) {
$copiedAttachment = $attachmentRepository->getCopiedAttachment($attachment);
$copiedAttachment->set('field', $field);
$this->entityManager->saveEntity($copiedAttachment);
$idList[] = $copiedAttachment->getId();
$nameHash->{$copiedAttachment->getId()} = $copiedAttachment->getName();
$typeHash->{$copiedAttachment->getId()} = $copiedAttachment->getType();
}
$valueMap->{$field . 'Ids'} = $idList;
$valueMap->{$field . 'Names'} = $nameHash;
$valueMap->{$field . 'Types'} = $typeHash;
return $valueMap;
}
}

View File

@@ -0,0 +1,75 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldDuplicators;
use Espo\Core\Record\Duplicator\FieldDuplicator;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\Attachment as AttachmentRepository;
use Espo\Entities\Attachment;
use stdClass;
class File implements FieldDuplicator
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function duplicate(Entity $entity, string $field): stdClass
{
$valueMap = (object) [];
/** @var Attachment|null $attachment */
$attachment = $this->entityManager
->getRDBRepository($entity->getEntityType())
->getRelation($entity, $field)
->findOne();
if (!$attachment) {
return $valueMap;
}
/** @var AttachmentRepository $attachmentRepository */
$attachmentRepository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
$copiedAttachment = $attachmentRepository->getCopiedAttachment($attachment);
$idAttribute = $field . 'Id';
$valueMap->$idAttribute = $copiedAttachment->getId();
return $valueMap;
}
}

View File

@@ -0,0 +1,82 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldDuplicators;
use Espo\Core\Record\Duplicator\FieldDuplicator;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use stdClass;
class LinkMultiple implements FieldDuplicator
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function duplicate(Entity $entity, string $field): stdClass
{
$valueMap = (object) [];
$entityDefs = $this->entityManager
->getDefs()
->getEntity($entity->getEntityType());
if (!$entity->hasRelation($field)) {
return $valueMap;
}
$relationDefs = $entityDefs->getRelation($field);
if (
!$relationDefs->hasForeignEntityType() ||
!$relationDefs->hasForeignRelationName()
) {
return $valueMap;
}
$foreignRelationType = $this->entityManager
->getDefs()
->getEntity($relationDefs->getForeignEntityType())
->getRelation($relationDefs->getForeignRelationName())
->getType();
if ($foreignRelationType !== Entity::MANY_MANY) {
$valueMap->{$field . 'Ids'} = [];
$valueMap->{$field . 'Names'} = (object) [];
$valueMap->{$field . 'Columns'} = (object) [];
}
return $valueMap;
}
}

View File

@@ -0,0 +1,117 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldDuplicators;
use Espo\Core\Record\Duplicator\FieldDuplicator;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\Repositories\Attachment as AttachmentRepository;
use Espo\Entities\Attachment;
use stdClass;
class Wysiwyg implements FieldDuplicator
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function duplicate(Entity $entity, string $field): stdClass
{
$valueMap = (object) [];
$contents = $entity->get($field);
if (!$contents) {
return $valueMap;
}
$matches = [];
$matchResult = preg_match_all("/\?entryPoint=attachment&amp;id=([^&=\"']+)/", $contents, $matches);
if (
!$matchResult ||
empty($matches[1]) ||
!is_array($matches[1])
) {
return $valueMap;
}
$attachmentIdList = $matches[1];
/** @var Attachment[] $attachmentList */
$attachmentList = [];
foreach ($attachmentIdList as $id) {
/** @var Attachment|null $attachment */
$attachment = $this->entityManager->getEntityById(Attachment::ENTITY_TYPE, $id);
if (!$attachment) {
continue;
}
$attachmentList[] = $attachment;
}
if (!count($attachmentList)) {
return $valueMap;
}
/** @var AttachmentRepository $attachmentRepository */
$attachmentRepository = $this->entityManager->getRepository(Attachment::ENTITY_TYPE);
foreach ($attachmentList as $attachment) {
$copiedAttachment = $attachmentRepository->getCopiedAttachment($attachment);
$copiedAttachment->set([
'relatedId' => null,
'relatedType' => $entity->getEntityType(),
'field' => $field,
]);
$this->entityManager->saveEntity($copiedAttachment);
$contents = str_replace(
'?entryPoint=attachment&amp;id=' . $attachment->getId(),
'?entryPoint=attachment&amp;id=' . $copiedAttachment->getId(),
$contents
);
}
$valueMap->$field = $contents;
return $valueMap;
}
}

View File

@@ -0,0 +1,57 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\ORM\Entity;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Repositories\Email as EmailRepository;
use Espo\Entities\Email;
/**
* @implements Loader<Email>
*/
class AddressDataLoader implements Loader
{
public function __construct(private EntityManager $entityManager)
{}
/**
* @param Email $entity
*/
public function process(Entity $entity, Params $params): void
{
/** @var EmailRepository $repository */
$repository = $this->entityManager->getRepository(Email::ENTITY_TYPE);
$repository->loadNameHash($entity);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Entities\Email;
use Espo\ORM\Entity;
use Espo\Repositories\Email as EmailRepository;
/**
* @implements Loader<Email>
*/
class AddressLoader implements Loader
{
public function __construct(private EntityManager $entityManager)
{}
/**
* @inheritDoc
*/
public function process(Entity $entity, Params $params): void
{
/** @var EmailRepository $repository */
$repository = $this->entityManager->getRepository(Email::ENTITY_TYPE);
$repository->loadFromField($entity);
$repository->loadToField($entity);
$repository->loadCcField($entity);
$repository->loadBccField($entity);
$repository->loadReplyToField($entity);
}
}

View File

@@ -0,0 +1,67 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Entities\Email;
use Espo\Entities\EmailFolder;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
/**
* @implements Loader<Email>
*/
class FolderDataLoader implements Loader
{
public function __construct(private EntityManager $entityManager) {}
public function process(Entity $entity, Params $params): void
{
$folderId = $entity->get(Email::USERS_COLUMN_FOLDER_ID);
if (!$folderId) {
return;
}
$folder = $this->entityManager
->getRDBRepositoryByClass(EmailFolder::class)
->select([Attribute::ID, 'name'])
->where([Attribute::ID => $folderId])
->findOne();
if (!$folder) {
return;
}
$entity->set('folderName', $folder->getName());
}
}

View File

@@ -0,0 +1,239 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\Core\Name\Field;
use Espo\Entities\User;
use Espo\Modules\Crm\Entities\Call;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\Modules\Crm\Entities\Meeting;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Entities\EmailAddress;
use Espo\Entities\Email;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\Mail\Event\Event as EspoEvent;
use Espo\Core\Mail\Event\EventFactory;
use Espo\Core\Utils\Log;
use ICal\Event;
use ICal\ICal;
use Throwable;
use stdClass;
/**
* @implements Loader<Email>
*/
class IcsDataLoader implements Loader
{
/** @var array<string, string> */
private $entityTypeLinkMap = [
User::ENTITY_TYPE => Meeting::LINK_USERS,
Contact::ENTITY_TYPE => Meeting::LINK_CONTACTS,
Lead::ENTITY_TYPE => Meeting::LINK_LEADS,
];
public function __construct(private EntityManager $entityManager, private Log $log)
{}
public function process(Entity $entity, Params $params): void
{
$icsContents = $entity->get('icsContents');
if ($icsContents === null) {
return;
}
$ical = new ICal();
$ical->initString($icsContents);
/* @var ?Event $event */
$event = $ical->events()[0] ?? null;
if ($event === null) {
return;
}
if ($event->status === 'CANCELLED') {
return;
}
$espoEvent = EventFactory::createFromU01jmg3Ical($ical);
$valueMap = (object) [
'sourceEmailId' => $entity->getId(),
];
try {
$valueMap->name = $espoEvent->getName();
$valueMap->description = $espoEvent->getDescription();
$valueMap->dateStart = $espoEvent->getDateStart();
$valueMap->dateEnd = $espoEvent->getDateEnd();
$valueMap->location = $espoEvent->getLocation();
$valueMap->isAllDay = $espoEvent->isAllDay();
if ($espoEvent->isAllDay()) {
$valueMap->dateStartDate = $espoEvent->getDateStart();
$valueMap->dateEndDate = $espoEvent->getDateEnd();
}
} catch (Throwable $e) {
$this->log->warning("Error while converting ICS event '" . $entity->getId() . "': " . $e->getMessage());
return;
}
if ($this->eventAlreadyExists($espoEvent)) {
return;
}
/** @var EmailAddressRepository $emailAddressRepository */
$emailAddressRepository = $this->entityManager->getRepository(EmailAddress::ENTITY_TYPE);
$attendeeEmailAddressList = $espoEvent->getAttendeeEmailAddressList();
$organizerEmailAddress = $espoEvent->getOrganizerEmailAddress();
if ($organizerEmailAddress) {
$attendeeEmailAddressList[] = $organizerEmailAddress;
}
foreach ($attendeeEmailAddressList as $address) {
$personEntity = $emailAddressRepository->getEntityByAddress($address);
if (!$personEntity) {
continue;
}
$link = $this->entityTypeLinkMap[$personEntity->getEntityType()] ?? null;
if (!$link) {
continue;
}
$idsAttribute = $link . 'Ids';
$namesAttribute = $link . 'Names';
$idList = $valueMap->$idsAttribute ?? [];
$nameMap = $valueMap->$namesAttribute ?? (object) [];
$idList[] = $personEntity->getId();
$nameMap->{$personEntity->getId()} = $personEntity->get(Field::NAME);
$valueMap->$idsAttribute = $idList;
$valueMap->$namesAttribute = $nameMap;
}
$eventData = (object) [
'valueMap' => $valueMap,
'uid' => $espoEvent->getUid(),
'createdEvent' => null,
];
$this->loadCreatedEvent($entity, $espoEvent, $eventData);
$entity->set('icsEventData', $eventData);
$entity->set('icsEventDateStart', $espoEvent->getDateStart());
if ($espoEvent->isAllDay()) {
$entity->set('icsEventDateStartDate', $espoEvent->getDateStart());
}
}
private function loadCreatedEvent(Entity $entity, EspoEvent $espoEvent, stdClass $eventData): void
{
$emailSameEvent = $this->entityManager
->getRDBRepository(Email::ENTITY_TYPE)
->where([
'icsEventUid' => $espoEvent->getUid(),
'id!=' => $entity->getId()
])
->findOne();
if (!$emailSameEvent) {
return;
}
if (
!$emailSameEvent->get('createdEventId') ||
!$emailSameEvent->get('createdEventType')
) {
return;
}
$createdEvent = $this->entityManager
->getEntityById($emailSameEvent->get('createdEventType'), $emailSameEvent->get('createdEventId'));
if (!$createdEvent) {
return;
}
$eventData->createdEvent = (object) [
'id' => $createdEvent->getId(),
'entityType' => $emailSameEvent->getEntityType(),
'name' => $createdEvent->get(Field::NAME),
];
}
private function eventAlreadyExists(EspoEvent $espoEvent): bool
{
$id = $espoEvent->getUid();
if (!$id) {
return false;
}
$found1 = $this->entityManager
->getRDBRepository(Meeting::ENTITY_TYPE)
->select([Attribute::ID])
->where([Attribute::ID => $id])
->findOne();
if ($found1) {
return true;
}
$found2 = $this->entityManager
->getRDBRepository(Call::ENTITY_TYPE)
->select([Attribute::ID])
->where([Attribute::ID => $id])
->findOne();
if ($found2) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,128 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\Core\Name\Field;
use Espo\Core\Name\Link;
use Espo\ORM\Entity;
use Espo\ORM\Name\Attribute;
use Espo\Repositories\EmailAddress as EmailAddressRepository;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Entities\Email;
use Espo\Entities\User;
/**
* @implements Loader<Email>
*/
class StringDataLoader implements Loader
{
private const LINK_EMAIL_ADDRESSES = Link::EMAIL_ADDRESSES;
/** @var array<string, string> */
private $fromEmailAddressNameCache = [];
public function __construct(
private EntityManager $entityManager,
private User $user
) {}
public function process(Entity $entity, Params $params): void
{
/** @var Email $entity */
$userEmailAddressIdList = [];
$emailAddressCollection = $this->entityManager
->getRelation($this->user, self::LINK_EMAIL_ADDRESSES)
->select([Attribute::ID])
->find();
foreach ($emailAddressCollection as $emailAddress) {
$userEmailAddressIdList[] = $emailAddress->getId();
}
if (
in_array($entity->get('fromEmailAddressId'), $userEmailAddressIdList) ||
$entity->get('createdById') === $this->user->getId() &&
$entity->getStatus() === Email::STATUS_SENT
) {
$entity->loadLinkMultipleField('toEmailAddresses');
$idList = $entity->get('toEmailAddressesIds');
$names = $entity->get('toEmailAddressesNames');
if (empty($idList)) {
return;
}
$list = [];
foreach ($idList as $emailAddressId) {
$person = $this->getEmailAddressRepository()->getEntityByAddressId($emailAddressId, null, true);
$list[] = $person ? $person->get(Field::NAME) : $names->$emailAddressId;
}
$entity->set('personStringData', 'To: ' . implode(', ', $list));
return;
}
/** @var ?string $fromEmailAddressId */
$fromEmailAddressId = $entity->get('fromEmailAddressId');
if (!$fromEmailAddressId) {
return;
}
if (!array_key_exists($fromEmailAddressId, $this->fromEmailAddressNameCache)) {
$person = $this->getEmailAddressRepository()->getEntityByAddressId($fromEmailAddressId, null, true);
$fromName = $person?->get(Field::NAME);
$this->fromEmailAddressNameCache[$fromEmailAddressId] = $fromName;
}
$fromName =
$this->fromEmailAddressNameCache[$fromEmailAddressId] ??
$entity->get('fromName') ??
$entity->get('fromEmailAddressName');
$entity->set('personStringData', $fromName);
}
private function getEmailAddressRepository(): EmailAddressRepository
{
/** @var EmailAddressRepository */
return $this->entityManager->getRepository('EmailAddress');
}
}

View File

@@ -0,0 +1,90 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Email;
use Espo\Entities\Email;
use Espo\ORM\Entity;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Entities\User;
use Espo\ORM\Name\Attribute;
/**
* @implements Loader<Email>
*/
class UserColumnsLoader implements Loader
{
public function __construct(
private EntityManager $entityManager,
private User $user
) {}
public function process(Entity $entity, Params $params): void
{
$emailUser = $this->entityManager
->getRDBRepository(Email::RELATIONSHIP_EMAIL_USER)
->select([
Email::USERS_COLUMN_IS_READ,
Email::USERS_COLUMN_IS_IMPORTANT,
Email::USERS_COLUMN_IN_TRASH,
Email::USERS_COLUMN_IN_ARCHIVE,
])
->where([
Attribute::DELETED => false,
'userId' => $this->user->getId(),
'emailId' => $entity->getId(),
])
->findOne();
if (!$emailUser) {
$entity->set(Email::USERS_COLUMN_IS_READ, null);
$entity->clear(Email::USERS_COLUMN_IS_IMPORTANT);
$entity->clear(Email::USERS_COLUMN_IN_TRASH);
$entity->clear(Email::USERS_COLUMN_IN_ARCHIVE);
return;
}
$values = [
Email::USERS_COLUMN_IS_READ => $emailUser->get(Email::USERS_COLUMN_IS_READ),
Email::USERS_COLUMN_IS_IMPORTANT => $emailUser->get(Email::USERS_COLUMN_IS_IMPORTANT),
Email::USERS_COLUMN_IN_TRASH => $emailUser->get(Email::USERS_COLUMN_IN_TRASH),
Email::USERS_COLUMN_IN_ARCHIVE => $emailUser->get(Email::USERS_COLUMN_IN_ARCHIVE),
'isUsersSent' => $entity->getSentBy()?->getId() === $this->user->getId(),
];
$entity->setMultiple($values);
foreach ($values as $key => $value) {
$entity->setFetched($key, $value);
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Import;
use Espo\Entities\Import;
use Espo\ORM\Entity;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use Espo\Repositories\Import as ImportRepository;
/**
* @implements Loader<Import>
*/
class CountsLoader implements Loader
{
public function __construct(private EntityManager $entityManager)
{}
public function process(Entity $entity, Params $params): void
{
/** @var ImportRepository $repository */
$repository = $this->entityManager->getRepository('Import');
$importedCount = $repository->countResultRecords($entity, 'imported');
$duplicateCount = $repository->countResultRecords($entity, 'duplicates');
$updatedCount = $repository->countResultRecords($entity, 'updated');
$entity->set([
'importedCount' => $importedCount,
'duplicateCount' => $duplicateCount,
'updatedCount' => $updatedCount,
]);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\InboundEmail;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\Mail\ConfigDataProvider;
use Espo\Entities\InboundEmail;
use Espo\ORM\Entity;
/**
* @implements Loader<InboundEmail>
*/
class IsSystemLoader implements Loader
{
public function __construct(
private ConfigDataProvider $configDataProvider,
) {}
public function process(Entity $entity, Params $params): void
{
$isSystem = $entity->getEmailAddress() === $this->configDataProvider->getSystemOutboundAddress();
$entity->set('isSystem', $isSystem);
}
}

View File

@@ -0,0 +1,159 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\LeadCapture;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Config\ApplicationConfig;
use Espo\Core\Utils\FieldUtil;
use Espo\Core\Utils\Util;
use Espo\Entities\LeadCapture;
use Espo\Modules\Crm\Entities\Lead;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Type\AttributeType;
/**
* @implements Loader<LeadCapture>
*/
class ExampleLoader implements Loader
{
public function __construct(
private FieldUtil $fieldUtil,
private ApplicationConfig $applicationConfig,
private EntityManager $entityManager,
private Config $config,
) {}
public function process(Entity $entity, Params $params): void
{
$entity->set('exampleRequestMethod', 'POST');
$entity->set('exampleRequestHeaders', [
'Content-Type: application/json',
]);
$this->processRequestUrl($entity);
$this->processRequestPayload($entity);
$this->processFormUrl($entity);
}
private function processRequestUrl(LeadCapture $entity): void
{
$apiKey = $entity->getApiKey();
$siteUrl = $this->applicationConfig->getSiteUrl();
if (!$apiKey) {
return;
}
$requestUrl = "$siteUrl/api/v1/LeadCapture/$apiKey";
$entity->set('exampleRequestUrl', $requestUrl);
}
private function processRequestPayload(LeadCapture $entity): void
{
$requestPayload = "```\n{\n";
$attributeList = [];
$attributeIgnoreList = [
'emailAddressIsOptedOut',
'phoneNumberIsOptedOut',
'emailAddressIsInvalid',
'phoneNumberIsInvalid',
'emailAddressData',
'phoneNumberData',
];
foreach ($entity->getFieldList() as $field) {
foreach ($this->fieldUtil->getActualAttributeList(Lead::ENTITY_TYPE, $field) as $attribute) {
if (!in_array($attribute, $attributeIgnoreList)) {
$attributeList[] = $attribute;
}
}
}
$seed = $this->entityManager->getNewEntity(Lead::ENTITY_TYPE);
foreach ($attributeList as $i => $attribute) {
$value = strtoupper(Util::camelCaseToUnderscore($attribute));
if (
in_array(
$seed->getAttributeType($attribute), [
Entity::VARCHAR,
Entity::TEXT,
AttributeType::DATETIME,
AttributeType::DATE,
]
)
) {
$value = '"' . $value . '"';
}
$requestPayload .= " \"" . $attribute . "\": " . $value;
if ($i < count($attributeList) - 1) {
$requestPayload .= ",";
}
$requestPayload .= "\n";
}
$requestPayload .= "}\n```";
$entity->set('exampleRequestPayload', $requestPayload);
}
private function processFormUrl(LeadCapture $entity): void
{
$formId = $entity->getFormId();
$siteUrl = $this->getSiteUrl();
if (!$entity->hasFormEnabled() || !$formId) {
/** @noinspection PhpRedundantOptionalArgumentInspection */
$entity->set('formUrl', null);
return;
}
$formUrl = "$siteUrl?entryPoint=leadCaptureForm&id=$formId";
$entity->set('formUrl', $formUrl);
}
private function getSiteUrl(): string
{
return $this->config->get('leadCaptureSiteUrl') ?? $this->applicationConfig->getSiteUrl();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Note;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Entities\Note;
use Espo\ORM\Entity;
use Espo\Tools\Stream\MassNotePreparator;
/**
* @implements Loader<Note>
*/
class AdditionalFieldsLoader implements Loader
{
public function __construct(
private MassNotePreparator $massNotePreparator,
) {}
public function process(Entity $entity, Params $params): void
{
$entity->loadAdditionalFields();
$this->massNotePreparator->prepare([$entity]);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\OAuthAccount;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Entities\OAuthAccount;
use Espo\ORM\Entity;
use Espo\Tools\OAuth\ConfigDataProvider;
/**
* @implements Loader<OAuthAccount>
*/
class DataLoader implements Loader
{
public function __construct(
private ConfigDataProvider $configDataProvider,
) {}
public function process(Entity $entity, Params $params): void
{
if (!$entity->get('providerId')) {
return;
}
$provider = $entity->getProvider();
$scope = null;
if ($provider->getScopes()) {
$scope = implode($provider->getScopeSeparator() ?? ' ', $provider->getScopes());
}
$data = [
'endpoint' => $provider->getAuthorizationEndpoint(),
'clientId' => $provider->getClientId(),
'redirectUri' => $this->configDataProvider->getRedirectUri(),
'scope' => $scope,
'prompt' => $provider->getAuthorizationPrompt(),
'params' => $provider->getAuthorizationParams(),
];
$entity->set('data', $data);
}
}

View File

@@ -0,0 +1,51 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\OAuthProvider;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Entities\OAuthProvider;
use Espo\ORM\Entity;
use Espo\Tools\OAuth\ConfigDataProvider;
/**
* @implements Loader<OAuthProvider>
*/
class AuthorizationRedirectUriLoader implements Loader
{
public function __construct(
private ConfigDataProvider $configDataProvider,
) {}
public function process(Entity $entity, Params $params): void
{
$entity->set('authorizationRedirectUri', $this->configDataProvider->getRedirectUri());
}
}

View File

@@ -0,0 +1,64 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\Portal;
use Espo\ORM\Entity;
use Espo\Repositories\Portal as PortalRepository;
use Espo\Entities\Portal;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
/**
* @implements Loader<Portal>
*/
class UrlLoader implements Loader
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function process(Entity $entity, Params $params): void
{
/** @var Portal $entity */
$this->getPortalRepository()->loadUrlField($entity);
}
private function getPortalRepository(): PortalRepository
{
/** @var PortalRepository */
return $this->entityManager->getRepository('Portal');
}
}

View File

@@ -0,0 +1,112 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldProcessing\User;
use Espo\Core\Name\Field;
use Espo\Entities\AuthLogRecord;
use Espo\Entities\AuthToken;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Core\Acl;
use Espo\Core\FieldProcessing\Loader;
use Espo\Core\FieldProcessing\Loader\Params;
use Espo\Core\ORM\EntityManager;
use DateTime;
use Espo\ORM\Name\Attribute;
use Exception;
/**
* @implements Loader<User>
* @noinspection PhpUnused
*/
class LastAccessLoader implements Loader
{
private EntityManager $entityManager;
private Acl $acl;
public function __construct(EntityManager $entityManager, Acl $acl)
{
$this->entityManager = $entityManager;
$this->acl = $acl;
}
public function process(Entity $entity, Params $params): void
{
if (!$this->acl->checkField($entity->getEntityType(), 'lastAccess')) {
return;
}
$authToken = $this->entityManager
->getRDBRepository(AuthToken::ENTITY_TYPE)
->select([Attribute::ID, 'lastAccess'])
->where([
'userId' => $entity->getId(),
])
->order('lastAccess', 'DESC')
->findOne();
$lastAccess = null;
if ($authToken) {
$lastAccess = $authToken->get('lastAccess');
}
$dt = null;
if ($lastAccess) {
try {
$dt = new DateTime($lastAccess);
} catch (Exception) {}
}
$where = [
'userId' => $entity->getId(),
'isDenied' => false,
];
if ($dt) {
$where['requestTime>'] = $dt->format('U');
}
$authLogRecord = $this->entityManager
->getRDBRepository(AuthLogRecord::ENTITY_TYPE)
->select([Attribute::ID, Field::CREATED_AT])
->where($where)
->order('requestTime', true)
->findOne();
if ($authLogRecord) {
$lastAccess = $authLogRecord->get(Field::CREATED_AT);
}
$entity->set('lastAccess', $lastAccess);
}
}

View File

@@ -0,0 +1,54 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class ArrayFromNull implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if ($value !== null) {
return;
}
$data->set($field, []);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class ArrayStringTrim implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if (!is_array($value)) {
return;
}
foreach ($value as $i => $item) {
if (!is_string($item)) {
continue;
}
$value[$i] = trim($item);
}
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,69 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use Espo\Core\Field\Date as DateValue;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class Date implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$value = $data->get($field);
if ($value === null) {
return;
}
try {
DateValue::fromString($value);
return;
} catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime->format(DateTimeUtil::SYSTEM_DATE_FORMAT);
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Espo\Core\Field\DateTime as DateTimeValue;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class Datetime implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$value = $data->get($field);
if ($value === null) {
return;
}
try {
DateTimeValue::fromString($value);
return;
} catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime
->setTimezone(new DateTimeZone('UTC'))
->format(DateTimeUtil::SYSTEM_DATE_TIME_FORMAT);
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,71 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use DateTimeImmutable;
use DateTimeInterface;
use Espo\Core\Field\Date;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Exception;
/**
* @noinspection PhpUnused
*/
class DatetimeOptionalDate implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
$attribute = $field . 'Date';
$value = $data->get($attribute);
if ($value === null) {
return;
}
try {
Date::fromString($value);
return;
} catch (Exception) {}
$dateTime = DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, $value);
if ($dateTime === false) {
return;
}
$value = $dateTime->format(DateTimeUtil::SYSTEM_DATE_FORMAT);
$data->set($attribute, $value);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class EmptyStringToNull implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if (!is_string($value)) {
return;
}
if ($value === '') {
$value = null;
}
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,77 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
use Espo\Core\PhoneNumber\Sanitizer as PhoneNumberSanitizer;
use stdClass;
class Phone implements Sanitizer
{
public function __construct(
private PhoneNumberSanitizer $phoneNumberSanitizer
) {}
public function sanitize(Data $data, string $field): void
{
$number = $data->get($field);
if ($number !== null) {
$number = $this->phoneNumberSanitizer->sanitize($number);
$data->set($field, $number);
}
$items = $data->get($field . 'Data');
if (!is_array($items)) {
return;
}
foreach ($items as $item) {
if (!$item instanceof stdClass) {
continue;
}
$number = $item->phoneNumber ?? null;
if (!is_scalar($number)) {
continue;
}
$number = (string) $number;
$item->phoneNumber = $this->phoneNumberSanitizer->sanitize($number);
}
$data->set($field . 'Data', $items);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class StringLowerCase implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if (!is_string($value)) {
return;
}
$value = mb_strtolower($value);
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class StringTrim implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if (!is_string($value)) {
return;
}
$value = trim($value);
if ($value === '') {
$value = null;
}
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldSanitizers;
use Espo\Core\FieldSanitize\Sanitizer;
use Espo\Core\FieldSanitize\Sanitizer\Data;
/**
* @noinspection PhpUnused
*/
class StringUpperCase implements Sanitizer
{
public function sanitize(Data $data, string $field): void
{
if (!$data->has($field)) {
return;
}
$value = $data->get($field);
if (!is_string($value)) {
return;
}
$value = mb_strtoupper($value);
$data->set($field, $value);
}
}

View File

@@ -0,0 +1,35 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators;
class ArrayIntType extends ArrayType
{
}

View File

@@ -0,0 +1,253 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Defs;
use Espo\ORM\Entity;
use stdClass;
class ArrayType
{
private const DEFAULT_MAX_ITEM_LENGTH = 100;
public function __construct(protected Metadata $metadata, private Defs $defs)
{}
public function checkRequired(Entity $entity, string $field): bool
{
return $this->isNotEmpty($entity, $field);
}
public function checkMaxCount(Entity $entity, string $field, int $validationValue): bool
{
if (!$this->isNotEmpty($entity, $field)) {
return true;
}
$list = $entity->get($field);
if (count($list) > $validationValue) {
return false;
}
return true;
}
public function checkArrayOfString(Entity $entity, string $field): bool
{
/** @var ?mixed[] $list */
$list = $entity->get($field);
if ($list === null) {
return true;
}
foreach ($list as $item) {
if (!is_string($item)) {
return false;
}
}
return true;
}
public function checkValid(Entity $entity, string $field): bool
{
if (!$entity->has($field)) {
return true;
}
/** @var ?string[] $value */
$value = $entity->get($field);
if ($value === null || $value === []) {
return true;
}
$fieldDefs = $this->defs
->getEntity($entity->getEntityType())
->getField($field);
if ($fieldDefs->getParam('allowCustomOptions')) {
return true;
}
$optionList = $this->getOptionList($entity->getEntityType(), $field);
if ($optionList === null) {
return true;
}
foreach ($value as $item) {
if (!in_array($item, $optionList)) {
return false;
}
}
return true;
}
/**
* @return ?string[]
*/
private function getOptionList(string $entityType, string $field): ?array
{
$fieldDefs = $this->defs
->getEntity($entityType)
->getField($field);
/** @var ?string $path */
$path = $fieldDefs->getParam('optionsPath');
/** @var ?string $path */
$ref = $fieldDefs->getParam('optionsReference');
if (!$path && $ref && str_contains($ref, '.')) {
[$refEntityType, $refField] = explode('.', $ref);
$path = "entityDefs.{$refEntityType}.fields.{$refField}.options";
}
/** @var string[]|null|false $optionList */
$optionList = $path ?
$this->metadata->get($path) :
$fieldDefs->getParam('options');
if ($optionList === null) {
return null;
}
// For bc.
if ($optionList === false) {
return null;
}
return $optionList;
}
public function rawCheckArray(stdClass $data, string $field): bool
{
if (isset($data->$field) && !is_array($data->$field)) {
return false;
}
return true;
}
protected function isNotEmpty(Entity $entity, string $field): bool
{
if (!$entity->has($field) || $entity->get($field) === null) {
return false;
}
$list = $entity->get($field);
if (!is_array($list)) {
return false;
}
if (count($list)) {
return true;
}
return false;
}
public function checkMaxItemLength(Entity $entity, string $field, ?int $validationValue): bool
{
$maxLength = $validationValue ?? self::DEFAULT_MAX_ITEM_LENGTH;
/** @var mixed[] $value */
$value = $entity->get($field) ?? [];
foreach ($value as $item) {
if (is_string($item) && mb_strlen($item) > $maxLength) {
return false;
}
}
return true;
}
public function checkPattern(Entity $entity, string $field, ?string $validationValue): bool
{
if (!$validationValue) {
return true;
}
$pattern = $validationValue;
if ($validationValue[0] === '$') {
$patternName = substr($validationValue, 1);
$pattern = $this->metadata->get(['app', 'regExpPatterns', $patternName, 'pattern']) ??
$pattern;
}
$preparedPattern = '/^' . $pattern . '$/';
/** @var string[] $value */
$value = $entity->get($field) ?? [];
foreach ($value as $item) {
if ($item === '') {
continue;
}
if (!preg_match($preparedPattern, $item)) {
return false;
}
}
return true;
}
public function checkNoEmptyString(Entity $entity, string $field, ?bool $validationValue): bool
{
if (!$validationValue) {
return true;
}
/** @var string[] $value */
$value = $entity->get($field) ?? [];
$optionList = $this->getOptionList($entity->getEntityType(), $field) ?? [];
foreach ($value as $item) {
if ($item === '' && !in_array($item, $optionList)) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators\Attachment;
use Espo\Classes\FieldValidators\LinkParentType;
use Espo\ORM\Entity;
class Related extends LinkParentType
{
public function checkValid(Entity $entity, string $field): bool
{
$typeValue = $entity->get($field . 'Type');
if ($typeValue === 'TemplateManager') {
return true;
}
return parent::checkValid($entity, $field);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators\AuthenticationProvider;
use Espo\Core\FieldValidation\Validator;
use Espo\Core\FieldValidation\Validator\Data;
use Espo\Core\FieldValidation\Validator\Failure;
use Espo\Core\Utils\Metadata;
use Espo\Entities\AuthenticationProvider;
use Espo\ORM\Entity;
/**
* @implements Validator<AuthenticationProvider>
*/
class MethodValid implements Validator
{
public function __construct(private Metadata $metadata) {}
public function validate(Entity $entity, string $field, Data $data): ?Failure
{
$value = $entity->get($field);
if (!$value) {
return Failure::create();
}
$isAvailable = $this->metadata->get(['authenticationMethods', $value, 'provider', 'isAvailable']);
if (!$isAvailable) {
return Failure::create();
}
return null;
}
}

View File

@@ -0,0 +1,32 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators;
class ChecklistType extends ArrayType {}

View File

@@ -0,0 +1,139 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators;
use Espo\Core\Field\Currency;
use Espo\Core\Utils\Config;
use Espo\ORM\BaseEntity;
use Espo\ORM\Defs\Params\AttributeParam;
use Espo\ORM\Entity;
class CurrencyType extends FloatType
{
private const DEFAULT_PRECISION = 13;
public function __construct(private Config $config) {}
protected function isNotEmpty(Entity $entity, string $field): bool
{
return
$entity->has($field) && $entity->get($field) !== null &&
$entity->has($field . 'Currency') && $entity->get($field . 'Currency') !== null &&
$entity->get($field . 'Currency') !== '';
}
public function checkValid(Entity $entity, string $field): bool
{
if (!$this->isNotEmpty($entity, $field)) {
return true;
}
if ($entity->getAttributeType($field) !== Entity::VARCHAR) {
return true;
}
/** @var string $value */
$value = $entity->get($field);
if (preg_match('/^-?[0-9]+\.?[0-9]*$/', $value)) {
return true;
}
return false;
}
public function checkInPermittedRange(Entity $entity, string $field): bool
{
if (!$this->isNotEmpty($entity, $field)) {
return true;
}
if ($entity->getAttributeType($field) !== Entity::VARCHAR) {
return true;
}
if (!$entity instanceof BaseEntity) {
return true;
}
/** @var int $precision */
$precision = $entity->getAttributeParam($field, AttributeParam::PRECISION) ?? self::DEFAULT_PRECISION;
$value = $entity->get($field);
$currency = Currency::create($value, 'USD');
if ($currency->isNegative()) {
$currency = $currency->multiply(-1);
}
$pad = str_pad('', $precision, '9');
assert(is_numeric($pad));
$limit = Currency::create($pad, 'USD');
if ($currency->compare($limit) === 1) {
return false;
}
return true;
}
public function checkValidCurrency(Entity $entity, string $field): bool
{
$attribute = $field . 'Currency';
if (!$entity->has($attribute)) {
return true;
}
$currency = $entity->get($attribute);
$currencyList = $this->config->get('currencyList') ?? [$this->config->get('defaultCurrency')];
if (
$currency === null &&
!$entity->has($field) &&
$entity->isNew()
) {
return true;
}
if (
$currency === null &&
$entity->has($field) &&
$entity->get($field) === null
) {
return true;
}
return in_array($currency, $currencyList);
}
}

View File

@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Classes\FieldValidators;
use Espo\Core\Field\Date;
use Espo\ORM\Entity;
use Exception;
class DateType
{
public function checkRequired(Entity $entity, string $field): bool
{
return $this->isNotEmpty($entity, $field);
}
protected function isNotEmpty(Entity $entity, string $field): bool
{
return $entity->has($field) && $entity->get($field) !== null;
}
public function checkValid(Entity $entity, string $field): bool
{
/** @var ?string $value */
$value = $entity->get($field);
if ($value === null) {
return true;
}
try {
Date::fromString($value);
} catch (Exception $e) {
return false;
}
return true;
}
}

Some files were not shown because too many files have changed in this diff Show More