From 10793ebdda471663aa45e2d33ac9a406b4efb3b5 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 30 Jun 2020 10:11:26 +0300 Subject: [PATCH] refactoring --- application/Espo/Core/Application.php | 1 - application/Espo/Core/ApplicationState.php | 11 +++- application/Espo/Core/ORM/EntityManager.php | 22 -------- .../Espo/Core/Repositories/Database.php | 54 +++++++++---------- application/Espo/Core/Utils/Auth.php | 2 - .../Espo/Core/Utils/Authentication/LDAP.php | 2 - .../Espo/Modules/Crm/Repositories/Meeting.php | 11 ++-- .../Espo/Modules/Crm/Services/Meeting.php | 9 +++- 8 files changed, 50 insertions(+), 62 deletions(-) diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 3f086ebb38..86bf64402d 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -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); } } diff --git a/application/Espo/Core/ApplicationState.php b/application/Espo/Core/ApplicationState.php index 48614e9680..a13a83006b 100644 --- a/application/Espo/Core/ApplicationState.php +++ b/application/Espo/Core/ApplicationState.php @@ -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'); } diff --git a/application/Espo/Core/ORM/EntityManager.php b/application/Espo/Core/ORM/EntityManager.php index 09009d9e18..9dcd538d1f 100644 --- a/application/Espo/Core/ORM/EntityManager.php +++ b/application/Espo/Core/ORM/EntityManager.php @@ -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; diff --git a/application/Espo/Core/Repositories/Database.php b/application/Espo/Core/Repositories/Database.php index ed27d8a9e7..585e0ea2ee 100644 --- a/application/Espo/Core/Repositories/Database.php +++ b/application/Espo/Core/Repositories/Database.php @@ -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')); } } } diff --git a/application/Espo/Core/Utils/Auth.php b/application/Espo/Core/Utils/Auth.php index 70423a3237..5101c19e3c 100644 --- a/application/Espo/Core/Utils/Auth.php +++ b/application/Espo/Core/Utils/Auth.php @@ -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; diff --git a/application/Espo/Core/Utils/Authentication/LDAP.php b/application/Espo/Core/Utils/Authentication/LDAP.php index 05ebbdc7ec..fb1c58979c 100644 --- a/application/Espo/Core/Utils/Authentication/LDAP.php +++ b/application/Espo/Core/Utils/Authentication/LDAP.php @@ -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() diff --git a/application/Espo/Modules/Crm/Repositories/Meeting.php b/application/Espo/Modules/Crm/Repositories/Meeting.php index 71eccd568b..46ea301f5b 100644 --- a/application/Espo/Modules/Crm/Repositories/Meeting.php +++ b/application/Espo/Modules/Crm/Repositories/Meeting.php @@ -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) && diff --git a/application/Espo/Modules/Crm/Services/Meeting.php b/application/Espo/Modules/Crm/Services/Meeting.php index 5c3dddf8c9..f10ed2db1e 100644 --- a/application/Espo/Modules/Crm/Services/Meeting.php +++ b/application/Espo/Modules/Crm/Services/Meeting.php @@ -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; }