diff --git a/application/Espo/Classes/FieldProcessing/Note/AttachmentsLoader.php b/application/Espo/Classes/FieldProcessing/Note/AttachmentsLoader.php new file mode 100644 index 0000000000..7eea6c2bcf --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/Note/AttachmentsLoader.php @@ -0,0 +1,47 @@ +loadAttachments(); + } +} diff --git a/application/Espo/Classes/FieldProcessing/User/LastAccessLoader.php b/application/Espo/Classes/FieldProcessing/User/LastAccessLoader.php new file mode 100644 index 0000000000..76ab2146a9 --- /dev/null +++ b/application/Espo/Classes/FieldProcessing/User/LastAccessLoader.php @@ -0,0 +1,111 @@ +entityManager = $entityManager; + $this->acl = $acl; + } + + public function process(Entity $entity, LoaderParams $params): void + { + $forbiddenFieldList = $this->acl + ->getScopeForbiddenFieldList($entity->getEntityType(), Table::ACTION_READ); + + if (in_array('lastAccess', $forbiddenFieldList)) { + return; + } + + $authToken = $this->entityManager + ->getRDBRepository('AuthToken') + ->select(['id', 'lastAccess']) + ->where([ + 'userId' => $entity->getId(), + ]) + ->order('lastAccess', 'DESC') + ->findOne(); + + $lastAccess = null; + + if ($authToken) { + $lastAccess = $authToken->get('lastAccess'); + } + + $dt = null; + + if ($lastAccess) { + try { + $dt = new DateTime($lastAccess); + } + catch (Exception $e) {} + } + + $where = [ + 'userId' => $entity->getId(), + 'isDenied' => false, + ]; + + if ($dt) { + $where['requestTime>'] = $dt->format('U'); + } + + $authLogRecord = $this->entityManager + ->getRDBRepository('AuthLogRecord') + ->select(['id', 'createdAt']) + ->where($where) + ->order('requestTime', true) + ->findOne(); + + if ($authLogRecord) { + $lastAccess = $authLogRecord->get('createdAt'); + } + + $entity->set('lastAccess', $lastAccess); + } +} diff --git a/application/Espo/Entities/Note.php b/application/Espo/Entities/Note.php index 92ad65110b..259fcd5eb0 100644 --- a/application/Espo/Entities/Note.php +++ b/application/Espo/Entities/Note.php @@ -87,35 +87,35 @@ class Note extends Entity return (bool) $this->aclIsProcessed; } - public function loadAttachments() + public function loadAttachments(): void { $data = $this->get('data'); - if (!empty($data) && !empty($data->attachmentsIds) && is_array($data->attachmentsIds)) { - $attachmentsIds = $data->attachmentsIds; - - $collection = $this->entityManager - ->getRepository('Attachment') - ->select(['id', 'name', 'type']) - ->order('createdAt') - ->where([ - 'id' => $attachmentsIds - ]) - ->find(); - } - else { + if (empty($data) || empty($data->attachmentsIds) || !is_array($data->attachmentsIds)) { $this->loadLinkMultipleField('attachments'); return; } + $attachmentsIds = $data->attachmentsIds; + + $collection = $this->entityManager + ->getRepository('Attachment') + ->select(['id', 'name', 'type']) + ->order('createdAt') + ->where([ + 'id' => $attachmentsIds + ]) + ->find(); + $ids = []; $names = (object) []; $types = (object) []; foreach ($collection as $e) { - $id = $e->id; + $id = $e->getId(); + $ids[] = $id; $names->$id = $e->get('name'); diff --git a/application/Espo/Resources/metadata/recordDefs/Note.json b/application/Espo/Resources/metadata/recordDefs/Note.json new file mode 100644 index 0000000000..889c8cd8c9 --- /dev/null +++ b/application/Espo/Resources/metadata/recordDefs/Note.json @@ -0,0 +1,5 @@ +{ + "readLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\Note\\AttachmentsLoader" + ] +} diff --git a/application/Espo/Resources/metadata/recordDefs/User.json b/application/Espo/Resources/metadata/recordDefs/User.json index 89e3222dca..4570185ff5 100644 --- a/application/Espo/Resources/metadata/recordDefs/User.json +++ b/application/Espo/Resources/metadata/recordDefs/User.json @@ -6,5 +6,8 @@ "delete": { "implementationClassName": "Espo\\Classes\\MassAction\\User\\MassDelete" } - } + }, + "readLoaderClassNameList": [ + "Espo\\Classes\\FieldProcessing\\User\\LastAccessLoader" + ] } \ No newline at end of file diff --git a/application/Espo/Services/Note.php b/application/Espo/Services/Note.php index 325eab073b..4faa8940ac 100644 --- a/application/Espo/Services/Note.php +++ b/application/Espo/Services/Note.php @@ -44,13 +44,6 @@ use StdClass; class Note extends Record { - public function loadAdditionalFields(Entity $entity) - { - parent::loadAdditionalFields($entity); - - $entity->loadAttachments(); - } - protected function afterCreateEntity(Entity $entity, $data) { parent::afterCreateEntity($entity, $data); diff --git a/application/Espo/Services/User.php b/application/Espo/Services/User.php index 998d66ee5f..79b40cfee2 100644 --- a/application/Espo/Services/User.php +++ b/application/Espo/Services/User.php @@ -783,66 +783,4 @@ class User extends Record implements $this->dataManager->updateCacheTimestamp(); } - - public function loadAdditionalFields(Entity $entity) - { - parent::loadAdditionalFields($entity); - - $this->loadLastAccessField($entity); - } - - public function loadLastAccessField(Entity $entity) - { - $forbiddenFieldList = $this->getAcl()->getScopeForbiddenFieldList($this->entityType, 'edit'); - - if (in_array('lastAccess', $forbiddenFieldList)) { - return; - } - - $authToken = $this->getEntityManager() - ->getRepository('AuthToken') - ->select(['id', 'lastAccess']) - ->where([ - 'userId' => $entity->id - ]) - ->order('lastAccess', 'DESC') - ->findOne(); - - $lastAccess = null; - - if ($authToken) { - $lastAccess = $authToken->get('lastAccess'); - } - - $dt = null; - - if ($lastAccess) { - try { - $dt = new DateTime($lastAccess); - } - catch (Exception $e) {} - } - - $where = [ - 'userId' => $entity->id, - 'isDenied' => false, - ]; - - if ($dt) { - $where['requestTime>'] = $dt->format('U'); - } - - $authLogRecord = $this->getEntityManager() - ->getRepository('AuthLogRecord') - ->select(['id', 'createdAt']) - ->where($where) - ->order('requestTime', true) - ->findOne(); - - if ($authLogRecord) { - $lastAccess = $authLogRecord->get('createdAt'); - } - - $entity->set('lastAccess', $lastAccess); - } }