refactoring

This commit is contained in:
Yuri Kuznetsov
2020-06-30 10:11:26 +03:00
parent 9c6bec3761
commit 10793ebdda
8 changed files with 50 additions and 62 deletions

View File

@@ -458,6 +458,5 @@ class Application
$user = $this->container->get('entityManager')->getEntity('User', 'system');
$user->set('type', 'system');
$this->container->set('user', $user);
$this->container->get('entityManager')->setUser($user);
}
}

View File

@@ -73,10 +73,19 @@ class ApplicationState
}
/**
* Get current logged user. If no auth is applied, then system used will be returned.
* Whether any user is initialized. If not logged, it will also return TRUE, meaning the system used is used.
*/
public function hasUser() : bool
{
return $this->container->has('user');
}
/**
* Get current logged user. If no auth is applied, then system user will be returned.
*/
public function getUser() : UserEntity
{
if (!$this->hasUser()) throw new Error("User is not available yet.");
return $this->container->get('user');
}

View File

@@ -41,41 +41,19 @@ use Espo\ORM\EntityManager as BaseEntityManager;
class EntityManager extends BaseEntityManager
{
private $hookManager;
private $helper;
protected $user = null;
public function __construct(
array $params,
RepositoryFactory $repositoryFactory,
EntityFactory $entityFactory,
HookManager $hookManager,
Helper $helper
) {
parent::__construct($params, $repositoryFactory, $entityFactory);
$this->hookManager = $hookManager;
$this->helper = $helper;
}
// TODO Check whether setUser is needed here
public function setUser(User $user)
{
$this->user = $user;
}
public function getUser() : ?User
{
return $this->user ?? null;
}
public function getHookManager() : HookManager
{
return $this->hookManager;
}
public function getHelper() : ?Helper
{
return $this->helper ?? null;

View File

@@ -42,6 +42,8 @@ use Espo\Core\ORM\{
use Espo\Core\{
Utils\Metadata,
Utils\Util,
HookManager,
ApplicationState,
};
class Database extends RDB
@@ -55,18 +57,22 @@ class Database extends RDB
private $restoreData = null;
protected $metadata;
protected $config;
protected $fieldManagerUtil;
protected $hookManager;
protected $applicationState;
public function __construct(
string $entityType,
EntityManager $entityManager,
EntityFactory $entityFactory,
Metadata $metadata
Metadata $metadata,
HookManager $hookManager,
ApplicationState $applicationState
) {
parent::__construct($entityType, $entityManager, $entityFactory, $metadata);
$this->metadata = $metadata;
$this->hookManager = $hookManager;
$this->applicationState = $applicationState;
parent::__construct($entityType, $entityManager, $entityFactory, $metadata);
}
protected function getMetadata()
@@ -74,16 +80,6 @@ class Database extends RDB
return $this->metadata;
}
protected function getConfig()
{
return $this->config;
}
protected function getFieldManagerUtil()
{
return $this->fieldManagerUtil;
}
public function handleSelectParams(&$params)
{
}
@@ -140,7 +136,7 @@ class Database extends RDB
{
parent::beforeRemove($entity, $options);
if (!$this->hooksDisabled && empty($options['skipHooks'])) {
$this->getEntityManager()->getHookManager()->process($this->entityType, 'beforeRemove', $entity, $options);
$this->hookManager->process($this->entityType, 'beforeRemove', $entity, $options);
}
$nowString = date('Y-m-d H:i:s', time());
@@ -148,8 +144,8 @@ class Database extends RDB
$entity->set('modifiedAt', $nowString);
}
if ($entity->hasAttribute('modifiedById')) {
if ($this->getEntityManager()->getUser()) {
$entity->set('modifiedById', $this->getEntityManager()->getUser()->id);
if ($this->applicationState->hasUser()) {
$entity->set('modifiedById', $this->applicationState->getUser()->id);
}
}
}
@@ -163,7 +159,7 @@ class Database extends RDB
}
if (!$this->hooksDisabled && empty($options['skipHooks'])) {
$this->getEntityManager()->getHookManager()->process($this->entityType, 'afterRemove', $entity, $options);
$this->hookManager->process($this->entityType, 'afterRemove', $entity, $options);
}
}
@@ -174,7 +170,7 @@ class Database extends RDB
'relationName' => $relationName,
'relationParams' => $params,
];
$this->getEntityManager()->getHookManager()->process(
$this->hookManager->process(
$this->entityType, 'afterMassRelate', $entity, $options, $hookData
);
}
@@ -208,7 +204,7 @@ class Database extends RDB
'foreignEntity' => $foreign,
'foreignId' => $foreign->id,
];
$this->getEntityManager()->getHookManager()->process(
$this->hookManager->process(
$this->entityType, 'afterRelate', $entity, $options, $hookData
);
}
@@ -236,7 +232,7 @@ class Database extends RDB
'foreignEntity' => $foreign,
'foreignId' => $foreign->id,
];
$this->getEntityManager()->getHookManager()->process(
$this->hookManager->process(
$this->entityType, 'afterUnrelate', $entity, $options, $hookData
);
}
@@ -248,7 +244,7 @@ class Database extends RDB
parent::beforeSave($entity, $options);
if (!$this->hooksDisabled && empty($options['skipHooks'])) {
$this->getEntityManager()->getHookManager()->process($this->entityType, 'beforeSave', $entity, $options);
$this->hookManager->process($this->entityType, 'beforeSave', $entity, $options);
}
}
@@ -270,7 +266,7 @@ class Database extends RDB
}
if (!$this->hooksDisabled && empty($options['skipHooks'])) {
$this->getEntityManager()->getHookManager()->process($this->entityType, 'afterSave', $entity, $options);
$this->hookManager->process($this->entityType, 'afterSave', $entity, $options);
}
}
@@ -299,8 +295,8 @@ class Database extends RDB
if (!empty($options['createdById'])) {
$entity->set('createdById', $options['createdById']);
} else if (empty($options['skipCreatedBy']) && (empty($options['import']) || !$entity->has('createdById'))) {
if ($this->getEntityManager()->getUser()) {
$entity->set('createdById', $this->getEntityManager()->getUser()->id);
if ($this->applicationState->hasUser()) {
$entity->set('createdById', $this->applicationState->getUser()->id);
}
}
}
@@ -312,9 +308,9 @@ class Database extends RDB
if ($entity->hasAttribute('modifiedById')) {
if (!empty($options['modifiedById'])) {
$entity->set('modifiedById', $options['modifiedById']);
} else if ($this->getEntityManager()->getUser()) {
$entity->set('modifiedById', $this->getEntityManager()->getUser()->id);
$entity->set('modifiedByName', $this->getEntityManager()->getUser()->get('name'));
} else if ($this->applicationState->hasUser()) {
$entity->set('modifiedById', $this->applicationState->getUser()->id);
$entity->set('modifiedByName', $this->applicationState->getUser()->get('name'));
}
}
}

