diff options
author | Mārtiņš Tālbergs <martins.talbergs@zabbix.com> | 2021-03-01 17:32:22 +0300 |
---|---|---|
committer | Mārtiņš Tālbergs <martins.talbergs@zabbix.com> | 2021-03-01 18:00:05 +0300 |
commit | 9eb72bd3fbdbdd1ee161a7b25c4ee0bef07d2668 (patch) | |
tree | d7e36b055e40bd0f72354af3b98965ac03fc3e36 | |
parent | 51979394019909650c7589dbc876318a98560ee6 (diff) |
A.F....... [ZBXNEXT-6288] removed screen API endpoint
-rw-r--r-- | ui/app/controllers/CControllerPopupGeneric.php | 24 | ||||
-rw-r--r-- | ui/include/classes/api/API.php | 14 | ||||
-rw-r--r-- | ui/include/classes/api/CApiServiceFactory.php | 2 | ||||
-rw-r--r-- | ui/include/classes/api/services/CScreen.php | 1374 | ||||
-rw-r--r-- | ui/include/classes/api/services/CScreenItem.php | 933 | ||||
-rw-r--r-- | ui/include/classes/api/services/CUser.php | 15 | ||||
-rw-r--r-- | ui/tests/api_json/testAuditLog.php | 4 |
7 files changed, 3 insertions, 2363 deletions
diff --git a/ui/app/controllers/CControllerPopupGeneric.php b/ui/app/controllers/CControllerPopupGeneric.php index aad1e3c880e..7ad41bde779 100644 --- a/ui/app/controllers/CControllerPopupGeneric.php +++ b/ui/app/controllers/CControllerPopupGeneric.php @@ -282,18 +282,6 @@ class CControllerPopupGeneric extends CController { _('Name') ] ], - 'screens' => [ - 'title' => _('Screens'), - 'min_user_type' => USER_TYPE_ZABBIX_USER, - 'allowed_src_fields' => 'screenid', - 'form' => [ - 'name' => 'screenform', - 'id' => 'screens' - ], - 'table_columns' => [ - _('Name') - ] - ], 'graphs' => [ 'title' => _('Graphs'), 'min_user_type' => USER_TYPE_ZABBIX_USER, @@ -427,7 +415,6 @@ class CControllerPopupGeneric extends CController { 'hostid' => 'db hosts.hostid', 'host' => 'string', 'parent_discoveryid' => 'db items.itemid', - 'screenid' => 'db screens.screenid', 'templates' => 'string|not_empty', 'host_templates' => 'string|not_empty', 'multiselect' => 'in 1', @@ -737,7 +724,7 @@ class CControllerPopupGeneric extends CController { */ protected function getPageOptions(): array { $option_fields_binary = ['noempty', 'real_hosts', 'submit_parent', 'with_items', 'writeonly']; - $option_fields_value = ['host_templates', 'screenid']; + $option_fields_value = ['host_templates']; $page_options = [ 'srcfld1' => $this->getInput('srcfld1', ''), @@ -1256,15 +1243,6 @@ class CControllerPopupGeneric extends CController { CArrayHelper::sort($records, ['name']); break; - case 'screens': - $options += [ - 'output' => ['screenid', 'name'] - ]; - - $records = API::Screen()->get($options); - CArrayHelper::sort($records, ['name']); - break; - case 'drules': $records = API::DRule()->get([ 'output' => ['druleid', 'name'], diff --git a/ui/include/classes/api/API.php b/ui/include/classes/api/API.php index b4262762c2f..5529dcbee3b 100644 --- a/ui/include/classes/api/API.php +++ b/ui/include/classes/api/API.php @@ -347,20 +347,6 @@ class API { } /** - * @return CScreen - */ - public static function Screen() { - return self::getApi('screen'); - } - - /** - * @return CScreenItem - */ - public static function ScreenItem() { - return self::getApi('screenitem'); - } - - /** * @return CScript */ public static function Script() { diff --git a/ui/include/classes/api/CApiServiceFactory.php b/ui/include/classes/api/CApiServiceFactory.php index 96112d7aa25..a17704f92e1 100644 --- a/ui/include/classes/api/CApiServiceFactory.php +++ b/ui/include/classes/api/CApiServiceFactory.php @@ -63,8 +63,6 @@ class CApiServiceFactory extends CRegistryFactory { 'proxy' => CProxy::class, 'role' => CRole::class, 'service' => CService::class, - 'screen' => CScreen::class, - 'screenitem' => CScreenItem::class, 'script' => CScript::class, 'settings' => CSettings::class, 'task' => CTask::class, diff --git a/ui/include/classes/api/services/CScreen.php b/ui/include/classes/api/services/CScreen.php deleted file mode 100644 index 8d9a4628d5f..00000000000 --- a/ui/include/classes/api/services/CScreen.php +++ /dev/null @@ -1,1374 +0,0 @@ -<?php -/* -** Zabbix -** Copyright (C) 2001-2021 Zabbix SIA -** -** This program 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 2 of the License, or -** (at your option) any later version. -** -** This program 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 this program; if not, write to the Free Software -** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -**/ - - -/** - * Class containing methods for operations with screens. - */ -class CScreen extends CApiService { - - public const ACCESS_RULES = [ - 'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER], - 'create' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS], - 'update' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS], - 'delete' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS] - ]; - - protected $tableName = 'screens'; - protected $tableAlias = 's'; - protected $sortColumns = ['screenid', 'name']; - - /** - * Get screen data. - * - * @param array $options - * @param bool $options['editable'] only with read-write permission. Ignored for SuperAdmins - * @param int $options['count'] count Hosts, returned column name is rowscount - * @param string $options['pattern'] search hosts by pattern in host names - * @param int $options['limit'] limit selection - * @param string $options['order'] deprecated parameter (for now) - * - * @return array - */ - public function get($options = []) { - $result = []; - $user_data = self::$userData; - - $sql_parts = [ - 'select' => ['screens' => 's.screenid'], - 'from' => ['screens' => 'screens s'], - 'where' => [], - 'order' => [], - 'group' => [], - 'limit' => null - ]; - - $defOptions = [ - 'screenids' => null, - 'userids' => null, - 'screenitemids' => null, - 'editable' => false, - 'nopermissions' => null, - // filter - 'filter' => null, - 'search' => null, - 'searchByAny' => null, - 'startSearch' => false, - 'excludeSearch' => false, - 'searchWildcardsEnabled' => null, - // output - 'output' => API_OUTPUT_EXTEND, - 'selectScreenItems' => null, - 'selectUsers' => null, - 'selectUserGroups' => null, - 'countOutput' => false, - 'groupCount' => false, - 'preservekeys' => false, - 'sortfield' => '', - 'sortorder' => '', - 'limit' => null - ]; - $options = zbx_array_merge($defOptions, $options); - - if ($options['countOutput']) { - $count_output = true; - $options['output'] = ['screenid']; - $options['countOutput'] = false; - $options['limit'] = null; - } - else { - $count_output = false; - } - - // Editable + permission check. - if ($user_data['type'] != USER_TYPE_SUPER_ADMIN && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN - && !$options['nopermissions']) { - $public_screens = ''; - - if ($options['editable']) { - $permission = PERM_READ_WRITE; - } - else { - $permission = PERM_READ; - $public_screens = ' OR s.private='.PUBLIC_SHARING; - } - - $user_groups = getUserGroupsByUserId($user_data['userid']); - - $sql_parts['where'][] = '(EXISTS ('. - 'SELECT NULL'. - ' FROM screen_user su'. - ' WHERE s.screenid=su.screenid'. - ' AND su.userid='.$user_data['userid']. - ' AND su.permission>='.$permission. - ')'. - ' OR EXISTS ('. - 'SELECT NULL'. - ' FROM screen_usrgrp sg'. - ' WHERE s.screenid=sg.screenid'. - ' AND '.dbConditionInt('sg.usrgrpid', $user_groups). - ' AND sg.permission>='.$permission. - ')'. - ' OR s.userid='.$user_data['userid']. - $public_screens. - ')'; - } - - // screenids - if (!is_null($options['screenids'])) { - zbx_value2array($options['screenids']); - $sql_parts['where'][] = dbConditionInt('s.screenid', $options['screenids']); - } - - // userids - if ($options['userids'] !== null) { - zbx_value2array($options['userids']); - - $sql_parts['where'][] = dbConditionInt('s.userid', $options['userids']); - } - - // screenitemids - if (!is_null($options['screenitemids'])) { - zbx_value2array($options['screenitemids']); - - $sql_parts['from']['screens_items'] = 'screens_items si'; - $sql_parts['where']['ssi'] = 'si.screenid=s.screenid'; - $sql_parts['where'][] = dbConditionInt('si.screenitemid', $options['screenitemids']); - } - - // filter - if (is_array($options['filter'])) { - $this->dbFilter('screens s', $options, $sql_parts); - } - - // search - if (is_array($options['search'])) { - zbx_db_search('screens s', $options, $sql_parts); - } - - // limit - if (zbx_ctype_digit($options['limit']) && $options['limit']) { - $sql_parts['limit'] = $options['limit']; - } - - $screenids = []; - $sql_parts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sql_parts); - $sql_parts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sql_parts); - $res = DBselect(self::createSelectQueryFromParts($sql_parts), $sql_parts['limit']); - while ($screen = DBfetch($res)) { - $screenids[$screen['screenid']] = true; - $result[$screen['screenid']] = $screen; - } - - // editable + PERMISSION CHECK - if ($user_data['type'] != USER_TYPE_SUPER_ADMIN && !$options['nopermissions'] && $screenids) { - $db_screen_items = DBselect( - 'SELECT si.screenid,si.resourcetype,si.resourceid,si.style'. - ' FROM screens_items si'. - ' WHERE '.dbConditionInt('si.screenid', array_keys($screenids)). - ' AND '.dbConditionInt('si.resourcetype', [ - SCREEN_RESOURCE_HOST_INFO, SCREEN_RESOURCE_TRIGGER_INFO, SCREEN_RESOURCE_TRIGGER_OVERVIEW, - SCREEN_RESOURCE_DATA_OVERVIEW, SCREEN_RESOURCE_HOSTGROUP_TRIGGERS, - SCREEN_RESOURCE_HOST_TRIGGERS, SCREEN_RESOURCE_GRAPH, SCREEN_RESOURCE_SIMPLE_GRAPH, - SCREEN_RESOURCE_PLAIN_TEXT, SCREEN_RESOURCE_CLOCK, SCREEN_RESOURCE_MAP - ]). - ' AND si.resourceid<>0' - ); - - $screens = []; - - while ($db_screen_item = DBfetch($db_screen_items)) { - if (!array_key_exists($db_screen_item['screenid'], $screens)) { - $screens[$db_screen_item['screenid']] = [ - 'groups' => [], 'hosts' => [], 'graphs' => [], 'items' => [], 'maps' => [] - ]; - } - - switch ($db_screen_item['resourcetype']) { - case SCREEN_RESOURCE_HOST_INFO: - case SCREEN_RESOURCE_TRIGGER_INFO: - case SCREEN_RESOURCE_TRIGGER_OVERVIEW: - case SCREEN_RESOURCE_DATA_OVERVIEW: - case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS: - $screens[$db_screen_item['screenid']]['groups'][$db_screen_item['resourceid']] = true; - break; - - case SCREEN_RESOURCE_HOST_TRIGGERS: - $screens[$db_screen_item['screenid']]['hosts'][$db_screen_item['resourceid']] = true; - break; - - case SCREEN_RESOURCE_GRAPH: - $screens[$db_screen_item['screenid']]['graphs'][$db_screen_item['resourceid']] = true; - break; - - case SCREEN_RESOURCE_SIMPLE_GRAPH: - case SCREEN_RESOURCE_PLAIN_TEXT: - $screens[$db_screen_item['screenid']]['items'][$db_screen_item['resourceid']] = true; - break; - - case SCREEN_RESOURCE_CLOCK: - if ($db_screen_item['style'] == TIME_TYPE_HOST) { - $screens[$db_screen_item['screenid']]['items'][$db_screen_item['resourceid']] = true; - } - break; - - case SCREEN_RESOURCE_MAP: - $screens[$db_screen_item['screenid']]['maps'][$db_screen_item['resourceid']] = true; - break; - } - } - - // groups - $groups = []; - - foreach ($screens as $screenid => $resources) { - foreach ($resources['groups'] as $groupid => $foo) { - $groups[$groupid][$screenid] = true; - } - } - - if ($groups) { - $db_groups = API::HostGroup()->get([ - 'output' => [], - 'groupids' => array_keys($groups), - 'preservekeys' => true - ]); - - foreach ($groups as $groupid => $resources) { - if (!array_key_exists($groupid, $db_groups)) { - foreach ($resources as $screenid => $foo) { - unset($screens[$screenid], $result[$screenid]); - } - } - } - } - - // hosts - $hosts = []; - - foreach ($screens as $screenid => $resources) { - foreach ($resources['hosts'] as $hostid => $foo) { - $hosts[$hostid][$screenid] = true; - } - } - - if ($hosts) { - $db_hosts = API::Host()->get([ - 'output' => [], - 'hostids' => array_keys($hosts), - 'preservekeys' => true - ]); - - foreach ($hosts as $hostid => $resources) { - if (!array_key_exists($hostid, $db_hosts)) { - foreach ($resources as $screenid => $foo) { - unset($screens[$screenid], $result[$screenid]); - } - } - } - } - - // graphs - $graphs = []; - - foreach ($screens as $screenid => $resources) { - foreach ($resources['graphs'] as $graphid => $foo) { - $graphs[$graphid][$screenid] = true; - } - } - - if ($graphs) { - $db_graphs = API::Graph()->get([ - 'output' => [], - 'graphids' => array_keys($graphs), - 'preservekeys' => true - ]); - - foreach ($graphs as $graphid => $resources) { - if (!array_key_exists($graphid, $db_graphs)) { - foreach ($resources as $screenid => $foo) { - unset($screens[$screenid], $result[$screenid]); - } - } - } - } - - // items - $items = []; - - foreach ($screens as $screenid => $resources) { - foreach ($resources['items'] as $itemid => $foo) { - $items[$itemid][$screenid] = true; - } - } - - if ($items) { - $db_items = API::Item()->get([ - 'output' => [], - 'itemids' => array_keys($items), - 'webitems' => true, - 'preservekeys' => true - ]); - - foreach ($items as $itemid => $resources) { - if (!array_key_exists($itemid, $db_items)) { - foreach ($resources as $screenid => $foo) { - unset($screens[$screenid], $result[$screenid]); - } - } - } - } - - // maps - $maps = []; - - foreach ($screens as $screenid => $resources) { - foreach ($resources['maps'] as $sysmapid => $foo) { - $maps[$sysmapid][$screenid] = true; - } - } - - if ($maps) { - $db_maps = API::Map()->get([ - 'output' => [], - 'sysmapids' => array_keys($maps), - 'preservekeys' => true - ]); - - foreach ($maps as $sysmapid => $resources) { - if (!array_key_exists($sysmapid, $db_maps)) { - foreach ($resources as $screenid => $foo) { - unset($screens[$screenid], $result[$screenid]); - } - } - } - } - } - - if ($count_output) { - if ($options['groupCount']) { - return [['rowscount' => count($result)]]; - } - - return (string) count($result); - } - - if ($result) { - $result = $this->addRelatedObjects($options, $result); - } - - // removing keys (hash -> array) - if (!$options['preservekeys']) { - $result = zbx_cleanHashes($result); - } - - return $result; - } - - /** - * Validate vsize and hsize parameters. - * - * @param array $screen - * - * @throws APIException if the input is invalid. - */ - protected function validateScreenSize(array $screen) { - foreach (['vsize', 'hsize'] as $field_name) { - if (!array_key_exists($field_name, $screen)) { - continue; - } - - if (!zbx_is_int($screen[$field_name])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _s('Incorrect value for field "%1$s": %2$s.', $field_name, _('a numeric value is expected')) - ); - } - - if ($screen[$field_name] < SCREEN_MIN_SIZE || $screen[$field_name] > SCREEN_MAX_SIZE) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _s('Incorrect value for field "%1$s": %2$s.', $field_name, - _s('must be between "%1$s" and "%2$s"', SCREEN_MIN_SIZE, SCREEN_MAX_SIZE) - ) - ); - } - } - } - - /** - * Validates the input parameters for the create() method. - * - * @param array $screens - * - * @throws APIException if the input is invalid. - */ - protected function validateCreate(array $screens) { - if (!$screens) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); - } - - $user_data = self::$userData; - - $screen_db_fields = ['name' => null]; - - foreach ($screens as &$screen) { - if (!check_db_fields($screen_db_fields, $screen)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect input parameters.')); - } - - $this->validateScreenSize($screen); - - unset($screen['screenid']); - } - unset($screen); - - // Check for duplicate names. - $duplicate = CArrayHelper::findDuplicate($screens, 'name'); - if ($duplicate) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Duplicate "name" value "%1$s" for screen.', $duplicate['name']) - ); - } - - // Check if screen already exists. - $db_screens = $this->get([ - 'output' => ['name'], - 'filter' => ['name' => zbx_objectValues($screens, 'name')], - 'nopermissions' => true, - 'limit' => 1 - ]); - - if ($db_screens) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" already exists.', $db_screens[0]['name'])); - } - - $private_validator = new CLimitedSetValidator([ - 'values' => [PUBLIC_SHARING, PRIVATE_SHARING] - ]); - - $permission_validator = new CLimitedSetValidator([ - 'values' => [PERM_READ, PERM_READ_WRITE] - ]); - - foreach ($screens as $screen) { - // Check if owner can be set. - if (array_key_exists('userid', $screen)) { - if ($screen['userid'] === '' || $screen['userid'] === null || $screen['userid'] === false) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Screen owner cannot be empty.')); - } - elseif ($screen['userid'] != $user_data['userid'] && $user_data['type'] != USER_TYPE_SUPER_ADMIN - && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Only administrators can set screen owner.')); - } - } - - // Check for invalid "private" values. - if (array_key_exists('private', $screen)) { - if (!$private_validator->validate($screen['private'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect "private" value "%1$s" for screen "%2$s".', $screen['private'], $screen['name']) - ); - } - } - - $userids = []; - - // Screen user shares. - if (array_key_exists('users', $screen)) { - if (!is_array($screen['users'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); - } - - $required_fields = ['userid', 'permission']; - - foreach ($screen['users'] as $share) { - // Check required parameters. - $missing_keys = array_diff($required_fields, array_keys($share)); - - if ($missing_keys) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'User sharing is missing parameters: %1$s for screen "%2$s".', - implode(', ', $missing_keys), - $screen['name'] - )); - } - else { - foreach ($required_fields as $field) { - if ($share[$field] === '' || $share[$field] === null) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Sharing option "%1$s" is missing a value for screen "%2$s".', - $field, - $screen['name'] - )); - } - } - } - - if (!$permission_validator->validate($share['permission'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Incorrect "permission" value "%1$s" in users for screen "%2$s".', - $share['permission'], - $screen['name'] - )); - } - - if (array_key_exists('private', $screen) && $screen['private'] == PUBLIC_SHARING - && $share['permission'] == PERM_READ) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name']) - ); - } - - if (array_key_exists($share['userid'], $userids)) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Duplicate userid "%1$s" in users for screen "%2$s".', $share['userid'], $screen['name']) - ); - } - - $userids[$share['userid']] = $share['userid']; - } - } - - if (array_key_exists('userid', $screen) && $screen['userid']) { - $userids[$screen['userid']] = $screen['userid']; - } - - // Users validation. - if ($userids) { - $db_users = API::User()->get([ - 'userids' => $userids, - 'countOutput' => true - ]); - - if (count($userids) != $db_users) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect user ID specified for screen "%1$s".', $screen['name']) - ); - } - } - - // Screen user group shares. - if (array_key_exists('userGroups', $screen)) { - if (!is_array($screen['userGroups'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); - } - - $shared_user_groupids = []; - $required_fields = ['usrgrpid', 'permission']; - - foreach ($screen['userGroups'] as $share) { - // Check required parameters. - $missing_keys = array_diff($required_fields, array_keys($share)); - - if ($missing_keys) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'User group sharing is missing parameters: %1$s for screen "%2$s".', - implode(', ', $missing_keys), - $screen['name'] - )); - } - else { - foreach ($required_fields as $field) { - if ($share[$field] === '' || $share[$field] === null) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Field "%1$s" is missing a value for screen "%2$s".', - $field, - $screen['name'] - )); - } - } - } - - if (!$permission_validator->validate($share['permission'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Incorrect "permission" value "%1$s" in user groups for screen "%2$s".', - $share['permission'], - $screen['name'] - )); - } - - if (array_key_exists('private', $screen) && $screen['private'] == PUBLIC_SHARING - && $share['permission'] == PERM_READ) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name']) - ); - } - - if (array_key_exists($share['usrgrpid'], $shared_user_groupids)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Duplicate usrgrpid "%1$s" in user groups for screen "%2$s".', - $share['usrgrpid'], - $screen['name'] - )); - } - - $shared_user_groupids[$share['usrgrpid']] = $share['usrgrpid']; - } - - if ($shared_user_groupids) { - $db_user_groups = API::UserGroup()->get([ - 'usrgrpids' => $shared_user_groupids, - 'countOutput' => true - ]); - - if (count($shared_user_groupids) != $db_user_groups) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect user group ID specified for screen "%1$s".', $screen['name']) - ); - } - } - - unset($shared_user_groupids); - } - } - } - - /** - * Create screen. - * - * @param array $screens - * - * @return array - */ - public function create(array $screens) { - $screens = zbx_toArray($screens); - - $this->validateCreate($screens); - - foreach ($screens as &$screen) { - $screen['userid'] = array_key_exists('userid', $screen) ? $screen['userid'] : self::$userData['userid']; - } - unset($screen); - - $screenids = DB::insert('screens', $screens); - - $shared_users = []; - $shared_user_groups = []; - $screenItems = []; - - foreach ($screens as $key => $screen) { - // Screen user shares. - if (array_key_exists('users', $screen)) { - foreach ($screen['users'] as $user) { - $shared_users[] = [ - 'screenid' => $screenids[$key], - 'userid' => $user['userid'], - 'permission' => $user['permission'] - ]; - } - } - - // Screen user group shares. - if (array_key_exists('userGroups', $screen)) { - foreach ($screen['userGroups'] as $user_group) { - $shared_user_groups[] = [ - 'screenid' => $screenids[$key], - 'usrgrpid' => $user_group['usrgrpid'], - 'permission' => $user_group['permission'] - ]; - } - } - - // Create screen items. - if (array_key_exists('screenitems', $screen)) { - foreach ($screen['screenitems'] as $screenItem) { - $screenItem['screenid'] = $screenids[$key]; - - $screenItems[] = $screenItem; - } - } - } - - DB::insert('screen_user', $shared_users); - DB::insert('screen_usrgrp', $shared_user_groups); - - if ($screenItems) { - API::ScreenItem()->create($screenItems); - } - - - return ['screenids' => $screenids]; - } - - /** - * Validates the input parameters for the update() method. - * - * @param array $screens - * @param array $db_screens array of existing screens with screen IDs as keys. - * - * @throws APIException if the input is invalid. - */ - protected function validateUpdate(array $screens, array $db_screens) { - if (!$screens) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); - } - - $user_data = self::$userData; - - // Validate given IDs. - $this->checkObjectIds($screens, 'screenid', - _('No "%1$s" given for screen.'), - _('Empty screen ID.'), - _('Incorrect screen ID.') - ); - - $check_names = []; - - foreach ($screens as $screen) { - $this->validateScreenSize($screen); - - if (!array_key_exists($screen['screenid'], $db_screens)) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _('No permissions to referred object or it does not exist!') - ); - } - } - - $screens = $this->extendFromObjects(zbx_toHash($screens, 'screenid'), $db_screens, ['name']); - - foreach ($screens as $screen) { - if (array_key_exists('name', $screen)) { - // Validate "name" field. - if (array_key_exists('name', $screen)) { - if (is_array($screen['name'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); - } - elseif ($screen['name'] === '' || $screen['name'] === null || $screen['name'] === false) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Screen name cannot be empty.')); - } - - if ($db_screens[$screen['screenid']]['name'] !== $screen['name']) { - $check_names[] = $screen; - } - } - } - } - - if ($check_names) { - // Check for duplicate names. - $duplicate = CArrayHelper::findDuplicate($check_names, 'name'); - if ($duplicate) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Duplicate "name" value "%1$s" for screen.', $duplicate['name']) - ); - } - - $db_screen_names = $this->get([ - 'output' => ['screenid', 'name'], - 'filter' => ['name' => zbx_objectValues($check_names, 'name')], - 'nopermissions' => true - ]); - $db_screen_names = zbx_toHash($db_screen_names, 'name'); - - // Check for existing names. - foreach ($check_names as $screen) { - if (array_key_exists($screen['name'], $db_screen_names) - && bccomp($db_screen_names[$screen['name']]['screenid'], $screen['screenid']) != 0) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" already exists.', $screen['name']) - ); - } - } - } - - $private_validator = new CLimitedSetValidator([ - 'values' => [PUBLIC_SHARING, PRIVATE_SHARING] - ]); - - $permission_validator = new CLimitedSetValidator([ - 'values' => [PERM_READ, PERM_READ_WRITE] - ]); - - foreach ($screens as $screen) { - // Check if owner can be set. - if (array_key_exists('userid', $screen)) { - if ($screen['userid'] === '' || $screen['userid'] === null || $screen['userid'] === false) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Screen owner cannot be empty.')); - } - elseif ($screen['userid'] != $user_data['userid'] && $user_data['type'] != USER_TYPE_SUPER_ADMIN - && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Only administrators can set screen owner.')); - } - } - - // Unset extra field. - unset($db_screens[$screen['screenid']]['userid']); - - $screen = array_merge($db_screens[$screen['screenid']], $screen); - - if (!$private_validator->validate($screen['private'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect "private" value "%1$s" for screen "%2$s".', $screen['private'], $screen['name']) - ); - } - - $userids = []; - - // Screen user shares. - if (array_key_exists('users', $screen)) { - if (!is_array($screen['users'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); - } - - $required_fields = ['userid', 'permission']; - - foreach ($screen['users'] as $share) { - // Check required parameters. - $missing_keys = array_diff($required_fields, array_keys($share)); - - if ($missing_keys) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'User sharing is missing parameters: %1$s for screen "%2$s".', - implode(', ', $missing_keys), - $screen['name'] - )); - } - else { - foreach ($required_fields as $field) { - if ($share[$field] === '' || $share[$field] === null) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Sharing option "%1$s" is missing a value for screen "%2$s".', - $field, - $screen['name'] - )); - } - } - } - - if (!$permission_validator->validate($share['permission'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Incorrect "permission" value "%1$s" in users for screen "%2$s".', - $share['permission'], - $screen['name'] - )); - } - - if ($screen['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name']) - ); - } - - if (array_key_exists($share['userid'], $userids)) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Duplicate userid "%1$s" in users for screen "%2$s".', $share['userid'], $screen['name']) - ); - } - - $userids[$share['userid']] = $share['userid']; - } - } - - if (array_key_exists('userid', $screen) && $screen['userid']) { - $userids[$screen['userid']] = $screen['userid']; - } - - // Users validation. - if ($userids) { - $db_users = API::User()->get([ - 'userids' => $userids, - 'countOutput' => true - ]); - - if (count($userids) != $db_users) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect user ID specified for screen "%1$s".', $screen['name']) - ); - } - } - - // Screen user group shares. - if (array_key_exists('userGroups', $screen)) { - if (!is_array($screen['userGroups'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); - } - - $shared_user_groupids = []; - $required_fields = ['usrgrpid', 'permission']; - - foreach ($screen['userGroups'] as $share) { - // Check required parameters. - $missing_keys = array_diff($required_fields, array_keys($share)); - - if ($missing_keys) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'User group sharing is missing parameters: %1$s for screen "%2$s".', - implode(', ', $missing_keys), - $screen['name']) - ); - } - else { - foreach ($required_fields as $field) { - if ($share[$field] === '' || $share[$field] === null) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Sharing option "%1$s" is missing a value for screen "%2$s".', - $field, - $screen['name'] - )); - } - } - } - - if (!$permission_validator->validate($share['permission'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Incorrect "permission" value "%1$s" in user groups for screen "%2$s".', - $share['permission'], - $screen['name'] - )); - } - - if ($screen['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name']) - ); - } - - if (array_key_exists($share['usrgrpid'], $shared_user_groupids)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Duplicate usrgrpid "%1$s" in user groups for screen "%2$s".', - $share['usrgrpid'], - $screen['name'] - )); - } - - $shared_user_groupids[$share['usrgrpid']] = $share['usrgrpid']; - } - - if ($shared_user_groupids) { - $db_user_groups = API::UserGroup()->get([ - 'usrgrpids' => $shared_user_groupids, - 'countOutput' => true - ]); - - if (count($shared_user_groupids) != $db_user_groups) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Incorrect user group ID specified for screen "%1$s".', $screen['name']) - ); - } - } - - unset($shared_user_groupids); - } - } - } - - /** - * Update screen. - * - * @param array $screens - * - * @return array - */ - public function update(array $screens) { - $screens = zbx_toArray($screens); - - // check screen IDs before doing anything - $this->checkObjectIds($screens, 'screenid', - _('No "%1$s" given for screen.'), - _('Empty screen ID for screen.'), - _('Incorrect screen ID.') - ); - - $db_screens = $this->get([ - 'output' => ['name', 'screenid', 'hsize', 'vsize', 'private'], - 'selectScreenItems' => ['screenitemid', 'x', 'y', 'colspan', 'rowspan'], - 'selectUsers' => ['screenuserid', 'screenid', 'userid', 'permission'], - 'selectUserGroups' => ['screenusrgrpid', 'screenid', 'usrgrpid', 'permission'], - 'screenids' => zbx_objectValues($screens, 'screenid'), - 'editable' => true, - 'preservekeys' => true - ]); - - $this->validateUpdate($screens, $db_screens); - $this->updateReal($screens, $db_screens); - $this->truncateScreenItems($screens, $db_screens); - - return ['screenids' => zbx_objectValues($screens, 'screenid')]; - } - - /** - * Saves screens and screen data. - * - * @param array $screens - * @param array $db_screens array of existing screens with screen IDs as keys - */ - protected function updateReal(array $screens, array $db_screens) { - $update_screens = []; - CArrayHelper::sort($screens, ['screenid']); - - foreach ($screens as $screen) { - $screenid = $screen['screenid']; - unset($screen['screenid'], $screen['screenitems'], $screen['users'], $screen['userGroups']); - - if ($screen) { - $update_screens[] = [ - 'values' => $screen, - 'where' => ['screenid' => $screenid] - ]; - } - } - - DB::update('screens', $update_screens); - - $shared_userids_to_delete = []; - $shared_users_to_update = []; - $shared_users_to_add = []; - $shared_user_groupids_to_delete = []; - $shared_user_groups_to_update = []; - $shared_user_groups_to_add = []; - - foreach ($screens as $screen) { - $db_screen = $db_screens[$screen['screenid']]; - - // Screen user shares. - if (array_key_exists('users', $screen)) { - $user_shares_diff = zbx_array_diff($screen['users'], $db_screen['users'], 'userid'); - - foreach ($user_shares_diff['both'] as $update_user_share) { - $shared_users_to_update[] = [ - 'values' => $update_user_share, - 'where' => ['userid' => $update_user_share['userid'], 'screenid' => $screen['screenid']] - ]; - } - - foreach ($user_shares_diff['first'] as $new_shared_user) { - $new_shared_user['screenid'] = $screen['screenid']; - $shared_users_to_add[] = $new_shared_user; - } - - $shared_userids_to_delete = array_merge($shared_userids_to_delete, - zbx_objectValues($user_shares_diff['second'], 'screenuserid') - ); - } - - // Screen user group shares. - if (array_key_exists('userGroups', $screen)) { - $user_group_shares_diff = zbx_array_diff($screen['userGroups'], $db_screen['userGroups'], 'usrgrpid'); - - foreach ($user_group_shares_diff['both'] as $update_user_share) { - $shared_user_groups_to_update[] = [ - 'values' => $update_user_share, - 'where' => ['usrgrpid' => $update_user_share['usrgrpid'], 'screenid' => $screen['screenid']] - ]; - } - - foreach ($user_group_shares_diff['first'] as $new_shared_user_group) { - $new_shared_user_group['screenid'] = $screen['screenid']; - $shared_user_groups_to_add[] = $new_shared_user_group; - } - - $shared_user_groupids_to_delete = array_merge($shared_user_groupids_to_delete, - zbx_objectValues($user_group_shares_diff['second'], 'screenusrgrpid') - ); - } - - // Replace screen items. - if (array_key_exists('screenitems', $screen)) { - $this->replaceItems($screen['screenid'], $screen['screenitems']); - } - } - - // User shares. - DB::insert('screen_user', $shared_users_to_add); - DB::update('screen_user', $shared_users_to_update); - - if ($shared_userids_to_delete) { - DB::delete('screen_user', ['screenuserid' => $shared_userids_to_delete]); - } - - // User group shares. - DB::insert('screen_usrgrp', $shared_user_groups_to_add); - DB::update('screen_usrgrp', $shared_user_groups_to_update); - - if ($shared_user_groupids_to_delete) { - DB::delete('screen_usrgrp', ['screenusrgrpid' => $shared_user_groupids_to_delete]); - } - } - - /** - * Delete or reduce the size of screens items when reducing the size of the screens. - * - * Each array in the $screens array must have the following values: - * - screenid - * - hsize - * - vsize - * - * Each array in the $dbScreens array must have the following values: - * - screenid - * - hsize - * - vsize - * - screenitems - * - * @param array $screens - * @param array $dbScreens array of existing screens with screen IDs as keys - */ - protected function truncateScreenItems(array $screens, array $dbScreens) { - $deleteScreenItemIds = []; - $updateScreenItems = []; - foreach ($screens as $screen) { - if (array_key_exists('screenitems', $screen)) { - continue; - } - - $dbScreen = $dbScreens[$screen['screenid']]; - $dbScreenItems = $dbScreen['screenitems']; - - if (isset($screen['hsize'])) { - foreach ($dbScreenItems as $dbScreenItem) { - // delete screen items that are located on the deleted columns - if ($dbScreenItem['x'] > $screen['hsize'] - 1) { - $deleteScreenItemIds[$dbScreenItem['screenitemid']] = $dbScreenItem['screenitemid']; - } - // reduce the colspan of screenitems that are displayed on the deleted columns - elseif (($dbScreenItem['x'] + $dbScreenItem['colspan']) > $screen['hsize']) { - $colspan = $screen['hsize'] - $dbScreenItem['x']; - - $screenItemId = $dbScreenItem['screenitemid']; - $updateScreenItems[$screenItemId]['screenitemid'] = $dbScreenItem['screenitemid']; - $updateScreenItems[$screenItemId]['colspan'] = $colspan; - } - } - } - - if (isset($screen['vsize'])) { - foreach ($dbScreenItems as $dbScreenItem) { - // delete screen items that are located on the deleted rows - if ($dbScreenItem['y'] > $screen['vsize'] - 1) { - $deleteScreenItemIds[$dbScreenItem['screenitemid']] = $dbScreenItem['screenitemid']; - } - // reduce the rowspan of screenitems that are displayed on the deleted rows - elseif (($dbScreenItem['y'] + $dbScreenItem['rowspan']) > $screen['vsize']) { - $rowspan = $screen['vsize'] - $dbScreenItem['y']; - - $screenItemId = $dbScreenItem['screenitemid']; - $updateScreenItems[$screenItemId]['screenitemid'] = $dbScreenItem['screenitemid']; - $updateScreenItems[$screenItemId]['rowspan'] = $rowspan; - } - } - } - } - - if ($deleteScreenItemIds) { - DB::delete('screens_items', ['screenitemid' => $deleteScreenItemIds]); - } - - foreach ($updateScreenItems as $screenItem) { - DB::updateByPk('screens_items', $screenItem['screenitemid'], $screenItem); - } - } - - /** - * Validate input for delete method. - * - * @param array $screenids - * - * @throws APIException if the input is invalid. - */ - protected function validateDelete(array $screenids) { - if (!$screenids) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); - } - - $db_screens = $this->get([ - 'output' => ['screenid'], - 'screenids' => $screenids, - 'editable' => true, - 'preservekeys' => true - ]); - - foreach ($screenids as $screenid) { - if (!array_key_exists($screenid, $db_screens)) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _('No permissions to referred object or it does not exist!') - ); - } - } - } - - /** - * Delete screen. - * - * @param array $screenids - * - * @return array - */ - public function delete(array $screenids) { - $this->validateDelete($screenids); - - DB::delete('screens_items', ['screenid' => $screenids]); - DB::delete('slides', ['screenid' => $screenids]); - DB::delete('screens', ['screenid' => $screenids]); - DB::delete('profiles', [ - 'idx' => 'web.favorite.screenids', - 'source' => 'screenid', - 'value_id' => $screenids - ]); - - return ['screenids' => $screenids]; - } - - /** - * Replaces all of the screen items of the given screen. - * - * @param int $screenId The ID of the target screen - * @param array $screenItems An array of screen items - */ - protected function replaceItems($screenId, $screenItems) { - foreach ($screenItems as &$screenItem) { - $screenItem['screenid'] = $screenId; - } - unset($screenItem); - - $createScreenItems = $deleteScreenItems = $updateScreenItems = []; - $deleteScreenItemsIds = []; - - $dbScreenItems = API::ScreenItem()->get([ - 'output' => ['screenitemid'], - 'screenids' => $screenId, - 'preservekeys' => true - ]); - - foreach ($screenItems as $screenItem) { - if (isset($screenItem['screenitemid']) && isset($dbScreenItems[$screenItem['screenitemid']])) { - $updateScreenItems[$screenItem['screenitemid']] = $screenItem; - } - else { - $createScreenItems[] = $screenItem; - } - } - - foreach ($dbScreenItems as $dbScreenItem) { - if (!isset($updateScreenItems[$dbScreenItem['screenitemid']])) { - $deleteScreenItemsIds[$dbScreenItem['screenitemid']] = $dbScreenItem['screenitemid']; - } - } - - if ($deleteScreenItemsIds) { - API::ScreenItem()->delete($deleteScreenItemsIds); - } - if ($updateScreenItems) { - API::ScreenItem()->update($updateScreenItems); - } - if ($createScreenItems) { - API::ScreenItem()->create($createScreenItems); - } - } - - protected function addRelatedObjects(array $options, array $result) { - $result = parent::addRelatedObjects($options, $result); - - $screenIds = array_keys($result); - - // adding ScreenItems - if ($options['selectScreenItems'] !== null && $options['selectScreenItems'] != API_OUTPUT_COUNT) { - $screenItems = API::getApiService()->select('screens_items', [ - 'output' => $this->outputExtend($options['selectScreenItems'], ['screenid', 'screenitemid']), - 'filter' => ['screenid' => $screenIds], - 'preservekeys' => true - ]); - - $relation_map = $this->createRelationMap($screenItems, 'screenid', 'screenitemid'); - - $screenItems = $this->unsetExtraFields($screenItems, ['screenid', 'screenitemid'], $options['selectScreenItems']); - $result = $relation_map->mapMany($result, $screenItems, 'screenitems'); - } - - // Adding user shares. - if ($options['selectUsers'] !== null && $options['selectUsers'] != API_OUTPUT_COUNT) { - $relation_map = $this->createRelationMap($result, 'screenid', 'userid', 'screen_user'); - // Get all allowed users. - $related_users = API::User()->get([ - 'output' => ['userid'], - 'userids' => $relation_map->getRelatedIds(), - 'preservekeys' => true - ]); - - $related_userids = zbx_objectValues($related_users, 'userid'); - - if ($related_userids) { - $users = API::getApiService()->select('screen_user', [ - 'output' => $this->outputExtend($options['selectUsers'], ['screenid', 'userid']), - 'filter' => ['screenid' => $screenIds, 'userid' => $related_userids], - 'preservekeys' => true - ]); - - $relation_map = $this->createRelationMap($users, 'screenid', 'screenuserid'); - - $users = $this->unsetExtraFields($users, ['screenuserid', 'userid', 'permission'], - $options['selectUsers'] - ); - - foreach ($users as &$user) { - unset($user['screenid']); - } - unset($user); - - $result = $relation_map->mapMany($result, $users, 'users'); - } - else { - foreach ($result as &$row) { - $row['users'] = []; - } - unset($row); - } - } - - // Adding user group shares. - if ($options['selectUserGroups'] !== null && $options['selectUserGroups'] != API_OUTPUT_COUNT) { - $relation_map = $this->createRelationMap($result, 'screenid', 'usrgrpid', 'screen_usrgrp'); - // Get all allowed groups. - $related_groups = API::UserGroup()->get([ - 'output' => ['usrgrpid'], - 'usrgrpids' => $relation_map->getRelatedIds(), - 'preservekeys' => true - ]); - - $related_groupids = zbx_objectValues($related_groups, 'usrgrpid'); - - if ($related_groupids) { - $user_groups = API::getApiService()->select('screen_usrgrp', [ - 'output' => $this->outputExtend($options['selectUserGroups'], ['screenid', 'usrgrpid']), - 'filter' => ['screenid' => $screenIds, 'usrgrpid' => $related_groupids], - 'preservekeys' => true - ]); - - $relation_map = $this->createRelationMap($user_groups, 'screenid', 'screenusrgrpid'); - - $user_groups = $this->unsetExtraFields($user_groups, ['screenusrgrpid', 'usrgrpid', 'permission'], - $options['selectUserGroups'] - ); - - foreach ($user_groups as &$user_group) { - unset($user_group['screenid']); - } - unset($user_group); - - $result = $relation_map->mapMany($result, $user_groups, 'userGroups'); - } - else { - foreach ($result as &$row) { - $row['userGroups'] = []; - } - unset($row); - } - } - - return $result; - } -} diff --git a/ui/include/classes/api/services/CScreenItem.php b/ui/include/classes/api/services/CScreenItem.php deleted file mode 100644 index dec428daf17..00000000000 --- a/ui/include/classes/api/services/CScreenItem.php +++ /dev/null @@ -1,933 +0,0 @@ -<?php -/* -** Zabbix -** Copyright (C) 2001-2021 Zabbix SIA -** -** This program 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 2 of the License, or -** (at your option) any later version. -** -** This program 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 this program; if not, write to the Free Software -** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -**/ - - -/** - * Class containing methods for operations with screen items. - */ -class CScreenItem extends CApiService { - - public const ACCESS_RULES = [ - 'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER], - 'create' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS], - 'update' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS], - 'delete' => ['min_user_type' => USER_TYPE_ZABBIX_USER, 'action' => CRoleHelper::ACTIONS_EDIT_DASHBOARDS] - ]; - - protected $tableName = 'screens_items'; - protected $tableAlias = 'si'; - - /** - * Supported values for the resourcetype column. - * - * @var array - */ - protected static $resourceTypes = [ - SCREEN_RESOURCE_GRAPH, - SCREEN_RESOURCE_SIMPLE_GRAPH, - SCREEN_RESOURCE_MAP, - SCREEN_RESOURCE_PLAIN_TEXT, - SCREEN_RESOURCE_HOST_INFO, - SCREEN_RESOURCE_TRIGGER_INFO, - SCREEN_RESOURCE_SERVER_INFO, - SCREEN_RESOURCE_CLOCK, - SCREEN_RESOURCE_TRIGGER_OVERVIEW, - SCREEN_RESOURCE_DATA_OVERVIEW, - SCREEN_RESOURCE_URL, - SCREEN_RESOURCE_ACTIONS, - SCREEN_RESOURCE_EVENTS, - SCREEN_RESOURCE_HOSTGROUP_TRIGGERS, - SCREEN_RESOURCE_SYSTEM_STATUS, - SCREEN_RESOURCE_HOST_TRIGGERS, - SCREEN_RESOURCE_LLD_GRAPH, - SCREEN_RESOURCE_LLD_SIMPLE_GRAPH - ]; - - protected $sortColumns = [ - 'screenitemid', - 'screenid' - ]; - - public function __construct() { - parent::__construct(); - - $this->getOptions = zbx_array_merge($this->getOptions, [ - 'screenitemids' => null, - 'screenids' => null, - 'editable' => false, - 'sortfield' => '', - 'sortorder' => '', - 'preservekeys' => false, - 'countOutput' => false - ]); - } - - /** - * Get screem item data. - * - * @param array $options - * @param array $options['screenitemids'] Search by screen item IDs - * @param array $options['screenids'] Search by screen IDs - * @param array $options['filter'] Result filter - * @param array $options['limit'] The size of the result set - * - * @return array - */ - public function get(array $options = []) { - $options = zbx_array_merge($this->getOptions, $options); - - // build and execute query - $sql = $this->createSelectQuery($this->tableName(), $options); - $res = DBselect($sql, $options['limit']); - - // fetch results - $result = []; - while ($row = DBfetch($res)) { - // count query, return a single result - if ($options['countOutput']) { - $result = $row['rowscount']; - } - // normal select query - else { - if ($options['preservekeys']) { - $result[$row['screenitemid']] = $row; - } - else { - $result[] = $row; - } - } - } - - return $result; - } - - /** - * Create screen items. - * - * @param array $screenItems An array of screen items - * - * @return array - */ - public function create(array $screenItems) { - $screenItems = zbx_toArray($screenItems); - - $this->validateCreate($screenItems); - - $screenItemIds = DB::insert($this->tableName(), $screenItems); - - return ['screenitemids' => $screenItemIds]; - } - - /** - * Validates the input parameters for the create() method. - * - * @throws APIException if the input is invalid - * - * @param array $screenItems - */ - protected function validateCreate(array $screenItems) { - $screenItemDBfields = [ - 'screenid' => null, - 'resourcetype' => null - ]; - - $defaults = [ - 'x' => 0, - 'y' => 0, - 'colspan' => 1, - 'rowspan' => 1 - ]; - - foreach ($screenItems as &$screenItem) { - if (!check_db_fields($screenItemDBfields, $screenItem)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Invalid method parameters.')); - } - - unset($screenItem['screenitemid']); - $screenItem += $defaults; - } - unset($screenItem); - - $this->validateItemsURL($screenItems); - - $screenIds = array_keys(array_flip(zbx_objectValues($screenItems, 'screenid'))); - - $dbScreens = API::Screen()->get([ - 'output' => ['screenid', 'hsize', 'vsize', 'name'], - 'screenids' => $screenIds, - 'editable' => true, - 'preservekeys' => true - ]); - - $dbScreenItems = API::getApiService()->select($this->tableName(), [ - 'output' => ['screenitemid', 'screenid', 'x', 'y', 'rowspan', 'colspan'], - 'filter' => ['screenid' => array_keys($dbScreens)], - 'preservekeys' => true - ]); - - $this->checkInput($screenItems, $dbScreenItems); - $this->checkDuplicateResourceInCell(array_merge($screenItems, $dbScreenItems), $dbScreens); - - foreach ($screenItems as $screenItem) { - $this->checkSpans($screenItem, $dbScreens[$screenItem['screenid']]); - $this->checkSpansInBounds($screenItem, $dbScreenItems, $dbScreens[$screenItem['screenid']]); - $this->checkGridCoordinates($screenItem, $dbScreens[$screenItem['screenid']]); - } - } - - /** - * Updates screen items. - * - * @param array $screenItems An array of screen items - * - * @return array - */ - public function update(array $screenItems) { - $screenItems = zbx_toArray($screenItems); - - $this->validateUpdate($screenItems); - - $screenItems = zbx_toHash($screenItems, 'screenitemid'); - - $update = []; - $screenItemIds = []; - - foreach ($screenItems as $screenItem) { - $screenItemId = $screenItem['screenitemid']; - unset($screenItem['screenitemid']); - - // If screen item type (field "resourcetype") is set to one that does not use "resourceid" field, - // value of field "resourceid" is set to 0 for those resource types. Clock screen item type has - // more refined check. - if (isset($screenItem['resourcetype'])) { - switch ($screenItem['resourcetype']) { - case SCREEN_RESOURCE_CLOCK: - if (isset($screenItem['style']) && $screenItem['style'] != TIME_TYPE_HOST) { - $screenItem['resourceid'] = 0; - } - break; - - case SCREEN_RESOURCE_ACTIONS: - case SCREEN_RESOURCE_HISTORY: - case SCREEN_RESOURCE_SERVER_INFO: - case SCREEN_RESOURCE_SYSTEM_STATUS: - case SCREEN_RESOURCE_URL: - $screenItem['resourceid'] = 0; - break; - } - } - - $update[] = [ - 'values' => $screenItem, - 'where' => ['screenitemid' => $screenItemId] - ]; - - $screenItemIds[] = $screenItemId; - } - - DB::update($this->tableName(), $update); - - return ['screenitemids' => $screenItemIds]; - } - - /** - * Validates the input parameters for the update() method. - * - * @throws APIException - * - * @param array $screenItems - */ - protected function validateUpdate(array $screenItems) { - $screenItemDBfields = [ - 'screenitemid' => null - ]; - - foreach ($screenItems as $screenItem) { - if (!check_db_fields($screenItemDBfields, $screenItem)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Invalid method parameters.')); - } - } - - $this->validateItemsURL($screenItems); - - $screenItems = zbx_toHash($screenItems, 'screenitemid'); - $screenItemIds = array_keys($screenItems); - - $dbScreens = API::Screen()->get([ - 'output' => ['screenid', 'hsize', 'vsize', 'name'], - 'screenitemids' => $screenItemIds, - 'editable' => true, - 'preservekeys' => true - ]); - - $dbScreenItems = API::getApiService()->select($this->tableName(), [ - 'output' => ['screenitemid', 'screenid', 'x', 'y', 'rowspan', 'colspan', 'resourcetype', 'resourceid', - 'style' - ], - 'filter' => ['screenid' => array_keys($dbScreens)], - 'preservekeys' => true - ]); - - $screenItems = $this->extendObjects($this->tableName(), $screenItems, - ['screenid', 'x', 'y', 'rowspan', 'colspan', 'style'] - ); - - $this->checkInput($screenItems, $dbScreenItems); - $this->checkDuplicateResourceInCell($screenItems + $dbScreenItems, $dbScreens); - - foreach ($screenItems as $screenItem) { - $this->checkSpans($screenItem, $dbScreens[$screenItem['screenid']]); - $this->checkSpansInBounds($screenItem, $dbScreenItems, $dbScreens[$screenItem['screenid']]); - $this->checkGridCoordinates($screenItem, $dbScreens[$screenItem['screenid']]); - } - } - - /** - * Update screen items using the given 'x' and 'y' parameters. - * If the given cell is free, a new screen item will be created. - * - * @param array $screenItems An array of screen items with the given X and Y coordinates - * - * @return array - */ - public function updateByPosition(array $screenItems) { - $screenItemDBfields = [ - 'screenid' => null, - 'x' => null, - 'y' => null - ]; - - foreach ($screenItems as $screenItem) { - if (!check_db_fields($screenItemDBfields, $screenItem)) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Invalid method parameters.')); - } - } - - $dbScreenItems = $this->get([ - 'output' => ['screenitemid', 'screenid', 'x', 'y'], - 'screenids' => zbx_objectValues($screenItems, 'screenid'), - 'editable' => true, - 'preservekeys' => true - ]); - - $create = $update = $affectedIds = []; - - foreach ($screenItems as $screenItem) { - foreach ($dbScreenItems as $dbScreenItem) { - if ($screenItem['screenid'] == $dbScreenItem['screenid'] - && $screenItem['x'] == $dbScreenItem['x'] && $screenItem['y'] == $dbScreenItem['y']) { - $screenItem['screenitemid'] = $dbScreenItem['screenitemid']; - $update[$dbScreenItem['screenitemid']] = $screenItem; - - continue 2; - } - } - - $create[] = $screenItem; - } - - if ($update) { - $screenItems = API::ScreenItem()->update($update); - - $affectedIds = $screenItems['screenitemids']; - } - - if ($create) { - $screenItems = API::ScreenItem()->create($create); - - $affectedIds = array_merge($affectedIds, $screenItems['screenitemids']); - } - - return ['screenitemids' => $affectedIds]; - } - - /** - * Deletes screen items. - * - * @param array $screenItemIds - * - * @return array - */ - public function delete(array $screenItemIds) { - // check permissions - $dbScreenItems = $this->get([ - 'output' => ['screenitemid'], - 'screenitemids' => $screenItemIds, - 'preservekeys' => true - ]); - - foreach ($screenItemIds as $screenItemId) { - if (!isset($dbScreenItems[$screenItemId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); - } - } - - // delete screen items - DB::delete($this->tableName(), [ - 'screenitemid' => $screenItemIds - ]); - - return ['screenitemids' => $screenItemIds]; - } - - /** - * Validates screen items. - * - * If the $dbScreenItems parameter is given, the screen items will be matched - * against the ones given in $dbScreenItems. If a screen item is not present in - * $dbScreenItems, a ZBX_API_ERROR_PERMISSIONS exception will be thrown. - * - * @throws APIException if a validation error occurred. - * - * @param array $screenItems - * @param array $dbScreenItems - */ - protected function checkInput(array $screenItems, array $dbScreenItems = []) { - $hostGroupsIds = []; - $hostIds = []; - $graphIds = []; - $itemIds = []; - $mapIds = []; - $screenIds = []; - $itemPrototypeIds = []; - $graphPrototypeIds = []; - - $screenItems = $this->extendFromObjects($screenItems, $dbScreenItems, ['resourcetype']); - - $validStyles = [ - SCREEN_RESOURCE_CLOCK => [TIME_TYPE_LOCAL, TIME_TYPE_SERVER, TIME_TYPE_HOST], - SCREEN_RESOURCE_DATA_OVERVIEW => [STYLE_TOP, STYLE_LEFT], - SCREEN_RESOURCE_TRIGGER_OVERVIEW => [STYLE_TOP, STYLE_LEFT], - SCREEN_RESOURCE_HOST_INFO => [STYLE_VERTICAL, STYLE_HORIZONTAL], - SCREEN_RESOURCE_TRIGGER_INFO => [STYLE_VERTICAL, STYLE_HORIZONTAL] - ]; - - foreach ($screenItems as $screenItem) { - // check permissions - if (isset($screenItem['screenitemid']) && !isset($dbScreenItems[$screenItem['screenitemid']])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); - } - - if (!$this->isValidResourceType($screenItem['resourcetype'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect resource type provided for screen item.')); - } - - if (!isset($screenItem['resourceid'])) { - $screenItem['resourceid'] = null; - } - - // check resource id - switch ($screenItem['resourcetype']) { - case SCREEN_RESOURCE_HOST_INFO: - case SCREEN_RESOURCE_TRIGGER_INFO: - case SCREEN_RESOURCE_TRIGGER_OVERVIEW: - case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS: - case SCREEN_RESOURCE_DATA_OVERVIEW: - $overviewResources = [SCREEN_RESOURCE_TRIGGER_OVERVIEW, SCREEN_RESOURCE_DATA_OVERVIEW]; - if (in_array($screenItem['resourcetype'], $overviewResources)) { - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _( - 'No host group ID provided for screen element.' - )); - } - } - - if ($screenItem['resourceid']) { - $hostGroupsIds[$screenItem['resourceid']] = $screenItem['resourceid']; - } - break; - - case SCREEN_RESOURCE_HOST_TRIGGERS: - if ($screenItem['resourceid']) { - $hostIds[$screenItem['resourceid']] = $screenItem['resourceid']; - } - break; - - case SCREEN_RESOURCE_GRAPH: - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('No graph ID provided for screen element.')); - } - - $graphIds[$screenItem['resourceid']] = $screenItem['resourceid']; - break; - - case SCREEN_RESOURCE_LLD_GRAPH: - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _( - 'No graph prototype ID provided for screen element.' - )); - } - - $graphPrototypeIds[$screenItem['resourceid']] = $screenItem['resourceid']; - break; - - case SCREEN_RESOURCE_LLD_SIMPLE_GRAPH: - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _( - 'No item prototype ID provided for screen element.' - )); - } - - $itemPrototypeIds[$screenItem['resourceid']] = $screenItem['resourceid']; - break; - - case SCREEN_RESOURCE_SIMPLE_GRAPH: - case SCREEN_RESOURCE_PLAIN_TEXT: - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('No item ID provided for screen element.')); - } - - $itemIds[$screenItem['resourceid']] = $screenItem['resourceid']; - break; - - case SCREEN_RESOURCE_CLOCK: - if (isset($screenItem['style'])) { - if ($screenItem['style'] == TIME_TYPE_HOST) { - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('No item ID provided for screen element.')); - } - - $itemIds[$screenItem['resourceid']] = $screenItem['resourceid']; - } - elseif ($screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set resource ID for screen element.')); - } - } - break; - - case SCREEN_RESOURCE_MAP: - if (!$screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('No map ID provided for screen element.')); - } - - $mapIds[$screenItem['resourceid']] = $screenItem['resourceid']; - break; - - case SCREEN_RESOURCE_ACTIONS: - case SCREEN_RESOURCE_SERVER_INFO: - case SCREEN_RESOURCE_SYSTEM_STATUS: - case SCREEN_RESOURCE_URL: - if ($screenItem['resourceid']) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set resource ID for screen element.')); - } - break; - } - - // check url - if ($screenItem['resourcetype'] == SCREEN_RESOURCE_URL) { - if (!isset($screenItem['url']) || zbx_empty($screenItem['url'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('No URL provided for screen element.')); - } - } - - // check "Show lines" - if (isset($screenItem['elements'])) { - switch ($screenItem['resourcetype']) { - case SCREEN_RESOURCE_ACTIONS: - case SCREEN_RESOURCE_EVENTS: - case SCREEN_RESOURCE_HOSTGROUP_TRIGGERS: - case SCREEN_RESOURCE_HOST_TRIGGERS: - case SCREEN_RESOURCE_PLAIN_TEXT: - if ($screenItem['elements'] < 1 || $screenItem['elements'] > 100) { - self::exception(ZBX_API_ERROR_PARAMETERS, _s( - 'Incorrect value "%1$s" for "%2$s" field: must be between %3$s and %4$s.', - $screenItem['elements'], 'elements', 1, 100 - )); - } - break; - } - } - - // check 'max_columns' parameter for LLD screen resources: - // is set and valid for create method, and is valid for update method, if set - $dbScreenItem = isset($screenItem['screenitemid']) ? $dbScreenItems[$screenItem['screenitemid']] : null; - - $lldResources = [SCREEN_RESOURCE_LLD_GRAPH, SCREEN_RESOURCE_LLD_SIMPLE_GRAPH]; - if (in_array($screenItem['resourcetype'], $lldResources)) { - $set = isset($screenItem['max_columns']); - $valid = ($set && $this->isValidMaxColumns($screenItem['max_columns'])); - - $error = ($dbScreenItem ? ($set && !$valid) : (!$set || !$valid)); - if ($error) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect max columns provided for screen element.')); - } - } - - if (isset($validStyles[$screenItem['resourcetype']]) - && !in_array($screenItem['style'], $validStyles[$screenItem['resourcetype']])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect style provided for screen element.')); - } - } - - // check host groups - if ($hostGroupsIds) { - $dbHostGroups = API::HostGroup()->get([ - 'output' => ['groupid'], - 'groupids' => $hostGroupsIds, - 'preservekeys' => true - ]); - - foreach ($hostGroupsIds as $hostGroupsId) { - if (!isset($dbHostGroups[$hostGroupsId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect host group ID "%1$s" provided for screen element.', $hostGroupsId - )); - } - } - } - - // check hosts - if ($hostIds) { - $dbHosts = API::Host()->get([ - 'output' => ['hostid'], - 'hostids' => $hostIds, - 'preservekeys' => true - ]); - - foreach ($hostIds as $hostId) { - if (!isset($dbHosts[$hostId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect host ID "%1$s" provided for screen element.', $hostId - )); - } - } - } - - // check graphs - if ($graphIds) { - $dbGraphs = API::Graph()->get([ - 'output' => ['graphid'], - 'graphids' => $graphIds, - 'preservekeys' => true - ]); - - foreach ($graphIds as $graphId) { - if (!isset($dbGraphs[$graphId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect graph ID "%1$s" provided for screen element.', $graphId - )); - } - } - } - - // check graph prototypes - if ($graphPrototypeIds) { - $dbGraphPrototypes = API::GraphPrototype()->get([ - 'output' => ['graphid'], - 'graphids' => $graphPrototypeIds, - 'preservekeys' => true - ]); - - foreach ($graphPrototypeIds as $graphPrototypeId) { - if (!isset($dbGraphPrototypes[$graphPrototypeId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect graph prototype ID "%1$s" provided for screen element.', $graphPrototypeId - )); - } - } - } - - // check items - if ($itemIds) { - $dbItems = API::Item()->get([ - 'output' => ['itemid'], - 'itemids' => $itemIds, - 'preservekeys' => true, - 'webitems' => true - ]); - - foreach ($itemIds as $itemId) { - if (!isset($dbItems[$itemId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect item ID "%1$s" provided for screen element.', $itemId - )); - } - } - } - - // check item prototypes - if ($itemPrototypeIds) { - $dbItemPrototypes = API::ItemPrototype()->get([ - 'output' => ['itemid'], - 'itemids' => $itemPrototypeIds, - 'preservekeys' => true - ]); - - foreach ($itemPrototypeIds as $itemPrototypeId) { - if (!isset($dbItemPrototypes[$itemPrototypeId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect item prototype ID "%1$s" provided for screen element.', $itemPrototypeId - )); - } - } - } - - // check maps - if ($mapIds) { - $dbMaps = API::Map()->get([ - 'output' => ['sysmapid'], - 'sysmapids' => $mapIds, - 'preservekeys' => true - ]); - - foreach ($mapIds as $mapId) { - if (!isset($dbMaps[$mapId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect map ID "%1$s" provided for screen element.', $mapId - )); - } - } - } - - // check screens - if ($screenIds) { - $dbScreens = API::Screen()->get([ - 'output' => ['screenid'], - 'screenids' => $screenIds, - 'preservekeys' => true - ]); - - foreach ($screenIds as $screenId) { - if (!isset($dbScreens[$screenId])) { - self::exception(ZBX_API_ERROR_PERMISSIONS, _s( - 'Incorrect screen ID "%1$s" provided for screen element.', $screenId - )); - } - } - } - } - - /** - * Returns true if the given resource type is supported. - * - * @param int $resourceType - * - * @return bool - */ - protected function isValidResourceType($resourceType) { - return in_array($resourceType, self::$resourceTypes); - } - - /** - * Checks that the row and column spans are valid. - * - * @throws APIException if the any of the spans is not an integer or missing - * - * @param array $screenItem - * @param array $screen - */ - protected function checkSpans(array $screenItem, array $screen) { - if (isset($screenItem['rowspan'])) { - if (!zbx_is_int($screenItem['rowspan']) || $screenItem['rowspan'] < 1) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'Screen "%1$s" row span in cell X - %2$s Y - %3$s is incorrect.', - $screen['name'], - $screenItem['x'], - $screenItem['y'] - ) - ); - } - } - - if (isset($screenItem['colspan'])) { - if (!zbx_is_int($screenItem['colspan']) || $screenItem['colspan'] < 1) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'Screen "%1$s" column span in cell X - %2$s Y - %3$s is incorrect.', - $screen['name'], - $screenItem['x'], - $screenItem['y'] - ) - ); - } - } - } - - /** - * Checks that the row and column spans fit into the size of the screen. - * - * @throws APIException if the any of the spans is bigger then the free space on the screen - * - * @param array $screenItem - * @param array $dbScreenItems - * @param array $screen - */ - protected function checkSpansInBounds(array $screenItem, array $dbScreenItems, array $screen) { - if (!isset($screenItem['x'])) { - $screenItem['x'] = isset($screenItem['screenitemid']) - ? $dbScreenItems[$screenItem['screenitemid']]['x'] - : 0; - } - if (!isset($screenItem['y'])) { - $screenItem['y'] = isset($screenItem['screenitemid']) - ? $dbScreenItems[$screenItem['screenitemid']]['y'] - : 0; - } - - if (isset($screenItem['rowspan']) && isset($screen['vsize']) - && $screenItem['rowspan'] > $screen['vsize'] - $screenItem['y']) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'Screen "%1$s" row span in cell X - %2$s Y - %3$s is too big.', - $screen['name'], - $screenItem['x'], - $screenItem['y'] - ) - ); - } - - if (isset($screenItem['colspan']) && isset($screen['hsize']) - && $screenItem['colspan'] > $screen['hsize'] - $screenItem['x']) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'Screen "%1$s" column span in cell X - %2$s Y - %3$s is too big.', - $screen['name'], - $screenItem['x'], - $screenItem['y'] - ) - ); - } - } - - /** - * Check duplicates screen items in one cell. - * - * @param array $screen_items - * @param string $screen_items['screenid'] - * @param int $screen_items['x'] - * @param int $screen_items['y'] - * @param int $screen_items['colspan'] - * @param int $screen_items['rowspan'] - * @param array $screens - * @param string $screens['screenid'] - * @param string $screens['name'] - * - * @throws APIException if input is invalid. - */ - protected function checkDuplicateResourceInCell(array $screen_items, array $screens) { - $calculated = []; - - foreach ($screen_items as $screen_item) { - $screenid = $screen_item['screenid']; - $checked = [ - 'left' => $screen_item['x'], - 'top' => $screen_item['y'], - 'right' => $screen_item['x'] + $screen_item['colspan'] - 1, - 'bottom' => $screen_item['y'] + $screen_item['rowspan'] - 1 - ]; - if (!array_key_exists($screenid, $calculated)) { - $calculated[$screenid] = []; - } - - foreach ($calculated[$screenid] as $entry) { - $overlaps_x = ($checked['left'] <= $entry['right'] && $checked['right'] >= $entry['left']); - $overlaps_y = ($checked['top'] <= $entry['bottom'] && $checked['bottom'] >= $entry['top']); - if ($overlaps_x && $overlaps_y) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Screen "%1$s" cell X - %2$s Y - %3$s is already taken.', $screens[$screenid]['name'], - $checked['left'], $checked['top'] - ) - ); - } - } - - $calculated[$screenid][] = $checked; - } - } - - /** - * Checks that the row and column fit into the size of the screen. - * - * @throws APIException if the any of the coordinates is bigger then the free space on the screen - * - * @param array $screenItem - * @param array $screen - */ - protected function checkGridCoordinates(array $screenItem, array $screen) { - if (isset($screenItem['x']) && $screenItem['x'] > $screen['hsize'] - 1) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'The X coordinate of screen element located at X - %1$s and Y - %2$s of screen "%3$s" is too big.', - $screenItem['x'], - $screenItem['y'], - $screen['name'] - ) - ); - } - - if (isset($screenItem['y']) && $screenItem['y'] > $screen['vsize'] - 1) { - self::exception( - ZBX_API_ERROR_PARAMETERS, - _s( - 'The Y coordinate of screen element located at X - %1$s and Y - %2$s of screen "%3$s" is too big.', - $screenItem['x'], - $screenItem['y'], - $screen['name'] - ) - ); - } - } - - protected function applyQueryFilterOptions($tableName, $tableAlias, array $options, array $sqlParts) { - $sqlParts = parent::applyQueryFilterOptions($tableName, $tableAlias, $options, $sqlParts); - - // screens - if ($options['screenids'] !== null) { - zbx_value2array($options['screenids']); - $sqlParts['where'][] = dbConditionInt($this->fieldId('screenid'), $options['screenids']); - } - - return $sqlParts; - } - - /** - * Checks if value of $maxColumns valid in respect to SCREEN_SURROGATE_MAX_COLUMNS_MIN and - * SCREEN_SURROGATE_MAX_COLUMNS_MIN constants. Returns true if valid, false otherwise. - * - * @param integer $maxColumns - * - * @return bool - */ - protected function isValidMaxColumns($maxColumns) { - return ($maxColumns >= SCREEN_SURROGATE_MAX_COLUMNS_MIN && $maxColumns <= SCREEN_SURROGATE_MAX_COLUMNS_MAX); - } - - /** - * Validates URL fields for submitted screen items. - * - * @throws APIException for invalid URL - * - * @param array $screen_items Array of screen items. - */ - protected function validateItemsURL($screen_items) { - foreach ($screen_items as $screen_item) { - if ($screen_item['resourcetype'] == SCREEN_RESOURCE_URL && array_key_exists('url', $screen_item) - && !CHtmlUrlValidator::validate($screen_item['url'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Wrong value for url field.')); - } - } - } -} diff --git a/ui/include/classes/api/services/CUser.php b/ui/include/classes/api/services/CUser.php index bc1174f974c..3eaf2c82e0a 100644 --- a/ui/include/classes/api/services/CUser.php +++ b/ui/include/classes/api/services/CUser.php @@ -1198,21 +1198,6 @@ class CUser extends CApiService { ); } - // Check if deleted users have a screen. - $db_screens = API::Screen()->get([ - 'output' => ['name', 'userid'], - 'userids' => $userids, - 'limit' => 1 - ]); - - if ($db_screens) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('User "%1$s" is screen "%2$s" owner.', $db_users[$db_screens[0]['userid']]['alias'], - $db_screens[0]['name'] - ) - ); - } - // Check if deleted users have a slide show. $db_slideshows = DB::select('slideshows', [ 'output' => ['name', 'userid'], diff --git a/ui/tests/api_json/testAuditLog.php b/ui/tests/api_json/testAuditLog.php index e034c7c2368..b192c457779 100644 --- a/ui/tests/api_json/testAuditLog.php +++ b/ui/tests/api_json/testAuditLog.php @@ -779,8 +779,8 @@ class testAuditLog extends CAPITest { ], '$expected_result' => null, 'expected_error' => 'Invalid parameter "/filter/resourcetype/1": value must be one of 0, 2, 3, 4, 5,'. - ' 6, 7, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,'. - ' 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45.' + ' 6, 7, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,'. + ' 36, 37, 38, 39, 40, 41, 42, 43, 45.' ], // Filter validation - ip. [ |