Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HL API integration #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
--------------------------------------------------------------------------
*/

use Glpi\Plugin\Hooks;

define('PLUGIN_VIP_VERSION', '1.8.2');

if (!defined("PLUGIN_VIP_DIR")) {
Expand Down Expand Up @@ -83,6 +85,8 @@ 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
$PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['vip'] = 'plugin_vip_redefine_api_schemas';
}

function plugin_version_vip() {
Expand Down