added check to group deletion

This commit is contained in:
alejandro.campos@artica.es 2024-02-29 09:33:05 +01:00
parent a5592eccc5
commit ead051073b
4 changed files with 109 additions and 28 deletions

View File

@ -727,6 +727,11 @@ if ($is_management_allowed === true
['id_grupo' => $id_group]
);
$result_user_profile = db_process_sql_delete(
'tusuario_perfil',
['id_grupo' => $id_group]
);
if ($result && (!$usedGroup['return'])) {
db_process_sql_delete(
'tfavmenu_user',
@ -1132,7 +1137,7 @@ if ($tab == 'tree') {
$confirm_message = __('The child groups will be updated to use the parent id of the deleted group').'. '.$confirm_message;
}
$table->data[$key][6] .= '<a href="'.$url_delete.'" onClick="if (!confirm(\' '.$confirm_message.'\')) return false;">'.html_print_image(
$table->data[$key][6] .= '<a href="'.$url_delete.'" onClick="event.preventDefault(); return preprocessDeletion('.$group['id_grupo'].', \''.$url_delete.'\',\''.$confirm_message.'\');">'.html_print_image(
'images/delete.svg',
true,
[
@ -1218,7 +1223,6 @@ $tab = 'group_edition';
});
$('#button-filter').on('click', function(event) {
console.log('here');
event.preventDefault();
load_tree(show_full_hirearchy, show_not_init_agents, show_not_init_modules);
@ -1318,5 +1322,35 @@ $tab = 'group_edition';
});
}
function preprocessDeletion(group_id, delete_URL, confirm_text) {
var parameters = {};
parameters['page'] = 'include/ajax/group';
parameters['method'] = 'checkGroupIsLinkedToElement';
parameters['group_id'] = group_id;
parameters['table_name'] = 'tusuario_perfil';
parameters['field_name'] = 'id_grupo';
$.ajax({
type: "POST",
url: "<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
data: parameters,
success: function(data) {
if (data.result == '1') {
confirmDialog({
title: '<?php echo __('Are you sure?'); ?>',
message: '<?php echo __('There are user profiles assigned to this group which will be deleted. Note that a user with no associated profiles will not be able to log in. Please ensure you want to proceed with the deletion.'); ?>',
onAccept: function() {
window.location.assign(delete_URL);
}
});
} else {
if (!confirm(confirm_text)) return false;
}
},
dataType: "json"
});
return true;
}
</script>

View File

@ -6938,3 +6938,45 @@ function get_defined_translation($string)
}
}
}
/**
* General utility to check if at least one element in an array meets a certain criteria defined by passed function.
*
* @param array $array Array to be checked.
* @param callable $fn Checking function (must accept one argument => array item to be evaluated and returns
* true/false depending on whether or not the condition was fulfilled).
*
* @return boolean
*/
function array_some(array $array, callable $fn)
{
foreach ($array as $value) {
if ($fn($value) === true) {
return true;
}
}
return false;
}
/**
* General utility to check if every element in an array meets a certain criteria defined by passed function.
*
* @param array $array Array to be checked.
* @param callable $fn Checking function (must accept one argument => array item to be evaluated and returns
* true/false depending on whether or not the condition was fulfilled).
*
* @return boolean
*/
function array_every(array $array, callable $fn)
{
foreach ($array as $value) {
if ($fn($value) === false) {
return false;
}
}
return true;
}

View File

@ -267,32 +267,6 @@ function groups_check_used($idGroup)
$return['tables'][] = __('SNMP alerts');
}
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
$numRows = db_get_num_rows(
'SELECT *
FROM tusuario_perfil WHERE id_grupo = '.$idGroup.';'
);
break;
case 'oracle':
$numRows = db_get_num_rows(
'SELECT *
FROM tusuario_perfil WHERE id_grupo = '.$idGroup
);
break;
default:
// Ignore.
break;
}
if ($numRows > 0) {
$return['return'] = true;
$return['tables'][] = __('User profile');
}
$hookEnterprise = enterprise_include_once('include/functions_groups.php');
if ($hookEnterprise !== ENTERPRISE_NOT_HOOK) {
$returnEnterprise = enterprise_hook('groups_check_used_group_enterprise', [$idGroup]);

View File

@ -50,6 +50,7 @@ class Group extends Entity
'loadInfoAgent',
'getAgentsByGroup',
'getGroupsName',
'checkGroupIsLinkedToElement',
];
@ -777,5 +778,35 @@ class Group extends Entity
exit;
}
/**
* Check whether group is linked to a database element (needed for ajax check).
*
* @return void
*/
public static function checkGroupIsLinkedToElement()
{
$group_id = get_parameter('group_id', null);
$table_name = get_parameter('table_name', null);
$field_name = get_parameter('field_name', null);
if (count(array_filter([$group_id, $table_name, $field_name])) < 3) {
$result['result'] = 0;
} else {
$sql = sprintf(
'SELECT * FROM %s WHERE %s = %s',
$table_name,
$field_name,
$group_id
);
$count = db_get_num_rows($sql);
$result['result'] = (int) ($count > 0);
}
echo json_encode($result);
exit;
}
}