EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
formula user update type check
Yuri Kuznetsovb98f53563466
diff --git a/application/Espo/Core/Formula/Exceptions/NotAllowedUsage.php b/application/Espo/Core/Formula/Exceptions/NotAllowedUsage.phpnew file mode 100644index 0000000000..6ddf5c59ac--- /dev/null+++ b/application/Espo/Core/Formula/Exceptions/NotAllowedUsage.php@@ -0,0 +1,36 @@+<?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\Core\Formula\Exceptions;++/**+ * @since 9.3.0+ */+class NotAllowedUsage extends Error+{}diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.phpindex 3ad1998d2d..d07850da3b 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/CreateType.php@@ -32,8 +32,8 @@ namespace Espo\Core\Formula\Functions\RecordGroup;use Espo\Core\Formula\ArgumentList;use Espo\Core\Formula\Exceptions\BadArgumentType;use Espo\Core\Formula\Functions\BaseFunction;-use Espo\Core\Di;+use Espo\Core\Formula\Utils\EntityUtil;use RuntimeException;use stdClass;@@ -65,7 +65,12 @@ class CreateType extends BaseFunction implements$data = $this->getData($args, $entityType);- $entity = $this->entityManager->createEntity($entityType, $data);+ $entity = $this->entityManager->getNewEntity($entityType);+ $entity->setMultiple($data);++ EntityUtil::checkUpdateAccess($entity);++ $this->entityManager->saveEntity($entity);return $entity->getId();}diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/DeleteType.php b/application/Espo/Core/Formula/Functions/RecordGroup/DeleteType.phpindex 7437d8b2fe..05a72029bb 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/DeleteType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/DeleteType.php@@ -33,6 +33,7 @@ use Espo\Core\Formula\EvaluatedArgumentList;use Espo\Core\Formula\Exceptions\BadArgumentType;use Espo\Core\Formula\Exceptions\TooFewArguments;use Espo\Core\Formula\Func;+use Espo\Core\Formula\Utils\EntityUtil;use Espo\ORM\EntityManager;class DeleteType implements Func@@ -62,6 +63,8 @@ class DeleteType implements Funcreturn null;}+ EntityUtil::checkRemoveAccess($entity);+$this->entityManager->removeEntity($entity);return null;diff --git a/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php b/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.phpindex d6e27a7e7e..27e93149cb 100644--- a/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php+++ b/application/Espo/Core/Formula/Functions/RecordGroup/UpdateType.php@@ -32,8 +32,8 @@ namespace Espo\Core\Formula\Functions\RecordGroup;use Espo\Core\Formula\ArgumentList;use Espo\Core\Formula\Exceptions\BadArgumentType;use Espo\Core\Formula\Functions\BaseFunction;-use Espo\Core\Di;+use Espo\Core\Formula\Utils\EntityUtil;use RuntimeException;use stdClass;@@ -78,6 +78,8 @@ class UpdateType extends BaseFunction implements$entity->set($data);+ EntityUtil::checkUpdateAccess($entity);+$this->entityManager->saveEntity($entity);return true;diff --git a/application/Espo/Core/Formula/Functions/SetAttributeType.php b/application/Espo/Core/Formula/Functions/SetAttributeType.phpindex 7e8ed394c6..869d8f9586 100644--- a/application/Espo/Core/Formula/Functions/SetAttributeType.php+++ b/application/Espo/Core/Formula/Functions/SetAttributeType.php@@ -30,7 +30,9 @@namespace Espo\Core\Formula\Functions;use Espo\Core\Formula\Exceptions\Error;+use Espo\Core\Formula\Utils\EntityUtil;use Espo\ORM\Name\Attribute;+use stdClass;class SetAttributeType extends Base{@@ -38,7 +40,7 @@ class SetAttributeType extends Base* @return mixed* @throws Error*/- public function process(\stdClass $item)+ public function process(stdClass $item){if (count($item->value) < 2) {throw new Error("SetAttribute: Too few arguments.");@@ -56,7 +58,11 @@ class SetAttributeType extends Base$value = $this->evaluate($item->value[1]);- $this->getEntity()->set($name, $value);+ $entity = $this->getEntity();++ $entity->set($name, $value);++ EntityUtil::checkUpdateAccess($entity);return $value;}diff --git a/application/Espo/Core/Formula/Utils/EntityUtil.php b/application/Espo/Core/Formula/Utils/EntityUtil.phpnew file mode 100644index 0000000000..1263fb367d--- /dev/null+++ b/application/Espo/Core/Formula/Utils/EntityUtil.php@@ -0,0 +1,84 @@+<?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\Core\Formula\Utils;++use Espo\Core\Formula\Exceptions\NotAllowedUsage;+use Espo\Entities\User;+use Espo\ORM\Entity;++/**+ * @internal+ * @since 9.3.0+ */+class EntityUtil+{+ /**+ * @throws NotAllowedUsage+ */+ public static function checkUpdateAccess(Entity $entity): void+ {+ if ($entity instanceof User) {+ $restrictedTypeList = self::getUserRestrictedTypeList();++ if (+ $entity->isAttributeChanged(User::ATTR_TYPE) &&+ (+ in_array($entity->getFetched(User::ATTR_TYPE), $restrictedTypeList) ||+ in_array($entity->getType(), $restrictedTypeList)+ )+ ) {+ throw new NotAllowedUsage("Cannot change user type.");+ }+ }+ }++ /**+ * @throws NotAllowedUsage+ */+ public static function checkRemoveAccess(Entity $entity): void+ {+ if ($entity instanceof User) {+ if (in_array($entity->getType(), self::getUserRestrictedTypeList())) {+ throw new NotAllowedUsage("Cannot remove the user.");+ }+ }+ }++ /**+ * @return string[]+ */+ private static function getUserRestrictedTypeList(): array+ {+ return [+ User::TYPE_SUPER_ADMIN,+ User::TYPE_SYSTEM,+ ];+ }+}