From cf9ab4f6458f505dc815a6dc60dfe8f6e8025355 Mon Sep 17 00:00:00 2001 From: Antoine Lemarchand Date: Tue, 22 Oct 2024 09:07:50 +0200 Subject: [PATCH] feat: group regex import --- front/config.form.php | 11 +++++++- inc/config.class.php | 61 ++++++++++++++++++++++++++++++++----------- setup.php | 2 +- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/front/config.form.php b/front/config.form.php index 4950866..679434f 100644 --- a/front/config.form.php +++ b/front/config.form.php @@ -36,7 +36,7 @@ if($plugin->isActivated("okta")) { $config = new PluginOktaConfig(); - if(isset($_POST["update"]) || isset($_POST["import"])) { + if(isset($_POST["update"]) || isset($_POST["import"]) || isset($_POST["import_regex"])) { Session::checkRight("plugin_okta_config", UPDATE); $config::updateConfigValues($_POST); if (isset($_POST["import"])) { @@ -45,6 +45,15 @@ } else { Session::addMessageAfterRedirect(__('Users imported successfully', 'okta')); } + } else if (isset($_POST["import_regex"])) { + $groups = $config::getGroupsByRegex($_POST["regex"]); + foreach ($groups as $id => $group) { + if (!$config::importUser(-1, $id)) { + Session::addMessageAfterRedirect(__('Could not import users', 'okta'), false, ERROR); + } else { + Session::addMessageAfterRedirect(sprintf(__('Users from %s imported successfully', 'okta'), $group)); + } + } } else { Session::addMessageAfterRedirect(__('Settings updated successfully', 'okta')); }; diff --git a/inc/config.class.php b/inc/config.class.php index ffeb129..7634791 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -163,12 +163,31 @@ static private function request($url, $key, $method = 'GET', $body = null) { return $jsonResponse; } - static function testConnection() { + static function getGroups() { $values = self::getConfigValues(); $url = $values['url']; $key = Toolbox::sodiumDecrypt($values['key']); - return self::request($url . "/api/v1/groups", $key); + $groupsObjects = self::request($url . "/api/v1/groups", $key); + $groups = []; + foreach ($groupsObjects as $group) { + $groups[$group['id']] = $group['profile']['name']; + } + return $groups; + } + + static function getGroupsByRegex($regex) { + $groups = self::getGroups(); + $regex = stripslashes($regex); + + $filteredGroups = []; + + foreach ($groups as $key => $value) { + if (preg_match("/$regex/i", $value)) { + $filteredGroups[$key] = $value; + } + } + return $filteredGroups; } static function fetchUserById($id) { @@ -224,9 +243,6 @@ private static function createOrUpdateUser($userId) { ]; $config = self::getConfigValues(); - if ($config['duplicate'] != 'email') { - $duplicateIndex = $OidcTranslation[$config['duplicate']]; - } $newUser = new User(); $OidcMappings = iterator_to_array($DB->query("SELECT * FROM glpi_oidc_mapping"))[0]; @@ -303,7 +319,7 @@ public function showConfigForm() { $action = self::getFormURL(); $csrf = Session::getNewCSRFToken(); - $groups = self::testConnection(); + $groups = self::getGroups(); $key = Toolbox::sodiumDecrypt($fields['key']); @@ -348,12 +364,6 @@ public function showConfigForm() { HTML; if ($groups) { - $keys = array_column($groups, 'id'); - $values = []; - foreach ($groups as $group) { - $values[$group['id']] = $group['profile']['name']; - } - $options = array_combine($keys, $values); echo << @@ -367,9 +377,9 @@ public function showConfigForm() {
+
+ + + + + + + + + + + + + +
Regex import groups
Group regex + +
+ +
+ +