Skip to content

Commit

Permalink
feat: deactivate non imported users
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineLemarchand committed Dec 18, 2024
1 parent c93f46c commit 15f2ba9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
60 changes: 44 additions & 16 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static function install() {
('use_group_regex', '0'),
('group_regex', ''),
('full_import', '0'),
('deactivate', '0'),
('use_norm_id', '0'),
('use_norm_name', '0'),
('use_norm_given_name', '0'),
Expand Down Expand Up @@ -148,6 +149,12 @@ static function install() {
('filter_family_name', ''),
('filter_email', ''),
('filter_phone_number', '')
SQL;
$DB->queryOrDie($query, $DB->error());
} else if (PLUGIN_OKTA_VERSION == "1.6.0") {
$query = <<<SQL
INSERT INTO `$table` (name, value)
VALUES ('deactivate', '0')
SQL;
$DB->queryOrDie($query, $DB->error());
}
Expand Down Expand Up @@ -356,6 +363,8 @@ private static function createOrUpdateUser($user, $config, $fullImport = false)
}

static function importUser($authorizedGroups, $fullImport = false, $userId = NULL) {
global $DB;

$importedUsers = [];
$config = self::getConfigValues();
if (!$userId) {
Expand Down Expand Up @@ -385,6 +394,20 @@ static function importUser($authorizedGroups, $fullImport = false, $userId = NUL
$importedUsers[] = $importedUser;
}
}
if ($config['deactivate'] == 1) {
$users = iterator_to_array($DB->request([
'SELECT' => ['id'],
'FROM' => 'glpi_users',
]));
$importedIds = array_map(function($user) {
return $user['id'];
}, $importedUsers);
foreach ($users as $user) {
if (!in_array($user['id'], $importedIds)) {
$DB->updateOrDie('glpi_users', ['is_active' => 0], ['id' => $user['id']]);
}
}
}
} else {
$userObject = self::request("api/v1/users/{$userId}");
$user = $userObject['profile'];
Expand Down Expand Up @@ -541,10 +564,15 @@ public function showConfigForm() {
</tr>
<tr>
<td><?php echo __('Update existing users', 'okta') ?></td>
<td colspan='3'>
<td>
<input type="hidden" name="full_import" value='0' >
<input type="checkbox" name="full_import" value='1' <?php echo $fields['full_import'] ? 'checked' : '' ?>>
</td>
<td><?php echo __('Deactivate non imported users', 'okta') ?></td>
<td>
<input type="hidden" name="deactivate" value='0' >
<input type="checkbox" name="deactivate" value='1' <?php echo $fields['deactivate'] ? 'checked' : '' ?>>
</td>
</tr>
<tr>
<td class="center" colspan="4">
Expand Down Expand Up @@ -591,22 +619,22 @@ public function showConfigForm() {
$(function() {
$('#user').select2({width: '100%'});
})
document.getElementById('group').addEventListener('change', function() {
var group = this.querySelector('option:checked').getAttribute('data-gid');
var user = document.getElementById('user');
user.innerHTML = '';
// add loading animation
user.innerHTML = '<option value="">Please wait...</option>';
fetch('<?php echo $action ?>?action=getUsers&group=' + group)
.then(response => response.json())
.then(data => {
$("#user").html('');
$("#user").append('<option value="-1">-----</option>');
for (const [key, value] of Object.entries(data)) {
$("#user").append('<option value="' + key + '">' + value + '</option>');
}
});
document.getElementById('group').addEventListener('change', function() {
var group = this.querySelector('option:checked').getAttribute('data-gid');
var user = document.getElementById('user');
user.innerHTML = '';
// add loading animation
user.innerHTML = '<option value="">Please wait...</option>';
fetch('<?php echo $action ?>?action=getUsers&group=' + group)
.then(response => response.json())
.then(data => {
$("#user").html('');
$("#user").append('<option value="-1">-----</option>');
for (const [key, value] of Object.entries(data)) {
$("#user").append('<option value="' + key + '">' + value + '</option>');
}
});
});
document.getElementById('regex_group_checkbox').addEventListener('change', function() {
const value = this.checked;
const dropdown = document.getElementById('group');
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.5.0');
define('PLUGIN_OKTA_VERSION', '1.6.0');

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

0 comments on commit 15f2ba9

Please sign in to comment.