From 1446aed5e3e1e163b6af638f3b78feb12f6b633d Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Thu, 15 Jul 2010 11:25:38 +0000 Subject: [PATCH] 2010-07-15 Miguel de Dios * include/functions.php: added function "array_key_to_offset" to resolve the offset in associative array. * include/functions_db.php: added function "get_childrens" for get the childrens of a group, "get_parents" for get the parents of a group (with the flag propagate or not as you want). In function "check_acl" added the method to scan groups that propagate flag in the branches. Added functions "get_user_groups_tree_recursive" and "get_user_groups_tree" for extract the groups as treefied list. Added in the function "get_db_all_rows_in_table" added the parameter $order to get with ASC or DESC order the rows, by default is ASC as mySQL. * godmode/groups/configure_group.php: added the field of propagate to update or creation a group. * godmode/groups/group_list.php: changed for the group trees the source code for to show the table as dinamic table that if the row have branch, you can click and desplegate children rows. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@3008 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 21 +++ .../godmode/groups/configure_group.php | 12 +- pandora_console/godmode/groups/group_list.php | 119 ++++++++++-- pandora_console/include/functions.php | 14 ++ pandora_console/include/functions_db.php | 175 ++++++++++++++++-- 5 files changed, 308 insertions(+), 33 deletions(-) diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 48281ca0e9..f1f8d99065 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,24 @@ +2010-07-15 Miguel de Dios + + * include/functions.php: added function "array_key_to_offset" to resolve + the offset in associative array. + + * include/functions_db.php: added function "get_childrens" for get the + childrens of a group, "get_parents" for get the parents of a group (with the + flag propagate or not as you want). In function "check_acl" added the method + to scan groups that propagate flag in the branches. Added functions + "get_user_groups_tree_recursive" and "get_user_groups_tree" for extract the + groups as treefied list. Added in the function "get_db_all_rows_in_table" + added the parameter $order to get with ASC or DESC order the rows, by + default is ASC as mySQL. + + * godmode/groups/configure_group.php: added the field of propagate to update + or creation a group. + + * godmode/groups/group_list.php: changed for the group trees the source code + for to show the table as dinamic table that if the row have branch, you can + click and desplegate children rows. + 2010-07-13 Miguel de Dios * pandoradb.sql: added in the table tgrupo the column 'propagrate' for to diff --git a/pandora_console/godmode/groups/configure_group.php b/pandora_console/godmode/groups/configure_group.php index ff8c7e10b9..df3db80b30 100644 --- a/pandora_console/godmode/groups/configure_group.php +++ b/pandora_console/godmode/groups/configure_group.php @@ -30,6 +30,7 @@ $name = ""; $id_parent = 0; $alerts_disabled = 0; $custom_id = ""; +$propagate = 0; $create_group = (bool) get_parameter ('create_group'); $id_group = (int) get_parameter ('id_group'); @@ -42,6 +43,7 @@ if ($id_group) { $alerts_disabled = $group["disabled"]; $id_parent = $group["parent"]; $custom_id = $group["custom_id"]; + $propagate = $group["propagate"]; } else { echo "

".__('There was a problem loading group')."

"; echo ""; @@ -75,7 +77,8 @@ $table->data[2][0] = __('Parent'); $sql = 'SELECT id_grupo, nombre FROM tgrupo '; if ($id_group) $sql .= sprintf ('WHERE id_grupo != %d', $id_group); -$table->data[2][1] = print_select_from_sql ($sql, 'id_parent', $id_parent, '', 'None', 0, true); +$groups = get_user_groups(); +$table->data[2][1] = print_select($groups, 'id_parent', 0, '', '', '', true); $table->data[2][1] .= ' '; if ($id_parent) { echo ''; @@ -85,8 +88,11 @@ echo''; $table->data[3][0] = __('Alerts'); $table->data[3][1] = print_checkbox ('alerts_enabled', 1, ! $alerts_disabled, true); -$table->data[4][0] = __('Custom ID'); -$table->data[4][1] = print_input_text ('custom_id', $custom_id, '', 16, 255, true); +$table->data[4][0] = __('Propagate ACL') . print_help_tip (__("Propagate the same ACL security into the child subgroups."), true); +$table->data[4][1] = print_checkbox('propagate', 1, $propagate, true); + +$table->data[5][0] = __('Custom ID'); +$table->data[5][1] = print_input_text ('custom_id', $custom_id, '', 16, 255, true); echo '
'; print_table ($table); diff --git a/pandora_console/godmode/groups/group_list.php b/pandora_console/godmode/groups/group_list.php index 26d4bd8a8f..ebc3ba5926 100644 --- a/pandora_console/godmode/groups/group_list.php +++ b/pandora_console/godmode/groups/group_list.php @@ -108,11 +108,12 @@ if ($update_group) { $id_parent = (int) get_parameter ('id_parent'); $alerts_enabled = (bool) get_parameter ('alerts_enabled'); $custom_id = (string) get_parameter ('custom_id'); + $propagate = (bool) get_parameter('propagate'); $sql = sprintf ('UPDATE tgrupo SET nombre = "%s", - icon = "%s", disabled = %d, parent = %d, custom_id = "%s" + icon = "%s", disabled = %d, parent = %d, custom_id = "%s", propagate = %d WHERE id_grupo = %d', - $name, substr ($icon, 0, -4), !$alerts_enabled, $id_parent, $custom_id, $id_group); + $name, substr ($icon, 0, -4), !$alerts_enabled, $id_parent, $custom_id, $propagate, $id_group); $result = process_sql ($sql); if ($result !== false) { echo "

".__('Group successfully updated')."

"; @@ -150,32 +151,116 @@ if ($delete_group) { $table->width = '65%'; $table->head = array (); -$table->head[0] = __('Icon'); -$table->head[1] = __('Name'); -$table->head[2] = __('Parent'); -$table->head[3] = __('Alerts'); -$table->head[4] = __('Delete'); +$table->head[0] = __('Name'); +$table->head[1] = __('Icon'); +$table->head[2] = __('Alerts'); +$table->head[3] = __('Actions'); $table->align = array (); -$table->align[4] = 'center'; +$table->align[3] = 'center'; $table->data = array (); -$groups = get_user_groups ($config['id_user'], "AR", false); +$groups = get_user_groups_tree ($config['id_user'], "AR", false); +$iterator = 0; -foreach ($groups as $id_group => $group_name) { - $data = array (); +foreach ($groups as $id_group => $group) { + if ($group['deep'] == 0) { + $table->rowstyle[$iterator] = ''; + } + else { + $table->rowstyle[$iterator] = 'display: none;'; + } - $group = get_db_row ('tgrupo', 'id_grupo', $id_group); + $symbolBranchs = ''; + if ($group['id_grupo'] != 0) { + + //Make a list of parents this group + $end = false; + $unloop = true; + $parents = null; + $parents[] = $group['parent']; + while (!$end) { + $lastParent = end($parents); + if ($lastParent == 0) { + $end = true; + } + else { + $unloop = true; + foreach ($groups as $id => $node) { + if ($node['id_grupo'] == 0) { + continue; + } + if ($node['id_grupo'] == $lastParent) { + array_push($parents, $node['parent']); + $unloop = false; + } + } + + //For exit of infinite loop + if ($unloop) { + break; + } + } + } + + $table->rowclass[$iterator] = 'parent_' . $group['parent']; + + //Print the branch classes (for close a branch with child branch in the + //javascript) of this parent as example: + // + // the tree (0(1,2(4,5),3)) + // for the group 4 have the style "parent_4 branch_0 branch_2" + if (!empty($parents)) { + foreach ($parents as $idParent) { + $table->rowclass[$iterator] .= ' branch_' . $idParent; + $symbolBranchs .= ' symbol_branch_' . $idParent; + } + } + } + $tabulation = str_repeat('    ', $group['deep']); - $data[0] = print_group_icon($id_group, true); - $data[1] = ''.$group_name.''; - $data[2] = get_group_name ($group["parent"]); - $data[3] = $group['disabled'] ? __('Disabled') : __('Enabled'); - $data[4] = ''; + if ($group['hash_branch']) { + $data[0] = ''.$tabulation . ' ' . + '+ '. $group['nombre'].''; + } + else { + $data[0] = ''.$tabulation . ' '. $group['nombre'].''; + } + $data[1] = print_group_icon($group['id_grupo'], true); + $data[2] = $group['disabled'] ? __('Disabled') : __('Enabled'); + if ($group['id_grupo'] == 0) { + $data[3] = ''; + } + else { + $data[3] = '' . __('Edit') . ''; + $data[3] .= '' . __('Delete') . ''; + } array_push ($table->data, $data); + $iterator++; } +?> + +'; diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index bf86888164..ad7709edee 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -1011,4 +1011,18 @@ function return_graphtype ($id_module_type){ return "sparse"; } + +/** + * Translate the key in assoc array to numeric offset. + * + * @param array $array The array to return the offset. + * @param mixed $key The key to translate to offset. + * + * @return mixed The offset or false is fail. + */ +function array_key_to_offset($array, $key) { + $offset = array_search($key, array_keys($array)); + + return $offset; +} ?> diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index c55856ba53..e6e8aecea9 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -50,6 +50,57 @@ function check_login () { include ($config["homedir"]."/general/noaccess.php"); exit; } + +/** + * Return a array of id_group of childrens (to branches down) + * + * @param integer $parent The id_group parent to search the childrens. + * @param array $groups The groups, its for optimize the querys to DB. + */ +function get_childrens($parent, $groups = null) { + if (empty($groups)) { + $groups = get_db_all_rows_in_table('tgrupo'); + } + + $return = array(); + + foreach ($groups as $key => $group) { + if ($group['id_grupo'] == 0) { + continue; + } + if ($group['parent'] == $parent) { + $return = $return + array($group['id_grupo'] => $group) + get_childrens($group['id_grupo'], $groups); + } + } + + return $return; +} + +/** + * Return a array of id_group of parents (to roots up). + * + * @param integer $parent The id_group parent to search the parent. + * @param boolean $onlyPropagate Flag to search only parents that true to propagate. + * @param array $groups The groups, its for optimize the querys to DB. + */ +function get_parents($parent, $onlyPropagate = false, $groups = null) { + if (empty($groups)) { + $groups = get_db_all_rows_in_table('tgrupo'); + } + + $return = array(); + + foreach ($groups as $key => $group) { + if ($group['id_grupo'] == 0) { + continue; + } + if (($group['id_grupo'] == $parent) && ($group['propagate'] || !$onlyPropagate)) { + $return = $return + array($group['id_grupo'] => $group) + get_parents($group['parent'], $groups); + } + } + + return $return; +} /** * Check access privileges to resources @@ -82,14 +133,43 @@ function check_acl ($id_user, $id_group, $access) { } else { $id_group = (int) $id_group; } - + + $parents_id = array($id_group); + if ($id_group != 0) { + $group = get_db_row_filter('tgrupo', array('id_grupo' => $id_group)); + $parents = get_parents($group['parent'], true); + + foreach ($parents as $parent) { + $parents_id[] = $parent['id_grupo']; + } + } + else { + $parents_id = array(); + } + //Joined multiple queries into one. That saves on the query overhead and query cache. if ($id_group == 0) { - $query = sprintf("SELECT tperfil.incident_view,tperfil.incident_edit,tperfil.incident_management,tperfil.agent_view,tperfil.agent_edit,tperfil.alert_edit,tperfil.alert_management,tperfil.pandora_management,tperfil.db_management,tperfil.user_management FROM tusuario_perfil,tperfil WHERE tusuario_perfil.id_perfil = tperfil.id_perfil AND tusuario_perfil.id_usuario = '%s'", $id_user); + $query = sprintf("SELECT tperfil.incident_view, tperfil.incident_edit, + tperfil.incident_management, tperfil.agent_view, + tperfil.agent_edit, tperfil.alert_edit, + tperfil.alert_management, tperfil.pandora_management, + tperfil.db_management, tperfil.user_management + FROM tusuario_perfil, tperfil + WHERE tusuario_perfil.id_perfil = tperfil.id_perfil + AND tusuario_perfil.id_usuario = '%s'", $id_user); //GroupID = 0, group id doesnt matter (use with caution!) - } else { - $query = sprintf("SELECT tperfil.incident_view,tperfil.incident_edit,tperfil.incident_management,tperfil.agent_view,tperfil.agent_edit,tperfil.alert_edit,tperfil.alert_management,tperfil.pandora_management,tperfil.db_management,tperfil.user_management FROM tusuario_perfil,tperfil WHERE tusuario_perfil.id_perfil = tperfil.id_perfil - AND tusuario_perfil.id_usuario = '%s' AND (tusuario_perfil.id_grupo = %d OR tusuario_perfil.id_grupo = 0)", $id_user, $id_group); + } + else { + $query = sprintf("SELECT tperfil.incident_view, tperfil.incident_edit, + tperfil.incident_management, tperfil.agent_view, + tperfil.agent_edit, tperfil.alert_edit, + tperfil.alert_management, tperfil.pandora_management, + tperfil.db_management, tperfil.user_management + FROM tusuario_perfil, tperfil + WHERE tusuario_perfil.id_perfil = tperfil.id_perfil + AND tusuario_perfil.id_usuario = '%s' + AND (tusuario_perfil.id_grupo IN (%s) + OR tusuario_perfil.id_grupo = 0)", $id_user, implode(', ', $parents_id)); } $rowdup = get_db_all_rows_sql ($query); @@ -1399,10 +1479,11 @@ function get_all_model_groups () { * @param string User id * @param string The privilege to evaluate * @param boolean $returnAllGroup Flag the return group, by default true. + * @param boolean $returnAllColumns Flag to return all columns of groups. * * @return array A list of the groups the user has certain privileges. */ -function get_user_groups ($id_user = false, $privilege = "AR", $returnAllGroup = true) { +function get_user_groups ($id_user = false, $privilege = "AR", $returnAllGroup = true, $returnAllColumns = false) { if (empty ($id_user)) { global $config; $id_user = $config['id_user']; @@ -1414,17 +1495,84 @@ function get_user_groups ($id_user = false, $privilege = "AR", $returnAllGroup = if (!$groups) return $user_groups; - if ($returnAllGroup) //All group - $user_groups[0] = "All"; - + if ($returnAllGroup) { //All group + if ($returnAllColumns) { + $groups[0] = array('id_grupo' => 0, 'nombre' => __('All'), + 'icon' => 'world', 'parent' => 0, 'disabled' => 0, + 'custom_id' => null, 'propagate' => 0); + } + else { + $groups[0] = array('id_grupo' => 0, 'nombre' => __("All")); + } + } + foreach ($groups as $group) { - if (give_acl ($id_user, $group["id_grupo"], $privilege)) - $user_groups[$group['id_grupo']] = $group['nombre']; + if (give_acl ($id_user, $group["id_grupo"], $privilege)) { + if ($returnAllColumns) { + $user_groups[$group['id_grupo']] = $group; + } + else { + $user_groups[$group['id_grupo']] = $group['nombre']; + } + } } return $user_groups; } +/** + * Make with a list of groups a treefied list of groups. + * + * @param array $groups The list of groups to create the treefield list. + * @param integer $parent The id_group of parent actual scan branch. + * @param integer $deep The level of profundity in the branch. + * + * @return array The treefield list of groups. + */ +function get_user_groups_tree_recursive($groups, $parent = 0, $deep = 0) { + $return = array(); + + foreach ($groups as $key => $group) { + if (($key == 0) && ($parent == 0)) { //When the groups is the all group + $group['deep'] = $deep; + $group['hash_branch'] = true; + $deep ++; + $return = $return + array($key => $group); + } + else if ($group['parent'] == $parent) { + $group['deep'] = $deep; + $branch = get_user_groups_tree_recursive($groups, $key, $deep + 1); + if (empty($branch)) { + $group['hash_branch'] = false; + } + else { + $group['hash_branch'] = true; + } + $return = $return + array($key => $group) + $branch; + } + } + + return $return; +} + +/** + * Get all the groups a user has reading privileges. Version for tree groups. + * + * @param string User id + * @param string The privilege to evaluate + * @param boolean $returnAllGroup Flag the return group, by default true. + * @param boolean $returnAllColumns Flag to return all columns of groups. + * + * @return array A treefield list of the groups the user has certain privileges. + */ +function get_user_groups_tree($id_user = false, $privilege = "AR", $returnAllGroup = true) { + $user_groups = get_user_groups (false, "AR", true, true); + + $user_groups_tree = get_user_groups_tree_recursive($user_groups); + + return $user_groups_tree; +} + /** * Get the first group of an user. * @@ -2156,12 +2304,13 @@ function process_sql ($sql, $rettype = "affected_rows", $dbconnection = '', $cac * * @param string Database table name. * @param string Field to order by. + * @param string $order The type of order, by default 'ASC'. * * @return mixed A matrix with all the values in the table */ -function get_db_all_rows_in_table ($table, $order_field = "") { +function get_db_all_rows_in_table ($table, $order_field = "", $order = 'ASC') { if ($order_field != "") { - return get_db_all_rows_sql ("SELECT * FROM `".$table."` ORDER BY ".$order_field); + return get_db_all_rows_sql ("SELECT * FROM `".$table."` ORDER BY ".$order_field . " " . $order); } else { return get_db_all_rows_sql ("SELECT * FROM `".$table."`"); }