LDAP sync
This commit is contained in:
@@ -12,7 +12,7 @@ const DEFAULT_INTEGRATIONS = {
|
||||
usernameAttribute: 'sAMAccountName',
|
||||
firstNameAttribute: 'givenName',
|
||||
lastNameAttribute: 'sn',
|
||||
syncIntervalMinutes: 1440,
|
||||
syncIntervalMinutes: 0,
|
||||
syncEnabled: false,
|
||||
syncNotes: '',
|
||||
},
|
||||
@@ -50,20 +50,41 @@ export function saveIntegrations(obj) {
|
||||
).run(json);
|
||||
}
|
||||
|
||||
let ldapSyncTimer = null;
|
||||
let ldapSchedulerTimer = null;
|
||||
|
||||
/** Wie Stundenerfassung: alle 5 Minuten prüfen, ob eine Synchronisation fällig ist. */
|
||||
export function restartLdapSyncScheduler() {
|
||||
if (ldapSyncTimer) {
|
||||
clearInterval(ldapSyncTimer);
|
||||
ldapSyncTimer = null;
|
||||
if (ldapSchedulerTimer) {
|
||||
clearInterval(ldapSchedulerTimer);
|
||||
ldapSchedulerTimer = null;
|
||||
}
|
||||
const cfg = loadIntegrations().ldap;
|
||||
if (!cfg.syncEnabled) return;
|
||||
const m = Math.max(0, Number(cfg.syncIntervalMinutes) || 0);
|
||||
if (m <= 0) return;
|
||||
ldapSyncTimer = setInterval(() => {
|
||||
performLdapSync(db, loadIntegrations, 'automatic').catch((err) =>
|
||||
console.error('LDAP auto-sync:', err),
|
||||
);
|
||||
}, m * 60 * 1000);
|
||||
ldapSchedulerTimer = setInterval(() => {
|
||||
const cfg = loadIntegrations().ldap;
|
||||
if (!cfg.syncEnabled) return;
|
||||
const intervalMin = Math.max(0, Number(cfg.syncIntervalMinutes) || 0);
|
||||
if (intervalMin <= 0) return;
|
||||
|
||||
const row = db
|
||||
.prepare("SELECT value FROM app_settings WHERE key = 'ldap_last_sync_at'")
|
||||
.get();
|
||||
const lastSync = row?.value ? new Date(row.value) : null;
|
||||
const now = new Date();
|
||||
const syncIntervalMs = intervalMin * 60 * 1000;
|
||||
|
||||
if (!lastSync || now - lastSync >= syncIntervalMs) {
|
||||
console.log('Starte automatische LDAP-Synchronisation…');
|
||||
performLdapSync(db, loadIntegrations, 'automatic')
|
||||
.then((r) => {
|
||||
if (r.skipped) return;
|
||||
if (r.ok) {
|
||||
console.log(
|
||||
`Automatische LDAP-Synchronisation abgeschlossen: ${r.usersSynced} Benutzer synchronisiert`,
|
||||
);
|
||||
} else {
|
||||
console.error('LDAP auto-sync:', r.error || r.errors);
|
||||
}
|
||||
})
|
||||
.catch((err) => console.error('LDAP auto-sync:', err));
|
||||
}
|
||||
}, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user