Skip to content

Commit

Permalink
feat: group regex import
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemarchand committed Oct 22, 2024
1 parent c691a49 commit cf9ab4f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
11 changes: 10 additions & 1 deletion front/config.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"])) {
Expand All @@ -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'));
};
Expand Down
61 changes: 46 additions & 15 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -303,7 +319,7 @@ public function showConfigForm() {
$action = self::getFormURL();
$csrf = Session::getNewCSRFToken();

$groups = self::testConnection();
$groups = self::getGroups();

$key = Toolbox::sodiumDecrypt($fields['key']);

Expand Down Expand Up @@ -348,12 +364,6 @@ public function showConfigForm() {
</form>
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 <<<HTML
<form method="post" action="{$action}">
<table class="tab_cadre">
Expand All @@ -367,9 +377,9 @@ public function showConfigForm() {
<select name="group" id="group">
<option value="">-----</option>
HTML;
echo implode('', array_map(function($key, $value) use ($options) {
return "<option value=\"$key\">$options[$key]</option>";
}, array_keys($options), array_values($options)));
echo implode('', array_map(function($key, $value) use ($groups) {
return "<option value=\"$key\">$groups[$key]</option>";
}, array_keys($groups), array_values($groups)));
echo <<<HTML
</select>
</td>
Expand All @@ -391,6 +401,27 @@ public function showConfigForm() {
</table>
<input type="hidden" name="_glpi_csrf_token" value="$csrf">
</form>
<form method="post" action="{$action}">
<table class="tab_cadre">
<tbody>
<tr>
<th colspan="2">Regex import groups</th>
</tr>
<tr>
<td>Group regex</td>
<td>
<input type="text" name="regex">
</td>
</tr>
<tr>
<td class="center" colspan="2">
<input type="submit" name="import_regex" class="submit" value="Import">
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="_glpi_csrf_token" value="$csrf">
</form>
<script>
$(document).ready(function() {
$('#user').select2({width: '100%'});
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* ---------------------------------------------------------------------
*/

define('PLUGIN_OKTA_VERSION', '1.2.2');
define('PLUGIN_OKTA_VERSION', '1.3.2');

if (!defined("PLUGIN_OKTA_DIR")) {
define("PLUGIN_OKTA_DIR", Plugin::getPhpDir("okta"));
Expand Down

0 comments on commit cf9ab4f

Please sign in to comment.