grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026. -------------------------------------------------------------------------- */ /** @file * @brief */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Group_User Class * * Relation between Group and User **/ class Group_User extends CommonDBRelation{ // From CommonDBRelation static $itemtype_1 = 'User'; static $items_id_1 = 'users_id'; static $itemtype_2 = 'Group'; static $items_id_2 = 'groups_id'; /** * @param $users_id * @param $condition (default '') **/ static function getUserGroups($users_id, $condition='') { global $DB; $groups = array(); $query = "SELECT `glpi_groups`.*, `glpi_groups_users`.`id` AS IDD, `glpi_groups_users`.`id` AS linkID, `glpi_groups_users`.`is_dynamic` AS is_dynamic, `glpi_groups_users`.`is_manager` AS is_manager, `glpi_groups_users`.`is_userdelegate` AS is_userdelegate FROM `glpi_groups_users` LEFT JOIN `glpi_groups` ON (`glpi_groups`.`id` = `glpi_groups_users`.`groups_id`) WHERE `glpi_groups_users`.`users_id` = '$users_id' "; if (!empty($condition)) { $query .= " AND $condition "; } $query.=" ORDER BY `glpi_groups`.`name`"; foreach ($DB->request($query) as $data) { $groups[] = $data; } return $groups; } /** * @since version 0.84 * * @param $groups_id * @param $condition (default '') **/ static function getGroupUsers($groups_id, $condition='') { global $DB; $users = array(); $query = "SELECT `glpi_users`.*, `glpi_groups_users`.`id` AS IDD, `glpi_groups_users`.`id` AS linkID, `glpi_groups_users`.`is_dynamic` AS is_dynamic, `glpi_groups_users`.`is_manager` AS is_manager, `glpi_groups_users`.`is_userdelegate` AS is_userdelegate FROM `glpi_groups_users` LEFT JOIN `glpi_users` ON (`glpi_users`.`id` = `glpi_groups_users`.`users_id`) WHERE `glpi_groups_users`.`groups_id` = '$groups_id'"; if (!empty($condition)) { $query .= " AND $condition "; } $query .= "ORDER BY `glpi_users`.`name`"; foreach ($DB->request($query) as $data) { $users[] = $data; } return $users; } /** Show groups of a user * * @param $user User object **/ static function showForUser(User $user) { global $CFG_GLPI; $ID = $user->fields['id']; if (!Group::canView() || !$user->can($ID, READ)) { return false; } $canedit = $user->can($ID, UPDATE); $rand = mt_rand(); $groups = self::getUserGroups($ID); $used = array(); if (!empty($groups)) { foreach ($groups as $data) { $used[$data["id"]] = $data["id"]; } } if ($canedit) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Associate to a group')."
"; echo ""; // All entities "edited user" have access $strict_entities = Profile_User::getUserEntities($ID, true); // Keep only entities "connected user" have access foreach ($strict_entities as $key => $val) { if (!Session::haveAccessToEntity($val)) { unset($strict_entities[$key]); } } $nb = countElementsInTableForEntity("glpi_groups", $strict_entities, '`is_usergroup`'); if ($nb > count($used)) { Group::dropdown(array('entity' => $strict_entities, 'used' => $used, 'condition' => '`is_usergroup`')); echo "".__('Manager').""; Dropdown::showYesNo('is_manager'); echo "".__('Delegatee').""; Dropdown::showYesNo('is_userdelegate'); echo ""; echo ""; } else { _e('None'); } echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && count($used)) { $rand = mt_rand(); Html::openMassiveActionsForm('mass'.__CLASS__.$rand); echo ""; $massiveactionparams = array('num_displayed' => min($_SESSION['glpilist_limit'], count($used)), 'container' => 'mass'.__CLASS__.$rand); Html::showMassiveActions($massiveactionparams); } echo ""; $header_begin = ""; $header_top = ''; $header_bottom = ''; $header_end = ''; if ($canedit && count($used)) { $header_begin .= ""; } $header_end .= ""; $header_end .= ""; $header_end .= ""; $header_end .= ""; echo $header_begin.$header_top.$header_end; $group = new Group(); if (!empty($groups)) { Session::initNavigateListItems('Group', //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), User::getTypeName(1), $user->getName())); foreach ($groups as $data) { if (!$group->getFromDB($data["id"])) { continue; } Session::addToNavigateListItems('Group', $data["id"]); echo ""; if ($canedit && count($used)) { echo ""; } $link = $data["completename"]; if ($_SESSION["glpiis_ids_visible"]) { $link = sprintf(__('%1$s (%2$s)'), $link, $data["id"]); } $href = "". $link.""; if ($data["is_dynamic"]) { $href = sprintf(__('%1$s (%2$s)'), $href, "".__('D').""); } echo ""; echo ""; } echo $header_begin.$header_bottom.$header_end; } else { echo ""; echo ""; } echo "
"; $header_top .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_bottom .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_end .= "".Group::getTypeName(1)."".__('Dynamic')."".__('Manager')."".__('Delegatee')."
"; Html::showMassiveActionCheckBox(__CLASS__, $data["linkID"]); echo "".$group->getLink().""; if ($data['is_dynamic']) { echo "\""."; } echo ""; if ($data['is_manager']) { echo "\""."; } echo ""; if ($data['is_userdelegate']) { echo "\""."; } echo "
".__('None')."
"; if ($canedit && count($used)) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } echo "
"; } /** * Show form to add a user in current group * * @since version 0.83 * * @param $group Group object * @param $used_ids Array of already add users * @param $entityrestrict Array of entities * @param $crit String for criteria (for default dropdown) **/ private static function showAddUserForm(Group $group, $used_ids, $entityrestrict, $crit) { global $CFG_GLPI, $DB; $rand = mt_rand(); $res = User::getSqlSearchResult (true, "all", $entityrestrict, 0, $used_ids); $nb = ($res ? $DB->result($res,0,"CPT") : 0); if ($nb) { echo ""; echo ""; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Add a user')."
"; User::dropdown(array('right' => "all", 'entity' => $entityrestrict, 'used' => $used_ids)); echo "".__('Manager').""; Dropdown::showYesNo('is_manager', (($crit == 'is_manager') ? 1 : 0)); echo "".__('Delegatee').""; Dropdown::showYesNo('is_userdelegate', (($crit == 'is_userdelegate') ? 1 : 0)); echo ""; echo ""; echo ""; echo "
"; Html::closeForm(); } } /** * Retrieve list of member of a Group * * @since version 0.83 * * @param $group Group object * @param $members Array filled on output of member (filtered) * @param $ids Array of ids (not filtered) * @param $crit String filter (is_manager, is_userdelegate) (default '') * @param $tree Boolean true to include member of sub-group (default 0) * * @return String tab of entity for restriction **/ static function getDataForGroup(Group $group, &$members, &$ids, $crit='', $tree=0) { global $DB; // Entity restriction for this group, according to user allowed entities if ($group->fields['is_recursive']) { $entityrestrict = getSonsOf('glpi_entities', $group->fields['entities_id']); // active entity could be a child of object entity if (($_SESSION['glpiactive_entity'] != $group->fields['entities_id']) && in_array($_SESSION['glpiactive_entity'], $entityrestrict)) { $entityrestrict = getSonsOf('glpi_entities', $_SESSION['glpiactive_entity']); } } else { $entityrestrict = $group->fields['entities_id']; } if ($tree) { $restrict = "IN (".implode(',', getSonsOf('glpi_groups', $group->getID())).")"; } else { $restrict = "='".$group->getID()."'"; } // All group members $query = "SELECT DISTINCT `glpi_users`.`id`, `glpi_groups_users`.`id` AS linkID, `glpi_groups_users`.`groups_id`, `glpi_groups_users`.`is_dynamic` AS is_dynamic, `glpi_groups_users`.`is_manager` AS is_manager, `glpi_groups_users`.`is_userdelegate` AS is_userdelegate FROM `glpi_groups_users` INNER JOIN `glpi_users` ON (`glpi_users`.`id` = `glpi_groups_users`.`users_id`) INNER JOIN `glpi_profiles_users` ON (`glpi_profiles_users`.`users_id`=`glpi_users`.`id`) WHERE `glpi_groups_users`.`groups_id` $restrict ". getEntitiesRestrictRequest('AND', 'glpi_profiles_users', '', $entityrestrict, 1)." ORDER BY `glpi_users`.`realname`, `glpi_users`.`firstname`, `glpi_users`.`name`"; $result = $DB->query($query); if ($DB->numrows($result) > 0) { while ($data=$DB->fetch_assoc($result)) { // Add to display list, according to criterion if (empty($crit) || $data[$crit]) { $members[] = $data; } // Add to member list (member of sub-group are not member) if ($data['groups_id'] == $group->getID()) { $ids[] = $data['id']; } } } return $entityrestrict; } /** * Show users of a group * * @since version 0.83 * * @param $group Group object: the group **/ static function showForGroup(Group $group) { global $DB, $CFG_GLPI; $ID = $group->getID(); if (!User::canView() || !$group->can($ID, READ)) { return false; } // Have right to manage members $canedit = self::canUpdate(); $rand = mt_rand(); $user = new User(); $crit = Session::getSavedOption(__CLASS__, 'criterion', ''); $tree = Session::getSavedOption(__CLASS__, 'tree', 0); $used = array(); $ids = array(); // Retrieve member list $entityrestrict = self::getDataForGroup($group, $used, $ids, $crit, $tree); if ($canedit) { self::showAddUserForm($group, $ids, $entityrestrict, $crit); } // Mini Search engine echo ""; echo ""; echo "
".User::getTypeName(Session::getPluralNumber())."
"; echo _n('Criterion', 'Criteria', 1)." "; $crits = array('is_manager' => __('Manager'), 'is_userdelegate' => __('Delegatee')); Dropdown::showFromArray('crit', $crits, array('value' => $crit, 'on_change' => 'reloadTab("start=0&criterion="+this.value)', 'display_emptychoice' => true)); if ($group->haveChildren()) { echo "".__('Child groups'); Dropdown::showYesNo('tree', $tree, -1, array('on_change' => 'reloadTab("start=0&tree="+this.value)')); } else { $tree = 0; } echo "
"; $number = count($used); $start = (isset($_GET['start']) ? intval($_GET['start']) : 0); if ($start >= $number) { $start = 0; } // Display results if ($number) { echo "
"; Html::printAjaxPager(sprintf(__('%1$s (%2$s)'), User::getTypeName(Session::getPluralNumber()), __('D=Dynamic')), $start, $number); Session::initNavigateListItems('User', //TRANS : %1$s is the itemtype name, // %2$s is the name of the item (used for headings of a list) sprintf(__('%1$s = %2$s'), Group::getTypeName(1), $group->getName())); if ($canedit) { Html::openMassiveActionsForm('mass'.__CLASS__.$rand); $massiveactionparams = array('num_displayed' => min($number-$start, $_SESSION['glpilist_limit']), 'container' => 'mass'.__CLASS__.$rand); Html::showMassiveActions($massiveactionparams); } echo ""; $header_begin = ""; $header_top = ''; $header_bottom = ''; $header_end = ''; if ($canedit) { $header_begin .= ""; } $header_end .= ""; if ($tree) { $header_end .= ""; } $header_end .= ""; $header_end .= ""; $header_end .= ""; $header_end .= ""; echo $header_begin.$header_top.$header_end; $tmpgrp = new Group(); for ($i=$start, $j=0 ; ($i < $number) && ($j < $_SESSION['glpilist_limit']) ; $i++, $j++) { $data = $used[$i]; $user->getFromDB($data["id"]); Session::addToNavigateListItems('User', $data["id"]); echo "\n"; if ($canedit) { echo ""; } echo ""; } echo $header_begin.$header_bottom.$header_end; echo "
"; $header_top .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_bottom .= Html::getCheckAllAsCheckbox('mass'.__CLASS__.$rand); $header_end .= "".User::getTypeName(1)."".Group::getTypeName(1)."".__('Dynamic')."".__('Manager')."".__('Delegatee')."".__('Active')."
"; Html::showMassiveActionCheckBox(__CLASS__, $data["linkID"]); echo "".$user->getLink(); if ($tree) { echo ""; if ($tmpgrp->getFromDB($data['groups_id'])) { echo $tmpgrp->getLink(array('comments' => true)); } } echo ""; if ($data['is_dynamic']) { echo "\""."; } echo ""; if ($data['is_manager']) { echo "\""."; } echo ""; if ($data['is_userdelegate']) { echo "\""."; } echo ""; if ($user->fields['is_active']) { echo "\""."; } echo "
"; if ($canedit) { $massiveactionparams['ontop'] = false; Html::showMassiveActions($massiveactionparams); Html::closeForm(); } Html::printAjaxPager(sprintf(__('%1$s (%2$s)'), User::getTypeName(Session::getPluralNumber()), __('D=Dynamic')), $start, $number); echo "
"; } else { echo "

".__('No item found')."

"; } } /** * @since version 0.85 * * @see CommonDBRelation::getRelationMassiveActionsSpecificities() **/ static function getRelationMassiveActionsSpecificities() { global $CFG_GLPI; $specificities = parent::getRelationMassiveActionsSpecificities(); $specificities['select_items_options_1'] = array('right' => 'all'); $specificities['select_items_options_2'] = array('condition' => '`is_usergroup`'); // Define normalized action for add_item and remove_item $specificities['normalized']['add'][] = 'add_supervisor'; $specificities['normalized']['add'][] = 'add_delegatee'; $specificities['button_labels']['add_supervisor'] = $specificities['button_labels']['add']; $specificities['button_labels']['add_delegatee'] = $specificities['button_labels']['add']; $specificities['update_if_different'] = true; return $specificities; } /** * @since version 0.85 * * @see CommonDBRelation::getRelationInputForProcessingOfMassiveActions() **/ static function getRelationInputForProcessingOfMassiveActions($action, CommonDBTM $item, array $ids, array $input) { switch ($action) { case 'add_supervisor' : return array('is_manager' => 1); case 'add_delegatee' : return array('is_userdelegate' => 1); } return array(); } /** * Get search function for the class * * @return array of search option **/ function getSearchOptions() { $tab = array(); $tab['common'] = __('Characteristics'); $tab[2]['table'] = $this->getTable(); $tab[2]['field'] = 'id'; $tab[2]['name'] = __('ID'); $tab[2]['massiveaction'] = false; $tab[2]['datatype'] = 'number'; $tab[3]['table'] = $this->getTable(); $tab[3]['field'] = 'is_dynamic'; $tab[3]['name'] = __('Dynamic'); $tab[3]['datatype'] = 'bool'; $tab[3]['massiveaction'] = false; $tab[4]['table'] = 'glpi_groups'; $tab[4]['field'] = 'completename'; $tab[4]['name'] = __('Group'); $tab[4]['massiveaction'] = false; $tab[4]['datatype'] = 'dropdown'; $tab[5]['table'] = 'glpi_users'; $tab[5]['field'] = 'name'; $tab[5]['name'] = __('User'); $tab[5]['massiveaction'] = false; $tab[5]['datatype'] = 'dropdown'; $tab[5]['right'] = 'all'; $tab[6]['table'] = $this->getTable(); $tab[6]['field'] = 'is_manager'; $tab[6]['name'] = __('Manager'); $tab[6]['datatype'] = 'bool'; $tab[7]['table'] = $this->getTable(); $tab[7]['field'] = 'is_userdelegate'; $tab[7]['name'] = __('Delegatee'); $tab[7]['datatype'] = 'bool'; return $tab; } /** * @param $user_ID * @param $only_dynamic (false by default **/ static function deleteGroups($user_ID, $only_dynamic=false) { global $DB; $crit['users_id'] = $user_ID; if ($only_dynamic) { $crit['is_dynamic'] = '1'; } $obj = new self(); $obj->deleteByCriteria($crit); } function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { if (!$withtemplate) { $nb = 0; switch ($item->getType()) { case 'User' : if (Group::canView()) { if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable($this->getTable(), "users_id = '".$item->getID()."'"); } return self::createTabEntry(Group::getTypeName(Session::getPluralNumber()), $nb); } break; case 'Group' : if (User::canView()) { if ($_SESSION['glpishow_count_on_tabs']) { $nb = countElementsInTable("glpi_groups_users", "`groups_id` = '".$item->getID()."'" ); } return self::createTabEntry(User::getTypeName(Session::getPluralNumber()), $nb); } break; } } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { switch ($item->getType()) { case 'User' : self::showForUser($item); break; case 'Group' : self::showForGroup($item); break; } return true; } } ?>