From 0e5d80213b62a973687ee2a2d77dd60287cbe250 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Sun, 24 Nov 2024 10:29:42 +0100 Subject: [PATCH] Add https://github.com/pluginsGLPI/vip/pull/4 Add https://github.com/pluginsGLPI/vip/pull/5 --- hook.php | 60 +++++++++++++++++++++++++++++-- inc/dashboard.class.php | 52 ++++++++++++++++++--------- inc/profile.class.php | 47 +++++++++++++++--------- inc/ticket.class.php | 79 ++++++++++++++++++++++++----------------- inc/vip.class.php | 12 +++++-- setup.php | 6 ++++ 6 files changed, 186 insertions(+), 70 deletions(-) diff --git a/hook.php b/hook.php index f7bb7bb..8832cca 100644 --- a/hook.php +++ b/hook.php @@ -76,9 +76,9 @@ function plugin_vip_uninstall() { "glpi_notepads", "glpi_dropdowntranslations"]; - foreach ($tables_glpi as $table_glpi) - $DB->query("DELETE FROM `$table_glpi` WHERE `itemtype` LIKE 'PluginVip%';"); - + foreach ($tables_glpi as $table_glpi) { + $DB->delete($table_glpi, ['itemtype' => ['LIKE' => 'PluginVip%']]); + } //drop rules $Rule = new Rule(); $a_rules = $Rule->find(['sub_type' => 'PluginVipRuleVip']); @@ -243,3 +243,57 @@ function plugin_vip_executeActions($options) { $vip = new PluginVipRuleVip(); return $vip->executeActions($options['action'], $options['output'], $options['params']); } + +function plugin_vip_redefine_api_schemas(array $data): array { + if (!Session::haveRight('plugin_vip', READ)) { + return $data; + } + foreach ($data['schemas'] as &$schema) { + if (!isset($schema['x-itemtype'])) { + continue; + } + switch ($schema['x-itemtype']) { + case 'User': + $schema['properties']['vip_groups'] = [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_ARRAY, + 'items' => [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_OBJECT, + 'x-join' => [ + // This is the join with the desired data + 'table' => 'glpi_plugin_vip_groups', + 'fkey' => 'groups_id', + 'field' => 'id', // This table uses the group ID as the primary key "id" + 'ref-join' => [ + // This is the linking join between the main item and the data needed + 'table' => 'glpi_groups_users', + 'fkey' => 'id', + 'field' => 'users_id', + ] + ], + 'properties' => [ + 'id' => [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_INTEGER, + 'x-readonly' => true, + ], + 'name' => [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING, + 'x-readonly' => true, + ], + 'color' => [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING, + 'x-readonly' => true, + 'x-field' => 'vip_color' + ], + 'icon' => [ + 'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING, + 'x-readonly' => true, + 'x-field' => 'vip_icon' + ], + ] + ] + ]; + break; + } + } + return $data; +} diff --git a/inc/dashboard.class.php b/inc/dashboard.class.php index 7ccb43c..6dade73 100644 --- a/inc/dashboard.class.php +++ b/inc/dashboard.class.php @@ -72,21 +72,41 @@ public function getWidgetContentForItem($widgetId) $groups[] = $mygroup["id"]; } - $query = "SELECT `glpi_tickets`.`id` AS tickets_id, - `glpi_tickets`.`status` AS status, - `glpi_tickets`.`time_to_resolve` AS time_to_resolve - FROM `glpi_tickets` - LEFT JOIN `glpi_entities` ON (`glpi_tickets`.`entities_id` = `glpi_entities`.`id`) - LEFT JOIN `glpi_groups_tickets` ON (`glpi_tickets`.`id` = `glpi_groups_tickets`.`tickets_id` - AND `glpi_groups_tickets`.`type` = " . CommonITILActor::ASSIGN . ") - WHERE `glpi_tickets`.`is_deleted` = '0' - AND `glpi_tickets`.`status` NOT IN (" . CommonITILObject::INCOMING . "," . CommonITILObject::SOLVED . "," . CommonITILObject::CLOSED . ") "; + $criteria = [ + 'SELECT' => [ + 'glpi_tickets.id AS tickets_id', 'glpi_tickets.status AS status', 'glpi_tickets.time_to_resolve AS time_to_resolve' + ], + 'FROM' => 'glpi_tickets', + 'LEFT JOIN' => [ + 'glpi_entities' => [ + 'ON' => [ + 'glpi_tickets' => 'entities_id', + 'glpi_entities' => 'id' + ] + ], + 'glpi_groups_tickets' => [ + 'ON' => [ + 'glpi_tickets' => 'id', + 'glpi_groups_tickets' => 'tickets_id', [ + 'AND' => [ + 'glpi_groups_tickets.type' => CommonITILActor::ASSIGN + ] + ] + ] + ] + ], + 'WHERE' => [ + 'glpi_tickets.is_deleted' => '0', + 'NOT' => ['glpi_tickets.status' => [CommonITILObject::INCOMING, CommonITILObject::SOLVED, CommonITILObject::CLOSED]] + ], + 'ORDER' => ['glpi_tickets.time_to_resolve' => 'DESC'] + ]; if (count($groups) > 0) { - $query .= "AND `glpi_groups_tickets`.`groups_id` IN (" . implode(",", $groups) . ")"; + $criteria['WHERE']['glpi_groups_tickets.groups_id'] = $groups; } - $query .= "ORDER BY `glpi_tickets`.`time_to_resolve` DESC";// - - $widget = PluginMydashboardHelper::getWidgetsFromDBQuery('table', $query); + $it = new DBmysqlIterator($DB); + $it->buildQuery($criteria); + $widget = PluginMydashboardHelper::getWidgetsFromDBQuery('table', $it->getSql()); $headers = [__('ID'), _n('Requester', 'Requesters', 2), __('Status'), @@ -94,14 +114,14 @@ public function getWidgetContentForItem($widgetId) __('Assigned to technicians')]; $widget->setTabNames($headers); - $result = $DB->query($query); - $nb = $DB->numrows($result); + $result = $DB->request($criteria); + $nb = count($result); $datas = []; $tickets = []; if ($nb) { - while ($data = $DB->fetchAssoc($result)) { + foreach ($result as $data) { $ticket = new Ticket(); $ticket->getFromDB($data['tickets_id']); if ($ticket->countUsers(CommonITILActor::REQUESTER)) { diff --git a/inc/profile.class.php b/inc/profile.class.php index 96a7994..c96c51b 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -178,10 +178,11 @@ public static function migrateOneProfile($profiles_id) return true; } - foreach ($DB->request( - 'glpi_plugin_vip_profiles', - "`profiles_id`='$profiles_id'" - ) as $profile_data) { + $it = $DB->request([ + 'FROM' => 'glpi_plugin_vip_profiles', + 'WHERE' => ['profiles_id' => $profiles_id] + ]); + foreach ($it as $profile_data) { $matching = ['show_vip_tab' => 'plugin_vip']; $current_rights = ProfileRight::getProfileRights($profiles_id, array_values($matching)); foreach ($matching as $old => $new) { @@ -196,10 +197,10 @@ public static function migrateOneProfile($profiles_id) break; } - $query = "UPDATE `glpi_profilerights` - SET `rights`='" . $right . "' - WHERE `name`='$new' AND `profiles_id`='$profiles_id'"; - $DB->query($query); + $DB->update('glpi_profilerights', ['rights' => $right], [ + 'name' => $new, + 'profiles_id' => $profiles_id + ]); } } } @@ -224,13 +225,21 @@ public static function initProfile() } //Migration old rights in new ones - foreach ($DB->request("SELECT `id` FROM `glpi_profiles`") as $prof) { + $it = $DB->request([ + 'SELECT' => ['id'], + 'FROM' => 'glpi_profiles' + ]); + foreach ($it as $prof) { self::migrateOneProfile($prof['id']); } - foreach ($DB->request("SELECT * - FROM `glpi_profilerights` - WHERE `profiles_id`='" . $_SESSION['glpiactiveprofile']['id'] . "' - AND `name` LIKE '%plugin_vip%'") as $prof) { + $it = $DB->request([ + 'FROM' => 'glpi_profilerights', + 'WHERE' => [ + 'profiles_id' => $_SESSION['glpiactiveprofile']['id'], + 'name' => ['LIKE', '%plugin_vip%'] + ] + ]); + foreach ($it as $prof) { if (isset($_SESSION['glpiactiveprofile'])) { $_SESSION['glpiactiveprofile'][$prof['name']] = $prof['rights']; } @@ -244,10 +253,14 @@ public static function changeProfile() { global $DB; - foreach ($DB->request("SELECT * - FROM `glpi_profilerights` - WHERE `profiles_id`='" . $_SESSION['glpiactiveprofile']['id'] . "' - AND `name` LIKE '%plugin_vip%'") as $prof) { + $it = $DB->request([ + 'FROM' => 'glpi_profilerights', + 'WHERE' => [ + 'profiles_id' => $_SESSION['glpiactiveprofile']['id'], + 'name' => ['LIKE', '%plugin_vip%'] + ] + ]); + foreach ($it as $prof) { $_SESSION['glpiactiveprofile'][$prof['name']] = $prof['rights']; } } diff --git a/inc/ticket.class.php b/inc/ticket.class.php index 0d0bc31..77c61ef 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -45,19 +45,24 @@ public static function isUserVip($uid) global $DB; if ($uid) { - $vipquery = "SELECT `glpi_plugin_vip_groups`.`id` - FROM `glpi_groups_users` - LEFT JOIN `glpi_plugin_vip_groups` - ON `glpi_plugin_vip_groups`.`id` = `glpi_groups_users`.`groups_id` - WHERE `glpi_plugin_vip_groups`.`isvip` = 1 - AND `glpi_groups_users`.`users_id` = " . $uid; - - $result = $DB->query($vipquery); - $nb = $DB->numrows($result); - if ($nb > 0) { - while ($uids = $DB->fetchArray($result)) { - return $uids['id']; - } + $result = $DB->request([ + 'SELECT' => ['glpi_plugin_vip_groups.id'], + 'FROM' => 'glpi_groups_users', + 'LEFT JOIN' => [ + 'glpi_plugin_vip_groups' => [ + 'ON' => [ + 'glpi_plugin_vip_groups' => 'id', + 'glpi_groups_users' => 'groups_id' + ] + ] + ], + 'WHERE' => [ + 'glpi_plugin_vip_groups.isvip' => 1, + 'glpi_groups_users.users_id' => $uid + ] + ]); + if (count($result) > 0) { + return $result->current()['id']; } } @@ -73,17 +78,25 @@ public static function getUserVipList($entities) { global $DB; - $vip = []; - $vipquery = "SELECT `glpi_groups_users`.`users_id` - FROM `glpi_groups_users` - LEFT JOIN `glpi_plugin_vip_groups` - ON `glpi_plugin_vip_groups`.`id` = `glpi_groups_users`.`groups_id` - WHERE `glpi_plugin_vip_groups`.`isvip` = 1"; - - $result = $DB->query($vipquery); - $nb = $DB->numrows($result); - if ($nb > 0) { - while ($uids = $DB->fetchArray($result)) { + $vip = []; + + $result = $DB->request([ + 'SELECT' => ['glpi_groups_users.users_id'], + 'FROM' => 'glpi_groups_users', + 'LEFT JOIN' => [ + 'glpi_plugin_vip_groups' => [ + 'ON' => [ + 'glpi_plugin_vip_groups' => 'id', + 'glpi_groups_users' => 'groups_id' + ] + ] + ], + 'WHERE' => [ + 'glpi_plugin_vip_groups.isvip' => 1 + ] + ]); + if (count($result) > 0) { + foreach ($result as $uids) { $vip[] = $uids['users_id']; } } @@ -100,14 +113,16 @@ public static function isTicketVip($ticketid) global $DB; if ($ticketid > 0) { - $userquery = "SELECT `users_id` - FROM `glpi_tickets_users` - WHERE `type` = " . CommonITILActor::REQUESTER . " - AND `tickets_id` = " . $ticketid; - $userresult = $DB->query($userquery); - $nb = $DB->numrows($userresult); - if ($nb > 0) { - while ($uids = $DB->fetchArray($userresult)) { + $userresult = $DB->request([ + 'SELECT' => ['users_id'], + 'FROM' => 'glpi_tickets_users', + 'WHERE' => [ + 'type' => CommonITILActor::REQUESTER, + 'tickets_id' => $ticketid + ] + ]); + if (count($userresult) > 0) { + foreach ($userresult as $uids) { $isuservip = self::isUserVip($uids['users_id']); if ($isuservip > 0) { return $isuservip; diff --git a/inc/vip.class.php b/inc/vip.class.php index cb54299..bb6baad 100644 --- a/inc/vip.class.php +++ b/inc/vip.class.php @@ -86,7 +86,11 @@ public static function afterAdd(User $user) if (isset($fields['groups_id'])) { $groupuser = new Group_User(); - if (!$groupuser->find(["`users_id` = " . $user->getID() . " AND `groups_id` =" . $fields['groups_id']])) { + $result = $groupuser->find([ + 'users_id' => $user->getID(), + 'groups_id' => $fields['groups_id'] + ]); + if (!$result) { $groupuser->add(['users_id' => $user->getID(), 'groups_id' => $fields['groups_id']]); } @@ -132,7 +136,11 @@ public static function afterUpdate(User $user) if (isset($fields['groups_id'])) { $groupuser = new Group_User(); - if (!$groupuser->find(["`users_id` = " . $user->getID() . " AND `groups_id` =" . $fields['groups_id']])) { + $result = $groupuser->find([ + 'users_id' => $user->getID(), + 'groups_id' => $fields['groups_id'] + ]); + if (!$result) { $groupuser->add(['users_id' => $user->getID(), 'groups_id' => $fields['groups_id']]); } diff --git a/setup.php b/setup.php index 29ee584..026a0a6 100644 --- a/setup.php +++ b/setup.php @@ -27,6 +27,8 @@ -------------------------------------------------------------------------- */ +use Glpi\Plugin\Hooks; + define('PLUGIN_VIP_VERSION', '1.8.3'); if (!defined("PLUGIN_VIP_DIR")) { @@ -83,6 +85,10 @@ function plugin_init_vip() { Plugin::registerClass('PluginVipRuleVipCollection', [ 'rulecollections_types' => true ]); + + // Cannot be placed inside any permission check as the plugin is initialized before the API router authenticates the user + //TODO activate in GLPI 11 version +// $PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['vip'] = 'plugin_vip_redefine_api_schemas'; } function plugin_version_vip() {