diff options
Diffstat (limited to 'ui/include/classes/api/services/CHttpTest.php')
-rw-r--r-- | ui/include/classes/api/services/CHttpTest.php | 226 |
1 files changed, 165 insertions, 61 deletions
diff --git a/ui/include/classes/api/services/CHttpTest.php b/ui/include/classes/api/services/CHttpTest.php index 590695af7c2..ae43d1b3bab 100644 --- a/ui/include/classes/api/services/CHttpTest.php +++ b/ui/include/classes/api/services/CHttpTest.php @@ -345,7 +345,10 @@ class CHttpTest extends CApiService { } $this->checkAndAddUuid($httptests); - $this->checkHostPermissions(array_keys($names_by_hostid)); + + $this->checkHostsAndTemplates($httptests, $db_hosts, $db_templates); + self::addHostStatus($httptests, $db_hosts, $db_templates); + $this->checkDuplicates($names_by_hostid); $this->validateAuthParameters($httptests, __FUNCTION__); $this->validateSslParameters($httptests, __FUNCTION__); @@ -573,17 +576,25 @@ class CHttpTest extends CApiService { } /** - * Delete web scenario. - * * @param array $httptestids - * @param bool $nopermissions * * @return array */ - public function delete(array $httptestids, $nopermissions = false) { - // TODO: remove $nopermissions hack + public function delete(array $httptestids) { + $this->validateDelete($httptestids, $db_httptests); + self::deleteForce($db_httptests); + + return ['httptestids' => $httptestids]; + } + + /** + * @param array $httptestids + * @param array|null $db_httptests + */ + private function validateDelete(array $httptestids, ?array &$db_httptests): void { $api_input_rules = ['type' => API_IDS, 'flags' => API_NOT_EMPTY, 'uniq' => true]; + if (!CApiInputValidator::validate($api_input_rules, $httptestids, '/', $error)) { self::exception(ZBX_API_ERROR_PARAMETERS, $error); } @@ -595,24 +606,23 @@ class CHttpTest extends CApiService { 'preservekeys' => true ]); - if (!$nopermissions) { - foreach ($httptestids as $httptestid) { - if (!array_key_exists($httptestid, $db_httptests)) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _('No permissions to referred object or it does not exist!') - ); - } - - $db_httptest = $db_httptests[$httptestid]; + if (count($db_httptests) != count($httptestids)) { + self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); + } - if ($db_httptest['templateid'] != 0) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _s('Cannot delete templated web scenario "%1$s".', $db_httptest['name']) - ); - } + foreach ($httptestids as $httptestid) { + if ($db_httptests[$httptestid]['templateid'] != 0) { + self::exception(ZBX_API_ERROR_PARAMETERS, + _s('Cannot delete templated web scenario "%1$s".', $db_httptests[$httptestid]['name']) + ); } } + } + /** + * @param array $db_httptests + */ + public static function deleteForce(array $db_httptests): void { self::addInheritedHttptests($db_httptests); $del_httptestids = array_keys($db_httptests); @@ -628,12 +638,12 @@ class CHttpTest extends CApiService { ]); DB::delete('httptest', ['httptestid' => $del_httptestids]); - $this->addAuditBulk(CAudit::ACTION_DELETE, CAudit::RESOURCE_SCENARIO, $db_httptests); - - return ['httptestids' => $httptestids]; + self::addAuditLog(CAudit::ACTION_DELETE, CAudit::RESOURCE_SCENARIO, $db_httptests); } /** + * Add the inherited web scenarios of the given web scenarios to the given web scenario array. + * * @param array $db_httptests */ private static function addInheritedHttptests(array &$db_httptests): void { @@ -659,20 +669,26 @@ class CHttpTest extends CApiService { } /** + * Delete items, which would remain without web scenarios after the given web scenarios deletion. + * * @param array $del_httptestids */ private static function deleteAffectedItems(array $del_httptestids): void { $db_items = DBfetchArrayAssoc(DBselect( - 'SELECT hti.itemid'. + 'SELECT hti.itemid,i.name'. ' FROM httptestitem hti,items i'. ' WHERE hti.itemid=i.itemid'. ' AND '.dbConditionId('hti.httptestid', $del_httptestids) ), 'itemid'); - CItemManager::delete(array_keys($db_items)); + CItem::addInheritedItems($db_items); + DB::delete('httptestitem', ['itemid' => array_keys($db_items)]); + CItem::deleteForce($db_items); } /** + * Delete steps of the given web scenarios. + * * @param array $del_httptestids */ private static function deleteAffectedSteps(array $del_httptestids): void { @@ -688,50 +704,77 @@ class CHttpTest extends CApiService { } /** + * Delete items of the given web scenario steps. + * * @param array $del_stepids */ - public static function deleteAffectedStepItems(array $del_stepids): void { + private static function deleteAffectedStepItems(array $del_stepids): void { $db_items = DBfetchArrayAssoc(DBselect( - 'SELECT hsi.itemid'. + 'SELECT hsi.itemid,i.name'. ' FROM httpstepitem hsi,items i'. ' WHERE hsi.itemid=i.itemid'. ' AND '.dbConditionId('hsi.httpstepid', $del_stepids) ), 'itemid'); - CItemManager::delete(array_keys($db_items)); + CItem::addInheritedItems($db_items); + DB::delete('httpstepitem', ['itemid' => array_keys($db_items)]); + CItem::deleteForce($db_items); } /** - * Checks if the current user has access to the given hosts and templates. + * Check that host IDs of given web scenarios are valid. + * If host IDs are valid, $db_hosts and $db_templates parameters will be filled with found hosts and templates. * - * @param array $hostids an array of host or template IDs + * @param array $httptests + * @param array|null $db_hosts + * @param array|null $db_templates * - * @throws APIException if the user doesn't have write permissions for the given hosts. + * @throws APIException */ - private function checkHostPermissions(array $hostids) { - if ($hostids) { - $count = API::Host()->get([ - 'countOutput' => true, - 'hostids' => $hostids, - 'editable' => true - ]); + protected static function checkHostsAndTemplates(array $httptests, array &$db_hosts = null, + array &$db_templates = null): void { + $hostids = array_unique(array_column($httptests, 'hostid')); - if ($count == count($hostids)) { - return; - } + $db_templates = API::Template()->get([ + 'output' => [], + 'templateids' => $hostids, + 'editable' => true, + 'preservekeys' => true + ]); - $count += API::Template()->get([ - 'countOutput' => true, - 'templateids' => $hostids, - 'editable' => true - ]); + $_hostids = array_diff($hostids, array_keys($db_templates)); - if ($count != count($hostids)) { - self::exception(ZBX_API_ERROR_PERMISSIONS, - _('No permissions to referred object or it does not exist!') - ); + $db_hosts = $_hostids + ? API::Host()->get([ + 'output' => ['status'], + 'hostids' => $_hostids, + 'editable' => true, + 'preservekeys' => true + ]) + : []; + + if (count($db_templates) + count($db_hosts) != count($hostids)) { + self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); + } + } + + /** + * Add host_status property to given web scenarios in accordance of given hosts and templates statuses. + * + * @param array $httptests + * @param array $db_hosts + * @param array $db_templates + */ + protected static function addHostStatus(array &$httptests, array $db_hosts, array $db_templates): void { + foreach ($httptests as &$httptest) { + if (array_key_exists($httptest['hostid'], $db_templates)) { + $httptest['host_status'] = HOST_STATUS_TEMPLATE; + } + else { + $httptest['host_status'] = $db_hosts[$httptest['hostid']]['status']; } } + unset($httptest); } /** @@ -774,23 +817,27 @@ class CHttpTest extends CApiService { $db_httptest = $db_httptests[$httptest['httptestid']]; - if ($db_httptest['templateid'] != 0) { - if (count($httptest['steps']) != count($db_httptest['steps'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect templated web scenario step count.')); - } + if ($db_httptest['templateid'] != 0 && count($httptest['steps']) != count($db_httptest['steps'])) { + self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect templated web scenario step count.')); + } - foreach ($httptest['steps'] as $httpstep) { - if (!array_key_exists('httpstepid', $httpstep)) { + foreach ($httptest['steps'] as $step) { + if (!array_key_exists('httpstepid', $step)) { + if ($db_httptest['templateid'] == 0) { + continue; + } + else { self::exception(ZBX_API_ERROR_PARAMETERS, _s( 'Cannot update step for a templated web scenario "%1$s": %2$s.', $httptest['name'], _s('the parameter "%1$s" is missing', 'httpstepid') )); } - elseif (!array_key_exists($httpstep['httpstepid'], $db_httptest['steps'])) { - self::exception(ZBX_API_ERROR_PARAMETERS, - _('No permissions to referred object or it does not exist!') - ); - } + } + + if (!array_key_exists($step['httpstepid'], $db_httptest['steps'])) { + self::exception(ZBX_API_ERROR_PARAMETERS, + _('No permissions to referred object or it does not exist!') + ); } } } @@ -1128,4 +1175,61 @@ class CHttpTest extends CApiService { } unset($httptest); } + + /** + * @param array $templateids + * @param array|null $hostids + */ + public static function unlinkTemplateObjects(array $templateids, array $hostids = null): void { + $hostids_condition = $hostids ? ' AND '.dbConditionId('hht.hostid', $hostids) : ''; + + $result = DBselect( + 'SELECT hht.httptestid,hht.name,h.status AS host_status'. + ' FROM httptest ht,httptest hht,hosts h'. + ' WHERE ht.httptestid=hht.templateid'. + ' AND hht.hostid=h.hostid'. + ' AND '.dbConditionId('ht.hostid', $templateids). + $hostids_condition + ); + + $httptests = []; + + while ($row = DBfetch($result)) { + $httptest = [ + 'httptestid' => $row['httptestid'], + 'name' => $row['name'], + 'templateid' => 0 + ]; + + if ($row['host_status'] == HOST_STATUS_TEMPLATE) { + $httptest += ['uuid' => generateUuidV4()]; + } + + $httptests[] = $httptest; + } + + if ($httptests) { + Manager::HttpTest()->update($httptests); + } + } + + /** + * @param array $templateids + * @param array|null $hostids + */ + public static function clearTemplateObjects(array $templateids, array $hostids = null): void { + $hostids_condition = $hostids ? ' AND '.dbConditionId('hht.hostid', $hostids) : ''; + + $db_httptests = DBfetchArrayAssoc(DBselect( + 'SELECT hht.httptestid,hht.name'. + ' FROM httptest ht,httptest hht'. + ' WHERE ht.httptestid=hht.templateid'. + ' AND '.dbConditionId('ht.hostid', $templateids). + $hostids_condition + ), 'httptestid'); + + if ($db_httptests) { + self::deleteForce($db_httptests); + } + } } |