Skip to content

EspoCRM – Open Source CRM Application

ref

Yuri Kuznetsov9fac1d6c5187
diff --git a/application/Espo/Classes/RecordHooks/User/BeforeDelete.php b/application/Espo/Classes/RecordHooks/User/BeforeDelete.php
new file mode 100644
index 0000000000..1938e04993
--- /dev/null
+++ b/application/Espo/Classes/RecordHooks/User/BeforeDelete.php
@@ -0,0 +1,53 @@
+<?php
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
+ * 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\RecordHooks\User;
+
+use Espo\Core\Exceptions\Forbidden;
+use Espo\Core\Record\DeleteParams;
+use Espo\Core\Record\Hook\DeleteHook;
+use Espo\Entities\User;
+use Espo\ORM\Entity;
+
+/**
+ * @implements DeleteHook<User>
+ */
+class BeforeDelete implements DeleteHook
+{
+ public function __construct(
+ private User $user,
+ ) {}
+
+ public function process(Entity $entity, DeleteParams $params): void
+ {
+ if ($entity->getId() === $this->user->getId()) {
+ throw new Forbidden("Can't delete own user.");
+ }
+ }
+}
diff --git a/application/Espo/Resources/metadata/recordDefs/User.json b/application/Espo/Resources/metadata/recordDefs/User.json
index 7df9200514..13ac541cde 100644
--- a/application/Espo/Resources/metadata/recordDefs/User.json
+++ b/application/Espo/Resources/metadata/recordDefs/User.json
@@ -32,5 +32,8 @@
],
"afterUpdateHookClassNameList": [
"Espo\\Classes\\RecordHooks\\User\\AfterUpdate"
+ ],
+ "beforeDeleteHookClassNameList": [
+ "Espo\\Classes\\RecordHooks\\User\\BeforeDelete"
]
}
diff --git a/application/Espo/Services/User.php b/application/Espo/Services/User.php
index 3df03af525..51e34c7fd9 100644
--- a/application/Espo/Services/User.php
+++ b/application/Espo/Services/User.php
@@ -38,7 +38,6 @@ use Espo\Entities\User as UserEntity;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\CreateParams;
-use Espo\Core\Record\DeleteParams;
use Espo\Core\Record\UpdateParams;
use Espo\Core\Utils\PasswordHash;
use Espo\ORM\Entity;
@@ -214,15 +213,6 @@ class User extends Record implements LogAware
}
}
- public function delete(string $id, DeleteParams $params): void
- {
- if ($id === $this->user->getId()) {
- throw new Forbidden("Can't delete own user.");
- }
-
- parent::delete($id, $params);
- }
-
/**
* @throws Forbidden
* @throws NotFound