. * --------------------------------------------------------------------- */ 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'; /** * Check if a user belongs to a group * * @since 9.4 * * @param integer $users_id the user ID * @param integer $groups_id the group ID * * @return boolean true if the user belongs to the group */ static function isUserInGroup($users_id, $groups_id) { return countElementsInTable( 'glpi_groups_users', [ 'users_id' => $users_id, 'groups_id' => $groups_id ] ) > 0; } /** * Get groups for a user * * @param integer $users_id User id * @param array $condition Query extra condition (default []) * * @return array **/ static function getUserGroups($users_id, $condition = []) { global $DB; $groups = []; $iterator = $DB->request([ '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' => self::getTable(), 'LEFT JOIN' => [ Group::getTable() => [ 'FKEY' => [ Group::getTable() => 'id', self::getTable() => 'groups_id' ] ] ], 'WHERE' => [ 'glpi_groups_users.users_id' => $users_id ] + $condition, 'ORDER' => 'glpi_groups.name' ]); while ($row = $iterator->next()) { $groups[] = $row; } return $groups; } /** * Get users for a group * * @since 0.84 * * @param integer $groups_id Group ID * @param array $condition Query extra condition (default []) * * @return array **/ static function getGroupUsers($groups_id, $condition = []) { global $DB; $users = []; $iterator = $DB->request([ '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' => self::getTable(), 'LEFT JOIN' => [ User::getTable() => [ 'FKEY' => [ User::getTable() => 'id', self::getTable() => 'users_id' ] ] ], 'WHERE' => [ 'glpi_groups_users.groups_id' => $groups_id ] + $condition, 'ORDER' => 'glpi_users.name' ]); while ($row = $iterator->next()) { $users[] = $row; } 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(); $iterator = self::getListForItem($user); $groups = []; //$groups = self::getUserGroups($ID); $used = []; while ($data = $iterator->next()) { $used[$data["id"]] = $data["id"]; $groups[] = $data; } if ($canedit) { echo "
"; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Associate to a group')."
"; echo ""; $params = [ 'used' => $used, 'condition' => [ 'is_usergroup' => 1, ] + getEntitiesRestrictCriteria(Group::getTable(), '', '', true) ]; Group::dropdown($params); echo "".__('Manager').""; Dropdown::showYesNo('is_manager'); echo "".__('Delegatee').""; Dropdown::showYesNo('is_userdelegate'); echo ""; echo ""; echo "
"; Html::closeForm(); echo "
"; } echo "
"; if ($canedit && count($used)) { $rand = mt_rand(); Html::openMassiveActionsForm('mass'.__CLASS__.$rand); echo ""; $massiveactionparams = ['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 ""; } 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 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) { $rand = mt_rand(); $res = User::getSqlSearchResult(true, "all", $entityrestrict, 0, $used_ids); $nb = count($res); if ($nb) { echo ""; echo ""; echo "
"; echo ""; echo ""; echo ""; echo "
".__('Add a user')."
"; User::dropdown(['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 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 = getSonsOf('glpi_groups', $group->getID()); } else { $restrict = $group->getID(); } // All group members $pu_table = Profile_User::getTable(); $iterator = $DB->request([ 'SELECT' => [ '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' ], 'DISTINCT' => true, 'FROM' => self::getTable(), 'LEFT JOIN' => [ User::getTable() => [ 'ON' => [ self::getTable() => 'users_id', User::getTable() => 'id' ] ], $pu_table => [ 'ON' => [ $pu_table => 'users_id', User::getTable() => 'id' ] ] ], 'WHERE' => [ self::getTable() . '.groups_id' => $restrict, 'OR' => [ "$pu_table.entities_id" => null ] + getEntitiesRestrictCriteria($pu_table, '', $entityrestrict, 1) ], 'ORDERBY' => [ User::getTable() . '.realname', User::getTable() . '.firstname', User::getTable() . '.name' ] ]); while ($data = $iterator->next()) { // 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 0.83 * * @param $group Group object: the group **/ static function showForGroup(Group $group) { global $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 = []; $ids = []; // Retrieve member list // TODO: migrate to use CommonDBRelation::getListForItem() $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 = ['is_manager' => __('Manager'), 'is_userdelegate' => __('Delegatee')]; Dropdown::showFromArray('crit', $crits, ['value' => $crit, 'on_change' => 'reloadTab("start=0&criterion="+this.value)', 'display_emptychoice' => true]); if ($group->haveChildren()) { echo "".__('Child groups'); Dropdown::showYesNo('tree', $tree, -1, ['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 = ['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(['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 0.85 * * @see CommonDBRelation::getRelationMassiveActionsSpecificities() **/ static function getRelationMassiveActionsSpecificities() { $specificities = parent::getRelationMassiveActionsSpecificities(); $specificities['select_items_options_1'] = ['right' => 'all']; $specificities['select_items_options_2'] = [ 'condition' => [ 'is_usergroup' => 1, ] + getEntitiesRestrictCriteria(Group::getTable(), '', '', true) ]; // 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; } static function getRelationInputForProcessingOfMassiveActions($action, CommonDBTM $item, array $ids, array $input) { switch ($action) { case 'add_supervisor' : return ['is_manager' => 1]; case 'add_delegatee' : return ['is_userdelegate' => 1]; } return []; } /** * Get search function for the class * * @return array of search option **/ function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => 'common', 'name' => __('Characteristics') ]; $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => __('ID'), 'massiveaction' => false, 'datatype' => 'number' ]; $tab[] = [ 'id' => '3', 'table' => $this->getTable(), 'field' => 'is_dynamic', 'name' => __('Dynamic'), 'datatype' => 'bool', 'massiveaction' => false ]; $tab[] = [ 'id' => '4', 'table' => 'glpi_groups', 'field' => 'completename', 'name' => Group::getTypeName(1), 'massiveaction' => false, 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '5', 'table' => 'glpi_users', 'field' => 'name', 'name' => User::getTypeName(1), 'massiveaction' => false, 'datatype' => 'dropdown', 'right' => 'all' ]; $tab[] = [ 'id' => '6', 'table' => $this->getTable(), 'field' => 'is_manager', 'name' => __('Manager'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '7', 'table' => $this->getTable(), 'field' => 'is_userdelegate', 'name' => __('Delegatee'), 'datatype' => 'bool' ]; return $tab; } /** * @param $user_ID * @param $only_dynamic (false by default **/ static function deleteGroups($user_ID, $only_dynamic = false) { $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 = self::countForItem($item); } return self::createTabEntry(Group::getTypeName(Session::getPluralNumber()), $nb); } break; case 'Group' : if (User::canView()) { if ($_SESSION['glpishow_count_on_tabs']) { $nb = self::countForItem($item); } 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; } /** * Get linked items list for specified item * * @since 9.3.1 * * @param CommonDBTM $item Item instance * @param boolean $noent Flag to not compute entity informations (see Document_Item::getListForItemParams) * * @return array */ protected static function getListForItemParams(CommonDBTM $item, $noent = false) { $params = parent::getListForItemParams($item, $noent); $params['SELECT'][] = self::getTable() . '.is_manager'; $params['SELECT'][] = self::getTable() . '.is_userdelegate'; return $params; } function post_addItem() { global $DB; // add new user to plannings $groups_id = $this->fields['groups_id']; $planning_k = 'group_'.$groups_id.'_users'; // find users with the current group in their plannings $user_inst = new User; $users = $user_inst->find([ 'plannings' => ['LIKE', "%$planning_k%"] ]); // add the new user to found plannings $query = $DB->buildUpdate( User::getTable(), [ 'plannings' => new QueryParam(), ], [ 'id' => new QueryParam() ] ); $stmt = $DB->prepare($query); $in_transaction = $DB->inTransaction(); if (!$in_transaction) { $DB->beginTransaction(); } foreach ($users as $user) { $users_id = $user['id']; $plannings = importArrayFromDB($user['plannings']); $nb_users = count($plannings['plannings'][$planning_k]['users']); // add the planning for the user $plannings['plannings'][$planning_k]['users']['user_'.$this->fields['users_id']]= [ 'color' => Planning::getPaletteColor('bg', $nb_users), 'display' => true, 'type' => 'user' ]; // if current user logged, append also to its session if ($users_id == Session::getLoginUserID()) { $_SESSION['glpi_plannings'] = $plannings; } // save the planning completed to db $json_plannings = exportArrayToDB($plannings); $stmt->bind_param('si', $json_plannings, $users_id); $stmt->execute(); } if (!$in_transaction) { $DB->commit(); } $stmt->close(); } function post_purgeItem() { global $DB; // remove user from plannings $groups_id = $this->fields['groups_id']; $planning_k = 'group_'.$groups_id.'_users'; // find users with the current group in their plannings $user_inst = new User; $users = $user_inst->find([ 'plannings' => ['LIKE', "%$planning_k%"] ]); // remove the deleted user to found plannings $query = $DB->buildUpdate( User::getTable(), [ 'plannings' => new QueryParam(), ], [ 'id' => new QueryParam() ] ); $stmt = $DB->prepare($query); $in_transaction = $DB->inTransaction(); if (!$in_transaction) { $DB->beginTransaction(); } foreach ($users as $user) { $users_id = $user['id']; $plannings = importArrayFromDB($user['plannings']); // delete planning for the user unset($plannings['plannings'][$planning_k]['users']['user_'.$this->fields['users_id']]); // if current user logged, append also to its session if ($users_id == Session::getLoginUserID()) { $_SESSION['glpi_plannings'] = $plannings; } // save the planning completed to db $json_plannings = exportArrayToDB($plannings); $stmt->bind_param('si', $json_plannings, $users_id); $stmt->execute(); } if (!$in_transaction) { $DB->commit(); } $stmt->close(); } }