This commit is contained in:
Yuri Kuznetsov
2022-10-16 12:01:04 +03:00
parent 838d0b08db
commit 6a940eb732
4 changed files with 136 additions and 213 deletions

View File

@@ -30,26 +30,33 @@
namespace Espo\Controllers;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Authentication\Authentication;
use Espo\Core\Di;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\InjectableFactory;
use Espo\Services\App as Service;
use Espo\Tools\App\AppService as Service;
use stdClass;
class App implements
Di\InjectableFactoryAware
class App
{
use Di\InjectableFactorySetter;
private InjectableFactory $injectableFactory;
public function __construct(
InjectableFactory $injectableFactory
) {
$this->injectableFactory = $injectableFactory;
}
public function getActionUser(): stdClass
{
return (object) $this->getService()->getUserData();
}
/**
* @throws BadRequest
*/
public function postActionDestroyAuthToken(Request $request, Response $response): bool
{
$data = $request->getParsedBody();

View File

@@ -27,49 +27,38 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Services;
namespace Espo\Tools\App;
use Espo\Entities\DashboardTemplate;
use Espo\Entities\EmailAccount as EmailAccountEntity;
use Espo\Entities\InboundEmail as InboundEmailEntity;
use Espo\Entities\Settings;
use Espo\Services\Settings as SettingsService;
use Espo\Repositories\PhoneNumber as PhoneNumberRepository;
use Espo\Repositories\ArrayValue as ArrayValueRepository;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\{
Acl,
Authentication\Logins\Espo,
DataManager,
InjectableFactory,
Utils\Metadata,
Utils\Config,
Utils\Language,
Utils\FieldUtil,
Utils\Log};
use Espo\Core\Acl;
use Espo\Core\Authentication\Logins\Espo;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\FieldUtil;
use Espo\Core\Utils\Language;
use Espo\Core\Utils\Log;
use Espo\Core\Utils\Metadata;
use Espo\Entities\User;
use Espo\Entities\Preferences;
use Espo\Entities\PhoneNumber;
use Espo\Entities\ArrayValue;
use Espo\ORM\{
Collection,
EntityManager,
Repository\RDBRepository,
Entity};
use Espo\ORM\Collection;
use Espo\ORM\EntityManager;
use Espo\Tools\App\AppParam;
use stdClass;
use Throwable;
class App
class AppService
{
private Config $config;
private EntityManager $entityManager;
private Metadata $metadata;
private Acl $acl;
private DataManager $dataManager;
private InjectableFactory $injectableFactory;
private SettingsService $settingsService;
private User $user;
@@ -82,7 +71,6 @@ class App
EntityManager $entityManager,
Metadata $metadata,
Acl $acl,
DataManager $dataManager,
InjectableFactory $injectableFactory,
SettingsService $settingsService,
User $user,
@@ -94,7 +82,6 @@ class App
$this->entityManager = $entityManager;
$this->metadata = $metadata;
$this->acl = $acl;
$this->dataManager = $dataManager;
$this->injectableFactory = $injectableFactory;
$this->settingsService = $settingsService;
$this->user = $user;
@@ -127,7 +114,7 @@ class App
if ($user->get('dashboardTemplateId')) {
$dashboardTemplate = $this->entityManager
->getEntity('DashboardTemplate', $user->get('dashboardTemplateId'));
->getEntity(DashboardTemplate::ENTITY_TYPE, $user->get('dashboardTemplateId'));
if ($dashboardTemplate) {
$settings->forcedDashletsOptions = $dashboardTemplate->get('dashletsOptions') ?? (object) [];
@@ -154,7 +141,7 @@ class App
'passwordChangeForNonAdminDisabled' => $this->config
->get('authenticationMethod', Espo::NAME) !== Espo::NAME,
'timeZoneList' => $this->metadata
->get(['entityDefs', 'Settings', 'fields', 'timeZone', 'options'], []),
->get(['entityDefs', Settings::ENTITY_TYPE, 'fields', 'timeZone', 'options'], []),
'auth2FARequired' => $auth2FARequired,
];
@@ -363,7 +350,7 @@ class App
}
$inboundEmailList = $this->entityManager
->getRDBRepository(InboundEmailEntity::ENTITY_TYPE)
->getRDBRepositoryByClass(InboundEmailEntity::class)
->where([
'status' => InboundEmailEntity::STATUS_ACTIVE,
'useSmtp' => true,
@@ -389,7 +376,7 @@ class App
if ($groupEmailAccountPermission === Acl\Table::LEVEL_ALL) {
$inboundEmailList = $this->entityManager
->getRDBRepository(InboundEmailEntity::ENTITY_TYPE)
->getRDBRepositoryByClass(InboundEmailEntity::class)
->where([
'status' => InboundEmailEntity::STATUS_ACTIVE,
'useSmtp' => true,
@@ -464,187 +451,12 @@ class App
return $value;
}
public function jobClearCache(): void
{
$this->dataManager->clearCache();
}
public function jobRebuild(): void
{
$this->dataManager->rebuild();
}
/**
* @todo Remove in 7.2.
*/
public function jobPopulatePhoneNumberNumeric(): void
{
$numberList = $this->entityManager
->getRDBRepository('PhoneNumber')
->find();
foreach ($numberList as $number) {
$this->entityManager->saveEntity($number);
}
}
/**
* @todo Remove in 7.2. Move to another place. CLI command.
*/
public function jobPopulateArrayValues(): void
{
/** @var string[] $scopeList */
$scopeList = array_keys($this->metadata->get(['scopes']));
$query = $this->entityManager->getQueryBuilder()
->delete()
->from('ArrayValue')
->build();
$this->entityManager
->getQueryExecutor()
->execute($query);
foreach ($scopeList as $scope) {
if (!$this->metadata->get(['scopes', $scope, 'entity'])) {
continue;
}
if ($this->metadata->get(['scopes', $scope, 'disabled'])) {
continue;
}
$attributeList = [];
$entityDefs = $this->entityManager
->getDefs()
->getEntity($scope);
foreach ($entityDefs->getAttributeList() as $attributeDefs) {
$attribute = $attributeDefs->getName();
$type = $attributeDefs->getType();
if ($type !== Entity::JSON_ARRAY) {
continue;
}
if (!$attributeDefs->getParam('storeArrayValues')) {
continue;
}
if (!$attributeDefs->getParam('notStorable')) {
continue;
}
$attributeList[] = $attribute;
}
$select = ['id'];
$orGroup = [];
foreach ($attributeList as $attribute) {
$select[] = $attribute;
$orGroup[$attribute . '!='] = null;
}
$repository = $this->entityManager->getRepository($scope);
if (!$repository instanceof RDBRepository) {
continue;
}
if (!count($attributeList)) {
continue;
}
$query = $this->entityManager
->getQueryBuilder()
->select()
->from($scope)
->select($select)
->where([
'OR' => $orGroup,
])
->build();
$sth = $this->entityManager
->getQueryExecutor()
->execute($query);
while ($dataRow = $sth->fetch()) {
$entity = $this->entityManager->getEntityFactory()->create($scope);
if (!$entity instanceof CoreEntity) {
continue;
}
$entity->set($dataRow);
$entity->setAsFetched();
foreach ($attributeList as $attribute) {
$this->getArrayValueRepository()->storeEntityAttribute($entity, $attribute, true);
}
}
}
}
/**
* @todo Remove in 7.4. Move to another place. CLI command.
*/
public function jobPopulateOptedOutPhoneNumbers(): void
{
$entityTypeList = ['Contact', 'Lead'];
foreach ($entityTypeList as $entityType) {
$entityList = $this->entityManager
->getRDBRepository($entityType)
->where([
'doNotCall' => true,
'phoneNumber!=' => null,
])
->select(['id', 'phoneNumber'])
->find();
foreach ($entityList as $entity) {
$phoneNumber = $entity->get('phoneNumber');
if (!$phoneNumber) {
continue;
}
$phoneNumberEntity = $this->getPhoneNumberRepository()->getByNumber($phoneNumber);
if (!$phoneNumberEntity) {
continue;
}
$phoneNumberEntity->set('optOut', true);
$this->entityManager->saveEntity($phoneNumberEntity);
}
}
}
private function filterPreferencesData(stdClass $data): void
{
$passwordFieldList = $this->fieldUtil->getFieldByTypeList('Preferences', 'password');
$passwordFieldList = $this->fieldUtil->getFieldByTypeList(Preferences::ENTITY_TYPE, 'password');
foreach ($passwordFieldList as $field) {
unset($data->$field);
}
}
private function getPhoneNumberRepository(): PhoneNumberRepository
{
/** @var PhoneNumberRepository */
return $this->entityManager->getRepository(PhoneNumber::ENTITY_TYPE);
}
private function getArrayValueRepository(): ArrayValueRepository
{
/** @var ArrayValueRepository */
return $this->entityManager->getRepository(ArrayValue::ENTITY_TYPE);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 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\Tools\App\Jobs;
use Espo\Core\DataManager;
use Espo\Core\Exceptions\Error;
use Espo\Core\Job\JobDataLess;
class ClearCache implements JobDataLess
{
private DataManager $dataManager;
public function __construct(DataManager $dataManager)
{
$this->dataManager = $dataManager;
}
/**
* @throws Error
*/
public function run(): void
{
$this->dataManager->clearCache();
}
}

View File

@@ -0,0 +1,52 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 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\Tools\App\Jobs;
use Espo\Core\DataManager;
use Espo\Core\Exceptions\Error;
use Espo\Core\Job\JobDataLess;
class Rebuild implements JobDataLess
{
private DataManager $dataManager;
public function __construct(DataManager $dataManager)
{
$this->dataManager = $dataManager;
}
/**
* @throws Error
*/
public function run(): void
{
$this->dataManager->rebuild();
}
}