From d460805c8b09518a8ba5793c8c218902ff046241 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 28 Apr 2023 15:57:25 +0300 Subject: [PATCH] ref --- application/Espo/Core/Field/LinkParent.php | 9 ++++ application/Espo/Core/Record/Service.php | 42 +++++++++-------- .../Espo/Entities/ActionHistoryRecord.php | 47 +++++++++++++++++++ 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/application/Espo/Core/Field/LinkParent.php b/application/Espo/Core/Field/LinkParent.php index 1d97e725cf..2c9abbf677 100644 --- a/application/Espo/Core/Field/LinkParent.php +++ b/application/Espo/Core/Field/LinkParent.php @@ -29,6 +29,7 @@ namespace Espo\Core\Field; +use Espo\ORM\Entity; use RuntimeException; /** @@ -99,4 +100,12 @@ class LinkParent { return new self($entityType, $id); } + + /** + * Create from an entity. + */ + public static function createFromEntity(Entity $entity): self + { + return new self($entity->getEntityType(), $entity->getId()); + } } diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php index 1970aaca98..10dd05ce39 100644 --- a/application/Espo/Core/Record/Service.php +++ b/application/Espo/Core/Record/Service.php @@ -33,7 +33,6 @@ use Espo\Core\Binding\BindingContainerBuilder; use Espo\Core\Binding\ContextualBinder; use Espo\Core\Exceptions\Conflict; use Espo\Core\Exceptions\Error; -use Espo\Core\ORM\Entity as CoreEntity; use Espo\Core\Exceptions\Error\Body as ErrorBody; use Espo\Core\Exceptions\BadRequest; use Espo\Core\Exceptions\ConflictSilent; @@ -41,15 +40,9 @@ use Espo\Core\Exceptions\Forbidden; use Espo\Core\Exceptions\ForbiddenSilent; use Espo\Core\Exceptions\NotFound; use Espo\Core\Exceptions\NotFoundSilent; +use Espo\Core\Field\LinkParent; +use Espo\Core\ORM\Entity as CoreEntity; use Espo\Core\Record\Access\LinkCheck; -use Espo\Entities\ActionHistoryRecord; -use Espo\ORM\Entity; -use Espo\ORM\Repository\RDBRepository; -use Espo\ORM\Collection; -use Espo\ORM\EntityManager; -use Espo\ORM\Query\Part\WhereClause; -use Espo\Entities\User; -use Espo\Tools\Stream\Service as StreamService; use Espo\Core\Utils\Json; use Espo\Core\Acl; use Espo\Core\Acl\Table as AclTable; @@ -65,6 +58,14 @@ use Espo\Core\Record\Select\ApplierClassNameListProvider; use Espo\Core\Select\SearchParams; use Espo\Core\Select\SelectBuilderFactory; use Espo\Core\Di; +use Espo\ORM\Entity; +use Espo\ORM\Repository\RDBRepository; +use Espo\ORM\Collection; +use Espo\ORM\EntityManager; +use Espo\ORM\Query\Part\WhereClause; +use Espo\Tools\Stream\Service as StreamService; +use Espo\Entities\User; +use Espo\Entities\ActionHistoryRecord; use stdClass; use InvalidArgumentException; use LogicException; @@ -204,6 +205,11 @@ class Service implements Crud, return $this->entityManager->getRDBRepository($this->entityType); } + /** + * Add an action-history record. + * + * @param ActionHistoryRecord::ACTION_* $action + */ public function processActionHistoryRecord(string $action, Entity $entity): void { if ($this->actionHistoryDisabled) { @@ -214,18 +220,16 @@ class Service implements Crud, return; } + /** @var ActionHistoryRecord $historyRecord */ $historyRecord = $this->entityManager->getNewEntity(ActionHistoryRecord::ENTITY_TYPE); - $historyRecord->set('action', $action); - $historyRecord->set('userId', $this->user->getId()); - $historyRecord->set('authTokenId', $this->user->get('authTokenId')); - $historyRecord->set('ipAddress', $this->user->get('ipAddress')); - $historyRecord->set('authLogRecordId', $this->user->get('authLogRecordId')); - - $historyRecord->set([ - 'targetType' => $entity->getEntityType(), - 'targetId' => $entity->getId() - ]); + $historyRecord + ->setAction($action) + ->setUserId($this->user->getId()) + ->setAuthTokenId($this->user->get('authTokenId')) + ->setAuthLogRecordId($this->user->get('authLogRecordId')) + ->setIpAddress($this->user->get('ipAddress')) + ->setTarget(LinkParent::createFromEntity($entity)); $this->entityManager->saveEntity($historyRecord); } diff --git a/application/Espo/Entities/ActionHistoryRecord.php b/application/Espo/Entities/ActionHistoryRecord.php index fcb02622f2..4210ad40aa 100644 --- a/application/Espo/Entities/ActionHistoryRecord.php +++ b/application/Espo/Entities/ActionHistoryRecord.php @@ -29,6 +29,7 @@ namespace Espo\Entities; +use Espo\Core\Field\LinkParent; use Espo\Core\ORM\Entity; class ActionHistoryRecord extends Entity @@ -39,4 +40,50 @@ class ActionHistoryRecord extends Entity public const ACTION_UPDATE = 'update'; public const ACTION_CREATE = 'create'; public const ACTION_DELETE = 'delete'; + + /** + * @param self::ACTION_* $action + */ + public function setAction(string $action): self + { + $this->set('action', $action); + + return $this; + } + + public function setUserId(string $userId): self + { + $this->set('userId', $userId); + + return $this; + } + + public function setIpAddress(?string $ipAddress): self + { + $this->set('ipAddress', $ipAddress); + + return $this; + } + + public function setAuthTokenId(?string $authTokenId): self + { + $this->set('authTokenId', $authTokenId); + + return $this; + } + + public function setAuthLogRecordId(?string $authLogRecordId): self + { + $this->set('authLogRecordId', $authLogRecordId); + + return $this; + } + + public function setTarget(LinkParent $target): self + { + $this->set('targetId', $target->getId()); + $this->set('targetType', $target->getEntityType()); + + return $this; + } }