/* Package ldapSync provides a scheduler for syncing ldap users and groups to the database */ package ldapSync import ( "fmt" "git.stuve.uni-ulm.de/StuVe-IT/stuve-it-backend/logger" "github.com/pocketbase/pocketbase" "github.com/pocketbase/pocketbase/tools/cron" "os" "strconv" "time" ) // ldapTimeToUnixTime converts a ldap time string to a time.Time object func ldapTimeToUnixTime(ldapTimeStampStr string) (time.Time, error) { ldapTimeStamp, err := strconv.ParseInt(ldapTimeStampStr, 10, 64) if err != nil { return time.Time{}, fmt.Errorf("error parsing ldap time string: %v", err) } // convert from 100 nanosecond intervals to milliseconds unixTimeStamp := ldapTimeStamp/1e4 - 1.16444736e13 if unixTimeStamp < 0 { return time.Time{}, fmt.Errorf("error parsing ldap time string: unixTimeStamp is negative") } // Convert milliseconds to seconds seconds := unixTimeStamp / 1000 // Create a time.Time object t := time.Unix(seconds, 0) return t, nil } // InitLdapSync initializes the ldap sync scheduler // // the function syncs the ldap users and groups initially and then as defined in the LDAP_SYNC_SCHEDULE env variable (cron syntax) // // the function also checks for the existence of the ldapUsers and ldapGroups tables and creates them if they do not exist func InitLdapSync(app *pocketbase.PocketBase) error { // check if ldapGroups table exists if _, err := app.Dao().FindCollectionByNameOrId(ldapGroupsTableName); err != nil { // create ldap_groups table if not exists logger.LogInfoF("creating " + ldapGroupsTableName + " table ...") if err := createLDAPGroupsTable(app); err != nil { return err } } else { logger.LogInfoF(ldapGroupsTableName + " table already exists ... skipping creation") } // check if ldapUsers table exists if _, err := app.Dao().FindCollectionByNameOrId(ldapUsersTableName); err != nil { // create ldap_users table if not exists logger.LogInfoF("creating " + ldapUsersTableName + " table ...") if err := createLDAPUsersTable(app); err != nil { return err } } else { logger.LogInfoF(ldapUsersTableName + " table already exists ... skipping creation") } // check if ldapSyncLogs table exists if _, err := app.Dao().FindCollectionByNameOrId(ldapSyncLogsTableName); err != nil { // create ldapSyncs table if not exists logger.LogInfoF("creating " + ldapSyncLogsTableName + " table ...") if err := createLDAPSyncLogsTable(app); err != nil { return err } } else { logger.LogInfoF(ldapSyncLogsTableName + " table already exists ... skipping creation") } // start sync scheduler := cron.New() // initial sync logger.LogInfoF("initial LDAP on startup") syncLdap(app) logger.LogInfoF("... initial LDAP Sync done") ldapSyncSchedule := os.Getenv("LDAP_SYNC_SCHEDULE") // syncs ldap - interval specified in the LDAP_SYNC_SCHEDULE env variable scheduler.MustAdd("ldapSync", ldapSyncSchedule, func() { logger.LogInfoF("syncing LDAP ...") syncLdap(app) logger.LogInfoF("... LDAP Sync done") }) scheduler.Start() logger.LogInfoF("ldap sync scheduler started with schedule: %s", ldapSyncSchedule) return nil }