deleteId param

This commit is contained in:
Yuri Kuznetsov
2024-06-26 13:27:20 +03:00
parent cc576a6af8
commit 338e0bb9d4
8 changed files with 119 additions and 32 deletions
+15 -2
View File
@@ -1115,10 +1115,23 @@ class Service implements Crud,
}
if (!$entity->get('deleted')) {
throw new Forbidden();
throw new Forbidden("No 'deleted' attribute.");
}
$this->getRepository()->restoreDeleted($entity->getId());
$this->entityManager->getTransactionManager()
->run(function () use ($entity) {
$this->getRepository()->restoreDeleted($entity->getId());
if (
$entity->hasAttribute('deleteId') &&
$this->metadata->get("entityDefs.$this->entityType.deleteId")
) {
$this->entityManager->refreshEntity($entity);
$entity->set('deleteId', '0');
$this->getRepository()->save($entity, [SaveOption::SILENT => true]);
}
});
}
public function getMaxSelectTextAttributeLength(): ?int
@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2024 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\Core\Utils\Metadata\AdditionalBuilder;
use Espo\Core\Utils\Metadata\AdditionalBuilder;
use stdClass;
class DeleteIdField implements AdditionalBuilder
{
public function build(stdClass $data): void
{
if (!isset($data->entityDefs)) {
return;
}
foreach (get_object_vars($data->entityDefs) as $entityType => $entityDefsItem) {
if (!($entityDefsItem->deleteId ?? false)) {
continue;
}
$entityDefsItem->fields ??= (object) [];
$data->entityDefs->$entityType->fields->deleteId = (object) [
"type" => "varchar",
"maxLength" => 17,
"readOnly" => true,
"notNull" => true,
"default" => "0",
"utility" => true,
"customizationDisabled" => true
];
}
}
}
@@ -27,35 +27,58 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Hooks\User;
namespace Espo\Hooks\Common;
use Espo\Core\Hook\Hook\BeforeRemove;
use Espo\Core\Hook\Hook\BeforeSave;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Util;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\ORM\Repository\Option\RemoveOptions;
use Espo\ORM\Repository\Option\SaveOptions;
use Espo\Core\Hook\Hook\BeforeSave;
/**
* @implements BeforeRemove<User>
* @implements BeforeSave<User>
* Handles 'deleteId' on soft-deletes.
*
* @implements BeforeSave<Entity>
* @implements BeforeRemove<Entity>
*/
class DeleteId implements BeforeRemove, BeforeSave
class DeleteId implements BeforeSave, BeforeRemove
{
private const ID_ATTR = 'deleteId';
private const DELETED_ATTR = 'deleted';
public function __construct(
private Metadata $metadata,
) {}
public function beforeRemove(Entity $entity, RemoveOptions $options): void
{
$entity->set('deleteId', Util::generateId());
if (!$this->hasDeleteId($entity)) {
return;
}
$entity->set(self::ID_ATTR, Util::generateId());
}
public function beforeSave(Entity $entity, SaveOptions $options): void
{
if (!$entity->isAttributeChanged('deleted')) {
if (!$this->hasDeleteId($entity)) {
return;
}
$deleteId = $entity->get('deleted') ? Util::generateId() : '0';
if (!$entity->isAttributeChanged(self::DELETED_ATTR)) {
return;
}
$entity->set('deleteId', $deleteId);
$deleteId = $entity->get(self::DELETED_ATTR) ? Util::generateId() : '0';
$entity->set(self::ID_ATTR, $deleteId);
}
private function hasDeleteId(Entity $entity): bool
{
return $entity->hasAttribute(self::DELETED_ATTR) &&
$this->metadata->get("entityDefs.{$entity->getEntityType()}.deleteId");
}
}
@@ -44,6 +44,7 @@
["recordDefs"]
],
"additionalBuilderClassNameList": [
"Espo\\Core\\Utils\\Metadata\\AdditionalBuilder\\Fields"
"Espo\\Core\\Utils\\Metadata\\AdditionalBuilder\\Fields",
"Espo\\Core\\Utils\\Metadata\\AdditionalBuilder\\DeleteIdField"
]
}
@@ -484,15 +484,6 @@
"directAccessDisabled": true,
"exportDisabled": true
},
"deleteId": {
"type": "varchar",
"maxLength": 17,
"readOnly": true,
"notNull": true,
"default": "0",
"utility": true,
"customizationDisabled": true
},
"emailAddressList": {
"type": "array",
"utility": true,
@@ -636,5 +627,6 @@
"deleteId"
]
}
}
},
"deleteId": true
}
-8
View File
@@ -257,14 +257,6 @@ class User extends Record
}
parent::restoreDeleted($id);
$entity = $this->getRepository()->getById($id);
if ($entity) {
$entity->set('deleteId', '0');
$this->getRepository()->save($entity);
}
}
private function createPasswordChecker(): PasswordChecker
@@ -55,6 +55,7 @@ class FieldManager
private $forbiddenFieldNameList = [
'id',
'deleted',
'deleteId',
'skipDuplicateCheck',
'isFollowed',
'isStarred',
+5 -1
View File
@@ -82,7 +82,11 @@
},
"noDeletedAttribute": {
"type": "boolean",
"description": "Do not add the `deleted` attribute."
"description": "Do not add the `deleted` attribute. Soft-deletes will be impossible."
},
"deleteId": {
"type": "boolean",
"description": "Adds `deletedId` attribute. An random ID is written there on soft-deletes. As of v8.4."
},
"hooksDisabled": {
"type": "boolean",