This commit is contained in:
Yuri Kuznetsov
2023-04-28 15:57:25 +03:00
parent a9d73a8501
commit d460805c8b
3 changed files with 79 additions and 19 deletions
@@ -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());
}
}
+23 -19
View File
@@ -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);
}
@@ -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;
}
}