From 5ceb57a2cfeaf4f297234cd00ee4bc0a48031b60 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 17 Dec 2018 13:47:01 +0200 Subject: [PATCH] config refactoring --- application/Espo/Controllers/Settings.php | 53 +---- application/Espo/Core/Services/Base.php | 7 +- application/Espo/Core/Utils/Config.php | 109 ++------- .../Espo/Core/defaults/systemConfig.php | 1 - .../metadata/entityDefs/Settings.json | 3 +- application/Espo/Services/App.php | 22 +- application/Espo/Services/Settings.php | 215 ++++++++++++++++++ 7 files changed, 244 insertions(+), 166 deletions(-) create mode 100644 application/Espo/Services/Settings.php diff --git a/application/Espo/Controllers/Settings.php b/application/Espo/Controllers/Settings.php index d11182bc26..568d524686 100644 --- a/application/Espo/Controllers/Settings.php +++ b/application/Espo/Controllers/Settings.php @@ -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(); } diff --git a/application/Espo/Core/Services/Base.php b/application/Espo/Core/Services/Base.php index 1d28b61415..cca6960676 100644 --- a/application/Espo/Core/Services/Base.php +++ b/application/Espo/Core/Services/Base.php @@ -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'); + } +} diff --git a/application/Espo/Core/Utils/Config.php b/application/Espo/Core/Utils/Config.php index 65a5faed48..a3a9447c21 100644 --- a/application/Espo/Core/Utils/Config.php +++ b/application/Espo/Core/Utils/Config.php @@ -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() diff --git a/application/Espo/Core/defaults/systemConfig.php b/application/Espo/Core/defaults/systemConfig.php index 40011108c5..e235fceaa2 100644 --- a/application/Espo/Core/defaults/systemConfig.php +++ b/application/Espo/Core/defaults/systemConfig.php @@ -200,7 +200,6 @@ return [ 'outboundEmailFromName', 'outboundEmailBccAddress', 'integrations', - 'googleMapsApiKey' ], 'isInstalled' => false, 'ldapUserNameAttribute' => 'sAMAccountName', diff --git a/application/Espo/Resources/metadata/entityDefs/Settings.json b/application/Espo/Resources/metadata/entityDefs/Settings.json index 864b609022..19030128c6 100644 --- a/application/Espo/Resources/metadata/entityDefs/Settings.json +++ b/application/Espo/Resources/metadata/entityDefs/Settings.json @@ -447,7 +447,8 @@ "view": "views/settings/fields/history-entity-list" }, "googleMapsApiKey": { - "type": "varchar" + "type": "varchar", + "onlyUser": true }, "massEmailDisableMandatoryOptOutLink": { "type": "bool" diff --git a/application/Espo/Services/App.php b/application/Espo/Services/App.php index c44433683b..1c34ae0e9e 100644 --- a/application/Espo/Services/App.php +++ b/application/Espo/Services/App.php @@ -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); diff --git a/application/Espo/Services/Settings.php b/application/Espo/Services/Settings.php new file mode 100644 index 0000000000..886e63da2e --- /dev/null +++ b/application/Espo/Services/Settings.php @@ -0,0 +1,215 @@ +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; + } +}