field loading usage

This commit is contained in:
Yuri Kuznetsov
2021-04-28 10:27:46 +03:00
parent 9d7582caa6
commit f9a4e3c9fc
7 changed files with 182 additions and 85 deletions
@@ -0,0 +1,47 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldProcessing\Note;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
};
use Espo\Entities\Note;
class AttachmentsLoader implements Loader
{
public function process(Entity $entity, LoaderParams $params): void
{
/* @var $entity Note */
$entity->loadAttachments();
}
}
@@ -0,0 +1,111 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Classes\FieldProcessing\User;
use Espo\Core\{
FieldProcessing\Loader,
FieldProcessing\LoaderParams,
ORM\Entity,
ORM\EntityManager,
Acl,
Acl\Table,
};
use DateTime;
use Exception;
class LastAccessLoader implements Loader
{
private $entityManager;
private $acl;
public function __construct(EntityManager $entityManager, Acl $acl)
{
$this->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);
}
}
+15 -15
View File
@@ -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');
@@ -0,0 +1,5 @@
{
"readLoaderClassNameList": [
"Espo\\Classes\\FieldProcessing\\Note\\AttachmentsLoader"
]
}
@@ -6,5 +6,8 @@
"delete": {
"implementationClassName": "Espo\\Classes\\MassAction\\User\\MassDelete"
}
}
},
"readLoaderClassNameList": [
"Espo\\Classes\\FieldProcessing\\User\\LastAccessLoader"
]
}
-7
View File
@@ -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);
-62
View File
@@ -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);
}
}