config refactoring

This commit is contained in:
yuri
2018-12-17 13:47:01 +02:00
parent 16e96f6454
commit 5ceb57a2cf
7 changed files with 244 additions and 166 deletions
+4 -49
View File
@@ -35,23 +35,12 @@ use \Espo\Core\Exceptions\BadRequest;
class Settings extends \Espo\Core\Controllers\Base
{
protected function getConfigData()
{
if ($this->getUser()->id == 'system') {
$data = $this->getConfig()->getData();
} else {
$data = $this->getConfig()->getData($this->getUser()->isAdmin());
}
$data = $this->getServiceFactory()->create('Settings')->getConfigData();
$fieldDefs = $this->getMetadata()->get('entityDefs.Settings.fields');
foreach ($fieldDefs as $field => $d) {
if ($d['type'] === 'password') {
unset($data[$field]);
}
}
$data['jsLibs'] = $this->getMetadata()->get('app.jsLibs');
$data->jsLibs = $this->getMetadata()->get('app.jsLibs');
return $data;
}
@@ -76,41 +65,7 @@ class Settings extends \Espo\Core\Controllers\Base
throw new BadRequest();
}
$ignoreItemList = [];
$systemOnlyItemList = $this->getConfig()->getSystemOnlyItemList();
foreach ($systemOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
if ($this->getConfig()->get('restrictedMode') && !$this->getUser()->isSuperAdmin()) {
$superAdminOnlyItemList = $this->getConfig()->getSuperAdminOnlyItemList();
foreach ($superAdminOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
}
foreach ($ignoreItemList as $item) {
unset($data->$item);
}
if (
(isset($data->useCache) && $data->useCache !== $this->getConfig()->get('useCache'))
||
(isset($data->aclStrictMode) && $data->aclStrictMode !== $this->getConfig()->get('aclStrictMode'))
) {
$this->getContainer()->get('dataManager')->clearCache();
}
$this->getConfig()->setData($data, $this->getUser()->isAdmin());
$result = $this->getConfig()->save();
if ($result === false) {
throw new Error('Cannot save settings');
}
if (isset($data->defaultCurrency) || isset($data->baseCurrency) || isset($data->currencyRates)) {
$this->getContainer()->get('dataManager')->rebuildDatabase([]);
}
$this->getServiceFactory()->create('Settings')->setConfigData($data);
return $this->getConfigData();
}
+6 -1
View File
@@ -37,6 +37,7 @@ abstract class Base implements Injectable
'config',
'entityManager',
'user',
'serviceFactory'
);
protected $injections = array();
@@ -95,5 +96,9 @@ abstract class Base implements Injectable
{
return $this->getInjection('user');
}
}
protected function getServiceFactory()
{
return $this->getInjection('serviceFactory');
}
}
+14 -95
View File
@@ -31,12 +31,6 @@ namespace Espo\Core\Utils;
class Config
{
/**
* Path of default config file
*
* @access private
* @var string
*/
private $defaultConfigPath = 'application/Espo/Core/defaults/config.php';
private $systemConfigPath = 'application/Espo/Core/defaults/systemConfig.php';
@@ -45,13 +39,7 @@ class Config
private $cacheTimestamp = 'cacheTimestamp';
/**
* Array of admin items
*
* @access protected
* @var array
*/
protected $adminItems = array();
protected $adminItems = [];
protected $associativeArrayAttributeList = [
'currencyRates',
@@ -61,21 +49,16 @@ class Config
];
/**
* Contains content of config
*
* @access private
* @var array
*/
private $data;
private $changedData = array();
private $removeData = array();
private $changedData = [];
private $removeData = [];
private $fileManager;
public function __construct(\Espo\Core\Utils\File\Manager $fileManager) //TODO
public function __construct(\Espo\Core\Utils\File\Manager $fileManager)
{
$this->fileManager = $fileManager;
}
@@ -228,11 +211,6 @@ class Config
return $this->getFileManager()->getPhpContents($this->defaultConfigPath);
}
/**
* Return an Object of all configs
* @param boolean $reload
* @return array()
*/
protected function loadConfig($reload = false)
{
if (!$reload && isset($this->data) && !empty($this->data)) {
@@ -249,50 +227,25 @@ class Config
return $this->data;
}
public function getAllData()
{
return (object) $this->loadConfig();
}
/**
* Get config acording to restrictions for a user
*
* @param $isAdmin
* @return array
*/
public function getData($isAdmin = null)
{
$data = $this->loadConfig();
$restrictedConfig = $data;
foreach ($this->getRestrictedItemList($isAdmin) as $name) {
if (isset($restrictedConfig[$name])) {
unset($restrictedConfig[$name]);
}
}
return $restrictedConfig;
return $data;
}
/**
* Set JSON data acording to restrictions for a user
*
* @param $isAdmin
* @return bool
*/
public function setData($data, $isAdmin = null)
public function setData($data)
{
$restrictItems = $this->getRestrictedItemList($isAdmin);
if (is_object($data)) {
$data = get_object_vars($data);
}
$values = array();
foreach ($data as $key => $item) {
if (!in_array($key, $restrictItems)) {
$values[$key] = $item;
}
}
return $this->set($values);
return $this->set($data);
}
/**
@@ -314,28 +267,6 @@ class Config
return $this->set($timestamp);
}
/**
* @return object
*/
protected function getRestrictedItemList($onlySystemItems = null)
{
$data = $this->loadConfig();
if ($onlySystemItems) {
return $data['systemItems'];
}
if (empty($this->adminItems)) {
$this->adminItems = array_merge($data['systemItems'], $data['adminItems']);
}
if ($onlySystemItems === false) {
return $this->adminItems;
}
return array_merge($this->adminItems, $data['userItems']);
}
public function getAdminOnlyItemList()
{
return $this->get('adminItems', []);
@@ -351,21 +282,9 @@ class Config
return $this->get('systemItems', []);
}
/**
* Check if an item is allowed to get and save
*
* @param $name
* @param $isAdmin
* @return bool
*/
protected function isAllowed($name, $isAdmin = false)
public function getUserOnlyItemList()
{
if (in_array($name, $this->getRestrictedItemList($isAdmin))) {
return false;
}
return true;
return $this->get('userItems', []);
}
public function getSiteUrl()
@@ -200,7 +200,6 @@ return [
'outboundEmailFromName',
'outboundEmailBccAddress',
'integrations',
'googleMapsApiKey'
],
'isInstalled' => false,
'ldapUserNameAttribute' => 'sAMAccountName',
@@ -447,7 +447,8 @@
"view": "views/settings/fields/history-entity-list"
},
"googleMapsApiKey": {
"type": "varchar"
"type": "varchar",
"onlyUser": true
},
"massEmailDisableMandatoryOptOutLink": {
"type": "bool"
+3 -19
View File
@@ -72,6 +72,8 @@ class App extends \Espo\Core\Services\Base
$preferencesData = $this->getPreferences()->getValueMap();
unset($preferencesData->smtpPassword);
$settingsService = $this->getServiceFactory()->create('Settings');
$user = $this->getUser();
if (!$user->has('teamsIds')) {
$user->loadLinkMultipleField('teams');
@@ -85,25 +87,7 @@ class App extends \Espo\Core\Services\Base
$userData->emailAddressList = $this->getEmailAddressList();
$settings = (object)[];
foreach ($this->getConfig()->get('userItems') as $item) {
$settings->$item = $this->getConfig()->get($item);
}
if ($this->getUser()->isAdmin()) {
foreach ($this->getConfig()->get('adminItems') as $item) {
if ($this->getConfig()->has($item)) {
$settings->$item = $this->getConfig()->get($item);
}
}
}
$settingsFieldDefs = $this->getInjection('metadata')->get('entityDefs.Settings.fields', []);
foreach ($settingsFieldDefs as $field => $d) {
if ($d['type'] === 'password') {
unset($settings->$field);
}
}
$settings = $this->getServiceFactory()->create('Settings')->getConfigData();
unset($userData->authTokenId);
unset($userData->password);
+215
View File
@@ -0,0 +1,215 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://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\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use Espo\ORM\Entity;
class Settings extends \Espo\Core\Services\Base
{
protected function init()
{
parent::init();
$this->addDependency('fieldManagerUtil');
$this->addDependency('metadata');
$this->addDependency('acl');
$this->addDependency('container');
}
protected function getFieldManagerUtil()
{
return $this->getInjection('fieldManagerUtil');
}
protected function getMetadata()
{
return $this->getInjection('metadata');
}
protected function getAcl()
{
return $this->getInjection('acl');
}
protected function getContainer()
{
return $this->getInjection('container');
}
public function getConfigData()
{
$data = $this->getConfig()->getAllData();
$ignoreItemList = [];
$systemOnlyItemList = $this->getSystemOnlyItemList();
foreach ($systemOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
if (!$this->getUser()->isAdmin() || $this->getUser()->isSystem()) {
$adminOnlyItemList = $this->getAdminOnlyItemList();
foreach ($adminOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
}
if ($this->getUser()->isSystem()) {
$userOnlyItemList = $this->getUserOnlyItemList();
foreach ($userOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
}
foreach ($ignoreItemList as $item) {
unset($data->$item);
}
$fieldDefs = $this->getMetadata()->get(['entityDefs', 'Settings', 'fields']);
foreach ($fieldDefs as $field => $fieldParams) {
if ($fieldParams['type'] === 'password') {
unset($data->$field);
}
}
$this->filterData($data);
return $data;
}
public function setConfigData($data)
{
if (!$this->getUser()->isAdmin()) {
throw new Forbidden();
}
$ignoreItemList = [];
$systemOnlyItemList = $this->getSystemOnlyItemList();
foreach ($systemOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
if ($this->getConfig()->get('restrictedMode') && !$this->getUser()->isSuperAdmin()) {
$superAdminOnlyItemList = $this->getConfig()->getSuperAdminOnlyItemList();
foreach ($superAdminOnlyItemList as $item) {
$ignoreItemList[] = $item;
}
}
foreach ($ignoreItemList as $item) {
unset($data->$item);
}
if (
(isset($data->useCache) && $data->useCache !== $this->getConfig()->get('useCache'))
||
(isset($data->aclStrictMode) && $data->aclStrictMode !== $this->getConfig()->get('aclStrictMode'))
) {
$this->getContainer()->get('dataManager')->clearCache();
}
$this->getConfig()->setData($data);
$result = $this->getConfig()->save();
if ($result === false) {
throw new Error('Cannot save settings');
}
if (isset($data->defaultCurrency) || isset($data->baseCurrency) || isset($data->currencyRates)) {
$this->getContainer()->get('dataManager')->rebuildDatabase([]);
}
return $result;
}
protected function filterData($data)
{
if ($this->getUser()->isSystem()) return;
if (!$this->getAcl()->checkScope('Email', 'create')) {
unset($data->outboundEmailFromAddress);
unset($data->outboundEmailFromName);
unset($data->outboundEmailBccAddress);
}
}
public function getAdminOnlyItemList()
{
$itemList = $this->getConfig()->getAdminOnlyItemList();
$fieldDefs = $this->getMetadata()->get(['entityDefs', 'Settings', 'fields']);
foreach ($fieldDefs as $field => $fieldParams) {
if (!empty($fieldParams['onlyAdmin'])) {
foreach ($this->getFieldManagerUtil()->getAttributeList('Settings', $field) as $attribute) {
$itemList[] = $attribute;
}
}
}
return $itemList;
}
public function getUserOnlyItemList()
{
$itemList = $this->getConfig()->getUserOnlyItemList();
$fieldDefs = $this->getMetadata()->get(['entityDefs', 'Settings', 'fields']);
foreach ($fieldDefs as $field => $fieldParams) {
if (!empty($fieldParams['onlyUser'])) {
foreach ($this->getFieldManagerUtil()->getAttributeList('Settings', $field) as $attribute) {
$itemList[] = $attribute;
}
}
}
return $itemList;
}
public function getSystemOnlyItemList()
{
$itemList = $this->getConfig()->getSystemOnlyItemList();
$fieldDefs = $this->getMetadata()->get(['entityDefs', 'Settings', 'fields']);
foreach ($fieldDefs as $field => $fieldParams) {
if (!empty($fieldParams['onlySystem'])) {
foreach ($this->getFieldManagerUtil()->getAttributeList('Settings', $field) as $attribute) {
$itemList[] = $attribute;
}
}
}
return $itemList;
}
}