View File

@@ -145,7 +145,6 @@ class Auth
$user->set('ipAddress', $_SERVER['REMOTE_ADDR']);
$entityManager->setUser($user);
$this->getContainer()->set('user', $user);
}
@@ -280,7 +279,6 @@ class Auth
$user->set('ipAddress', $_SERVER['REMOTE_ADDR']);
$this->getEntityManager()->setUser($user);
$this->getContainer()->set('user', $user);
$secondStepRequired = false;

View File

@@ -37,7 +37,6 @@ use Espo\Entities\AuthToken;
use Espo\Core\Container;
class LDAP extends Espo
{
private $utils;
@@ -109,7 +108,6 @@ class LDAP extends Espo
}
$this->getContainer()->set('user', $systemUser);
$this->getEntityManager()->setUser($systemUser);
}
protected function getLdapClient()

View File

@@ -32,8 +32,13 @@ namespace Espo\Modules\Crm\Repositories;
use Espo\ORM\Entity;
use Espo\Core\Utils\Util;
class Meeting extends \Espo\Core\Repositories\Event
use Espo\Core\Di;
class Meeting extends \Espo\Core\Repositories\Event implements Di\ConfigAware, Di\UserAware
{
use Di\ConfigSetter;
use Di\UserSetter;
protected function beforeSave(Entity $entity, array $options = [])
{
if (!$entity->isNew() && $entity->isAttributeChanged('parentId')) {
@@ -97,7 +102,7 @@ class Meeting extends \Espo\Core\Repositories\Event
parent::beforeSave($entity, $options);
if (!$this->getConfig()->get('eventAssignedUserIsAttendeeDisabled')) {
if (!$this->config->get('eventAssignedUserIsAttendeeDisabled')) {
if ($entity->hasLinkMultipleField('assignedUsers')) {
$assignedUserIdList = $entity->getLinkMultipleIdList('assignedUsers');
foreach ($assignedUserIdList as $assignedUserId) {
@@ -116,7 +121,7 @@ class Meeting extends \Espo\Core\Repositories\Event
}
if ($entity->isNew()) {
$currentUserId = $this->getEntityManager()->getUser()->id;
$currentUserId = $this->user->id;
if (
$entity->hasLinkMultipleId('users', $currentUserId)
&&

View File

@@ -36,7 +36,12 @@ use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\BadRequest;
class Meeting extends \Espo\Services\Record {
use Espo\Core\Di;
class Meeting extends \Espo\Services\Record implements
Di\HookManagerAware
{
use Di\HookManagerSetter;
protected $validateRequiredSkipFieldList = [
'dateEnd'
@@ -219,7 +224,7 @@ class Meeting extends \Espo\Services\Record {
'inviteeId' => $userId,
];
$this->getEntityManager()->getHookManager()->process($this->entityType, 'afterConfirmation', $entity, [], $actionData);
$this->hookManager->process($this->entityType, 'afterConfirmation', $entity, [], $actionData);
return true;
}