delete restorer

This commit is contained in:
Yuri Kuznetsov
2025-08-10 15:52:59 +03:00
parent 9fac1d6c51
commit f158501ed6
8 changed files with 209 additions and 59 deletions
@@ -0,0 +1,69 @@
<?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\Record\User;
use Espo\Core\Exceptions\Conflict;
use Espo\Core\Record\Deleted\DefaultRestorer;
use Espo\Core\Record\Deleted\Restorer;
use Espo\Entities\User;
use Espo\ORM\Entity;
use Espo\Tools\User\UserUtil;
/**
* @implements Restorer<User>
*/
class DeletedRestorer implements Restorer
{
public function __construct(
private DefaultRestorer $defaultRestorer,
private UserUtil $userUtil,
) {}
/**
* @inheritDoc
*/
public function restore(Entity $entity): void
{
$this->processUserExistsChecking($entity);
$this->defaultRestorer->restore($entity);
}
/**
* @throws Conflict
*/
private function processUserExistsChecking(User $user): void
{
if ($this->userUtil->checkExists($user)) {
throw new Conflict('userNameExists');
}
}
}
@@ -317,6 +317,7 @@ class RecordBase extends Base implements
* @throws BadRequest
* @throws Forbidden
* @throws NotFound
* @throws Conflict
* @noinspection PhpUnused
*/
public function postActionRestoreDeleted(Request $request): bool
@@ -0,0 +1,76 @@
<?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\Core\Record\Deleted;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\ORM\Repository\Option\SaveOption;
use Espo\Core\Utils\Metadata;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
/**
* @implements Restorer<Entity>
*/
class DefaultRestorer implements Restorer
{
public function __construct(
private EntityManager $entityManager,
private Metadata $metadata,
) {}
public function restore(Entity $entity): void
{
if (!$entity->get(Attribute::DELETED)) {
throw new Forbidden("No 'deleted' attribute.");
}
$this->entityManager
->getTransactionManager()
->run(fn () => $this->restoreInTransaction($entity));
}
private function restoreInTransaction(Entity $entity): void
{
$repository = $this->entityManager->getRDBRepository($entity->getEntityType());
$repository->restoreDeleted($entity->getId());
if (
$entity->hasAttribute('deleteId') &&
$this->metadata->get("entityDefs.{$entity->getEntityType()}.deleteId")
) {
$this->entityManager->refreshEntity($entity);
$entity->set('deleteId', '0');
$repository->save($entity, [SaveOption::SILENT => true]);
}
}
}
@@ -0,0 +1,47 @@
<?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\Core\Record\Deleted;
use Espo\Core\Exceptions\Conflict;
use Espo\Core\Exceptions\Forbidden;
use Espo\ORM\Entity;
/**
* @template TEntity of Entity
*/
interface Restorer
{
/**
* @param TEntity $entity A deleted entity.
* @throws Forbidden
* @throws Conflict
*/
public function restore(Entity $entity): void;
}
+10 -17
View File
@@ -51,6 +51,8 @@ use Espo\Core\Record\ActionHistory\ActionLogger;
use Espo\Core\Record\ConcurrencyControl\OptimisticProcessor;
use Espo\Core\Record\Defaults\Populator as DefaultsPopulator;
use Espo\Core\Record\Defaults\PopulatorFactory as DefaultsPopulatorFactory;
use Espo\Core\Record\Deleted\DefaultRestorer;
use Espo\Core\Record\Deleted\Restorer;
use Espo\Core\Record\DynamicLogic\InputFilterProcessor;
use Espo\Core\Record\Formula\Processor as FormulaProcessor;
use Espo\Core\Record\Input\Data;
@@ -950,11 +952,12 @@ class Service implements Crud,
*
* @throws NotFound If not found.
* @throws Forbidden If no access.
* @throws Conflict If a conflict occurred.
*/
public function restoreDeleted(string $id): void
{
if (!$this->user->isAdmin()) {
throw new Forbidden();
throw new Forbidden("Only admin can restore.");
}
$entity = $this->getEntityEvenDeleted($id);
@@ -963,24 +966,14 @@ class Service implements Crud,
throw new NotFound();
}
if (!$entity->get(Attribute::DELETED)) {
throw new Forbidden("No 'deleted' attribute.");
}
/** @var class-string<Restorer<Entity>> $restorerClassName */
$restorerClassName = $this->metadata->get("recordDefs.$this->entityType.deletedRestorerClassName") ??
DefaultRestorer::class;
$this->entityManager->getTransactionManager()
->run(function () use ($entity) {
$this->getRepository()->restoreDeleted($entity->getId());
/** @var Restorer<Entity> $restorer */
$restorer = $this->injectableFactory->createWithBinding($restorerClassName, $this->createBinding());
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]);
}
});
$restorer->restore($entity);
}
public function getMaxSelectTextAttributeLength(): ?int
@@ -35,5 +35,6 @@
],
"beforeDeleteHookClassNameList": [
"Espo\\Classes\\RecordHooks\\User\\BeforeDelete"
]
],
"deletedRestorerClassName": "Espo\\Classes\\Record\\User\\DeletedRestorer"
}
-41
View File
@@ -31,8 +31,6 @@ namespace Espo\Services;
use Espo\Core\Di\LogAware;
use Espo\Core\Di\LogSetter;
use Espo\Core\Exceptions\Conflict;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Mail\Exceptions\SendingError;
use Espo\Entities\User as UserEntity;
use Espo\Core\Exceptions\BadRequest;
@@ -41,9 +39,6 @@ use Espo\Core\Record\CreateParams;
use Espo\Core\Record\UpdateParams;
use Espo\Core\Utils\PasswordHash;
use Espo\ORM\Entity;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Query\SelectBuilder;
use Espo\Tools\User\UserUtil;
use Espo\Tools\UserSecurity\Password\Checker as PasswordChecker;
use Espo\Tools\UserSecurity\Password\Generator as PasswordGenerator;
use Espo\Tools\UserSecurity\Password\Sender as PasswordSender;
@@ -201,42 +196,6 @@ class User extends Record implements LogAware
->sendPassword($user, $password);
}
/**
* @throws Conflict
*/
private function processUserExistsChecking(UserEntity $user): void
{
$util = $this->injectableFactory->create(UserUtil::class);
if ($util->checkExists($user)) {
throw new Conflict('userNameExists');
}
}
/**
* @throws Forbidden
* @throws NotFound
* @throws Conflict
*/
public function restoreDeleted(string $id): void
{
$entity = $this->getRepository()
->clone(
SelectBuilder::create()
->from(UserEntity::ENTITY_TYPE)
->withDeleted()
->build()
)
->where([Attribute::ID => $id])
->findOne();
if ($entity) {
$this->processUserExistsChecking($entity);
}
parent::restoreDeleted($id);
}
private function createPasswordChecker(): PasswordChecker
{
return $this->injectableFactory->create(PasswordChecker::class);
+4
View File
@@ -281,6 +281,10 @@
"items": {
"type": "string"
}
},
"deletedRestorerClassName": {
"type": "string",
"description": "A deleted record restorer. Should implement `Espo\\Core\\Record\\Deleted\\Restorer`. As of v9.2."
}
}
}