2010-07-15 Miguel de Dios <miguel.dedios@artica.es>

* 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
This commit is contained in:
mdtrooper 2010-07-15 11:25:38 +00:00
parent 16f3d39f87
commit 1446aed5e3
5 changed files with 308 additions and 33 deletions

View File

@ -1,3 +1,24 @@
2010-07-15 Miguel de Dios <miguel.dedios@artica.es>
* 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 <miguel.dedios@artica.es>
* pandoradb.sql: added in the table tgrupo the column 'propagrate' for to

View File

@ -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 "<h3 class='error'>".__('There was a problem loading group')."</h3>";
echo "</table>";
@ -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] .= ' <span id="parent_preview">';
if ($id_parent) {
echo '<img src="images/groups_small/'.get_group_icon ($id_parent).'.png" />';
@ -85,8 +88,11 @@ echo'</span>';
$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 '<form name="grupo" method="post" action="index.php?sec=gagente&sec2=godmode/groups/group_list">';
print_table ($table);

View File

@ -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 "<h3 class='suc'>".__('Group successfully updated')."</h3>";
@ -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('&nbsp;&nbsp;&nbsp;&nbsp;', $group['deep']);
$data[0] = print_group_icon($id_group, true);
$data[1] = '<strong><a href="index.php?sec=gagente&sec2=godmode/groups/configure_group&id_group='.$id_group.'">'.$group_name.'</a></strong>';
$data[2] = get_group_name ($group["parent"]);
$data[3] = $group['disabled'] ? __('Disabled') : __('Enabled');
$data[4] = '<a href="index.php?sec=gagente&sec2=godmode/groups/group_list&id_group='.$id_group.'&delete_group=1" onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;"><img border="0" src="images/cross.png"></a>';
if ($group['hash_branch']) {
$data[0] = '<strong>'.$tabulation . ' ' .
'<a href="javascript: showBranch(' . $group['id_grupo'] . ', ' . $group['parent'] . ');" title="' . __('Show branch children') . '"><span class="symbol_' . $group['id_grupo'] . ' ' . $symbolBranchs . '">+</span> '. $group['nombre'].'</a></strong>';
}
else {
$data[0] = '<strong>'.$tabulation . ' '. $group['nombre'].'</strong>';
}
$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] = '<a href="index.php?sec=gagente&sec2=godmode/groups/configure_group&id_group='.$group['id_grupo'].'"><img border="0" src="images/config.png" alt="' . __('Edit') . '" title="' . __('Edit') . '" /></a>';
$data[3] .= '<a href="index.php?sec=gagente&sec2=godmode/groups/group_list&id_group='.$id_group.'&delete_group=1" onClick="if (!confirm(\' '.__('Are you sure?').'\')) return false;"><img alt="' . __('Delete') . '" alt="' . __('Delete') . '" border="0" src="images/cross.png"></a>';
}
array_push ($table->data, $data);
$iterator++;
}
?>
<script type="text/javascript">
function showBranch(parent) {
display = $('.parent_' + parent).css('display');
if (display != 'none') {
$('.symbol_' + parent).html('+');
$('.parent_' + parent).css('display', 'none');
//Close the child branch too
$('.branch_' + parent).css('display', 'none');
$('.symbol_branch_' + parent).html('+');
}
else {
$('.symbol_' + parent).html('-');
$('.parent_' + parent).css('display', '');
}
}
</script>
<?php
print_table ($table);
echo '<form method="post" action="index.php?sec=gagente&sec2=godmode/groups/configure_group">';

View File

@ -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;
}
?>

View File

@ -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."`");
}