Skip to content

Commit

Permalink
feat: duplicate config key
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemarchand committed Oct 21, 2024
1 parent b26c8bd commit 1d926ab
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
65 changes: 49 additions & 16 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ static function install() {
INSERT INTO `$table` (name, value)
VALUES ('url', ''),
('key', ''),
('group', '')
('duplicate', 'id')
SQL;

$DB->queryOrDie($addquery, $DB->error());
}
} else if (PLUGIN_OKTA_VERSION == "1.2.2") {
$query = <<<SQL
INSERT INTO `$table` (name, value)
VALUES ('duplicate', 'id')
SQL;
$DB->queryOrDie($query, $DB->error());
}

return true;
}
Expand Down Expand Up @@ -208,14 +214,25 @@ private static function createOrUpdateUser($userId) {
'phone_number' => 'mobilePhone',
'preferred_username' => 'login',
];
$OidcTranslation = [
'id' => 'id',
'name' => 'name',
'given_name' => 'firstName',
'family_name' => 'lastName',
'phone_number' => 'phone',
'email' => 'email'
];

$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];
if (!isset($OidcMappings['name'])) return false;
$distantUser = self::fetchUserById($userId);
$localUsers = iterator_to_array($DB->query("SELECT * FROM glpi_users"));
$localNames = array_combine(array_column($localUsers, 'id'), array_column($localUsers, 'name'));
if (!isset($OidcMappings[$OidcMappings[$config['duplicate']]])) return false;

$distantUser = self::fetchUserById($userId);
if (!$distantUser) return false;
$userObject = [];
foreach ($apiMappings as $key => $value) {
Expand All @@ -225,25 +242,26 @@ private static function createOrUpdateUser($userId) {
};
$profile = $distantUser['profile'];
$profile += ['id' => $distantUser['id']];
if (!isset($OidcMappings['name']) || !isset($apiMappings[$OidcMappings['name']])) {
Session::addMessageAfterRedirect(__('No okta mapping found for : ', 'okta') . $OidcMappings['name'], false, ERROR);
return false;
}
$userName = $profile[$apiMappings[$OidcMappings['name']]];
$ID = array_search($userName, $localNames);


$query = "SELECT * FROM glpi_users
LEFT JOIN glpi_useremails ON glpi_users.id = glpi_useremails.users_id
WHERE " . $OidcTranslation[$config['duplicate']] . " = '" . $profile[$apiMappings[$config['duplicate']]] . "'";
$localUser = iterator_to_array($DB->query($query));
$localUser = empty($localUser) ? false : $localUser[0];

$ID = empty($localUser) ? false : $localUser['id'];
if (!$ID) {
$rule = new RuleRightCollection();
$input = [
'authtype' => Auth::EXTERNAL,
'name' => $userName,
'name' => $profile[$apiMappings[$OidcMappings['name']]],
'_extauth' => 1,
'add' => 1
];
$input = $rule->processAllRules([], Toolbox::stripslashes_deep($input), [
'type' => Auth::EXTERNAL,
'email' => $userObject["email"] ?? '',
'login' => $userName,
'email' => $profile["email"] ?? '',
'login' => $profile[$apiMappings[$OidcMappings['name']]],
]);
$input['_ruleright_process'] = true;

Expand Down Expand Up @@ -276,6 +294,7 @@ static function importUser($userId, $groupId = null) {
* @return void
*/
public function showConfigForm() {
global $DB;
if (!Session::haveRight("plugin_okta_config",UPDATE)) {
return false;
}
Expand Down Expand Up @@ -304,6 +323,20 @@ public function showConfigForm() {
<td>API key</td>
<td><input type="text" name="key" value="{$key}"></td>
</tr>
<tr>
<td>Duplicate key</td>
<td>
<select name="duplicate">
HTML;
$OidcMappings = iterator_to_array($DB->query("SELECT * FROM glpi_oidc_mapping"))[0];
foreach ($OidcMappings as $key => $value) {
if (in_array($key, ['picture', 'locale', 'group'])) continue;
echo "<option value=\"$key\" ". (($key == $fields['duplicate']) ? "selected" : "") ." >$key</option>";
}
echo <<<HTML
</select>
</td>
</tr>
<tr>
<td class="center" colspan="2">
<input type="submit" name="update" class="submit" value="Save">
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.1.2');
define('PLUGIN_OKTA_VERSION', '1.2.2');

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

0 comments on commit 1d926ab

Please sign in to comment.