Search in sources :

Example 1 with LDAPEvent

use of org.olat.ldap.LDAPEvent in project OpenOLAT by OpenOLAT.

the class LDAPLoginManagerImpl method doBatchSync.

/**
 * Execute Batch Sync. Will update all Attributes of LDAP users in OLAt, create new users and delete users in OLAT.
 * Can be configured in ldapContext.xml
 *
 * @param LDAPError
 */
@Override
public boolean doBatchSync(LDAPError errors) {
    // property read.
    synchronized (LDAPLoginManagerImpl.class) {
        if (batchSyncIsRunning) {
            // don't run twice, skip this execution
            log.info("LDAP user doBatchSync started, but another job is still running - skipping this sync");
            errors.insert("BatchSync already running by concurrent process");
            return false;
        }
    }
    WorkThreadInformations.setLongRunningTask("ldapSync");
    coordinator.getEventBus().fireEventToListenersOf(new LDAPEvent(LDAPEvent.SYNCHING), ldapSyncLockOres);
    lastSyncDate = null;
    LdapContext ctx = null;
    boolean success = false;
    try {
        acquireSyncLock();
        long startTime = System.currentTimeMillis();
        ctx = bindSystem();
        if (ctx == null) {
            errors.insert("LDAP connection ERROR");
            log.error("LDAP batch sync: LDAP connection empty", null);
            freeSyncLock();
            success = false;
            return success;
        }
        Date timeBeforeSync = new Date();
        // check server capabilities
        // Get time before sync to have a save sync time when sync is successful
        String sinceSentence = (lastSyncDate == null ? "" : " since last sync from " + lastSyncDate);
        doBatchSyncDeletedUsers(ctx, sinceSentence);
        // bind again to use an initial unmodified context. lookup of server-properties might fail otherwise!
        ctx.close();
        ctx = bindSystem();
        Map<String, LDAPUser> dnToIdentityKeyMap = new HashMap<>();
        List<LDAPUser> ldapUsers = doBatchSyncNewAndModifiedUsers(ctx, sinceSentence, dnToIdentityKeyMap, errors);
        ctx.close();
        ctx = bindSystem();
        // sync groups by LDAP groups or attributes
        doBatchSyncGroups(ctx, ldapUsers, dnToIdentityKeyMap, errors);
        // sync roles
        doBatchSyncRoles(ctx, ldapUsers, dnToIdentityKeyMap, errors);
        // update sync time and set running flag
        lastSyncDate = timeBeforeSync;
        ctx.close();
        success = true;
        log.audit("LDAP batch sync done: " + success + " in " + ((System.currentTimeMillis() - startTime) / 1000) + "s");
        return success;
    } catch (Exception e) {
        errors.insert("Unknown error");
        log.error("LDAP batch sync, unknown reason", e);
        success = false;
        return success;
    } finally {
        WorkThreadInformations.unsetLongRunningTask("ldapSync");
        freeSyncLock();
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            // try but failed silently
            }
        }
        LDAPEvent endEvent = new LDAPEvent(LDAPEvent.SYNCHING_ENDED);
        endEvent.setTimestamp(new Date());
        endEvent.setSuccess(success);
        endEvent.setErrors(errors);
        coordinator.getEventBus().fireEventToListenersOf(endEvent, ldapSyncLockOres);
    }
}
Also used : HashMap(java.util.HashMap) LDAPUser(org.olat.ldap.model.LDAPUser) LDAPEvent(org.olat.ldap.LDAPEvent) Date(java.util.Date) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 2 with LDAPEvent

use of org.olat.ldap.LDAPEvent in project OpenOLAT by OpenOLAT.

the class LDAPAdminController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
 *      org.olat.core.gui.components.Component,
 *      org.olat.core.gui.control.Event)
 */
@Override
protected void event(UserRequest ureq, Component source, Event event) {
    if (source == syncStartLink) {
        // Start sync job
        // Disable start link during sync
        syncStartLink.setEnabled(false);
        LDAPEvent ldapEvent = new LDAPEvent(LDAPEvent.DO_SYNCHING);
        CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(ldapEvent, LDAPLoginManager.ldapSyncLockOres);
        showInfo("admin.synchronize.started");
    } else if (source == syncOneUserLink) {
        userSearchCtrl = new UserSearchController(ureq, getWindowControl(), false);
        listenTo(userSearchCtrl);
        calloutCtr = new CloseableCalloutWindowController(ureq, getWindowControl(), userSearchCtrl.getInitialComponent(), syncOneUserLink, null, true, null);
        calloutCtr.addDisposableChildController(userSearchCtrl);
        calloutCtr.activate();
        listenTo(calloutCtr);
    } else if (source == deletStartLink) {
        // cancel if some one else is making sync or delete job
        if (!ldapLoginManager.acquireSyncLock()) {
            showError("delete.error.lock");
        } else {
            deletStartLink.setEnabled(false);
            // check and get LDAP connection
            LdapContext ctx = ldapLoginManager.bindSystem();
            if (ctx == null) {
                showError("delete.error.connection");
                return;
            }
            // get deleted users
            identitiesToDelete = ldapLoginManager.getIdentitysDeletedInLdap(ctx);
            try {
                ctx.close();
            } catch (NamingException e) {
                showError("delete.error.connection.close");
                logError("Could not close LDAP connection on manual delete sync", e);
            }
            if (identitiesToDelete != null && identitiesToDelete.size() != 0) {
                hasIdentitiesToDelete = true;
                /*
					 * start step which spawns the whole wizard
					 */
                Step start = new DeletStep00(ureq, hasIdentitiesToDelete, identitiesToDelete);
                /*
					 * wizard finish callback called after "finish" is called
					 */
                StepRunnerCallback finishCallback = new StepRunnerCallback() {

                    public Step execute(UserRequest uureq, WindowControl control, StepsRunContext runContext) {
                        hasIdentitiesToDeleteAfterRun = ((Boolean) runContext.get("hasIdentitiesToDelete")).booleanValue();
                        if (hasIdentitiesToDeleteAfterRun) {
                            @SuppressWarnings("unchecked") List<Identity> idToDelete = (List<Identity>) runContext.get("identitiesToDelete");
                            amountUsersToDelete = idToDelete.size();
                            // Delete all identities now and tell everybody that
                            // we are finished
                            ldapLoginManager.deletIdentities(idToDelete);
                            return StepsMainRunController.DONE_MODIFIED;
                        } else {
                            return StepsMainRunController.DONE_UNCHANGED;
                        }
                    // otherwise return without deleting anything
                    }
                };
                deleteStepController = new StepsMainRunController(ureq, getWindowControl(), start, finishCallback, null, translate("admin.deleteUser.title"), "o_sel_ldap_delete_user_wizard");
                listenTo(deleteStepController);
                getWindowControl().pushAsModalDialog(deleteStepController.getInitialComponent());
            } else {
                hasIdentitiesToDelete = false;
                showInfo("delete.step.noUsers");
                deletStartLink.setEnabled(true);
                ldapLoginManager.freeSyncLock();
            }
        }
    } else if (source == removeFallBackAuthsLink) {
        removeFallBackAuthsLink.setEnabled(false);
        ldapLoginManager.removeFallBackAuthentications();
        showInfo("opsuccess");
    }
}
Also used : CloseableCalloutWindowController(org.olat.core.gui.control.generic.closablewrapper.CloseableCalloutWindowController) Step(org.olat.core.gui.control.generic.wizard.Step) UserSearchController(org.olat.admin.user.UserSearchController) WindowControl(org.olat.core.gui.control.WindowControl) StepsRunContext(org.olat.core.gui.control.generic.wizard.StepsRunContext) LDAPEvent(org.olat.ldap.LDAPEvent) NamingException(javax.naming.NamingException) List(java.util.List) StepsMainRunController(org.olat.core.gui.control.generic.wizard.StepsMainRunController) Identity(org.olat.core.id.Identity) LdapContext(javax.naming.ldap.LdapContext) StepRunnerCallback(org.olat.core.gui.control.generic.wizard.StepRunnerCallback) UserRequest(org.olat.core.gui.UserRequest)

Example 3 with LDAPEvent

use of org.olat.ldap.LDAPEvent in project openolat by klemens.

the class LDAPLoginManagerImpl method doBatchSync.

/**
 * Execute Batch Sync. Will update all Attributes of LDAP users in OLAt, create new users and delete users in OLAT.
 * Can be configured in ldapContext.xml
 *
 * @param LDAPError
 */
@Override
public boolean doBatchSync(LDAPError errors) {
    // property read.
    synchronized (LDAPLoginManagerImpl.class) {
        if (batchSyncIsRunning) {
            // don't run twice, skip this execution
            log.info("LDAP user doBatchSync started, but another job is still running - skipping this sync");
            errors.insert("BatchSync already running by concurrent process");
            return false;
        }
    }
    WorkThreadInformations.setLongRunningTask("ldapSync");
    coordinator.getEventBus().fireEventToListenersOf(new LDAPEvent(LDAPEvent.SYNCHING), ldapSyncLockOres);
    lastSyncDate = null;
    LdapContext ctx = null;
    boolean success = false;
    try {
        acquireSyncLock();
        long startTime = System.currentTimeMillis();
        ctx = bindSystem();
        if (ctx == null) {
            errors.insert("LDAP connection ERROR");
            log.error("LDAP batch sync: LDAP connection empty", null);
            freeSyncLock();
            success = false;
            return success;
        }
        Date timeBeforeSync = new Date();
        // check server capabilities
        // Get time before sync to have a save sync time when sync is successful
        String sinceSentence = (lastSyncDate == null ? "" : " since last sync from " + lastSyncDate);
        doBatchSyncDeletedUsers(ctx, sinceSentence);
        // bind again to use an initial unmodified context. lookup of server-properties might fail otherwise!
        ctx.close();
        ctx = bindSystem();
        Map<String, LDAPUser> dnToIdentityKeyMap = new HashMap<>();
        List<LDAPUser> ldapUsers = doBatchSyncNewAndModifiedUsers(ctx, sinceSentence, dnToIdentityKeyMap, errors);
        ctx.close();
        ctx = bindSystem();
        // sync groups by LDAP groups or attributes
        doBatchSyncGroups(ctx, ldapUsers, dnToIdentityKeyMap, errors);
        // sync roles
        doBatchSyncRoles(ctx, ldapUsers, dnToIdentityKeyMap, errors);
        // update sync time and set running flag
        lastSyncDate = timeBeforeSync;
        ctx.close();
        success = true;
        log.audit("LDAP batch sync done: " + success + " in " + ((System.currentTimeMillis() - startTime) / 1000) + "s");
        return success;
    } catch (Exception e) {
        errors.insert("Unknown error");
        log.error("LDAP batch sync, unknown reason", e);
        success = false;
        return success;
    } finally {
        WorkThreadInformations.unsetLongRunningTask("ldapSync");
        freeSyncLock();
        if (ctx != null) {
            try {
                ctx.close();
            } catch (NamingException e) {
            // try but failed silently
            }
        }
        LDAPEvent endEvent = new LDAPEvent(LDAPEvent.SYNCHING_ENDED);
        endEvent.setTimestamp(new Date());
        endEvent.setSuccess(success);
        endEvent.setErrors(errors);
        coordinator.getEventBus().fireEventToListenersOf(endEvent, ldapSyncLockOres);
    }
}
Also used : HashMap(java.util.HashMap) LDAPUser(org.olat.ldap.model.LDAPUser) LDAPEvent(org.olat.ldap.LDAPEvent) Date(java.util.Date) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 4 with LDAPEvent

use of org.olat.ldap.LDAPEvent in project openolat by klemens.

the class LDAPAdminController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
 *      org.olat.core.gui.components.Component,
 *      org.olat.core.gui.control.Event)
 */
@Override
protected void event(UserRequest ureq, Component source, Event event) {
    if (source == syncStartLink) {
        // Start sync job
        // Disable start link during sync
        syncStartLink.setEnabled(false);
        LDAPEvent ldapEvent = new LDAPEvent(LDAPEvent.DO_SYNCHING);
        CoordinatorManager.getInstance().getCoordinator().getEventBus().fireEventToListenersOf(ldapEvent, LDAPLoginManager.ldapSyncLockOres);
        showInfo("admin.synchronize.started");
    } else if (source == syncOneUserLink) {
        userSearchCtrl = new UserSearchController(ureq, getWindowControl(), false);
        listenTo(userSearchCtrl);
        calloutCtr = new CloseableCalloutWindowController(ureq, getWindowControl(), userSearchCtrl.getInitialComponent(), syncOneUserLink, null, true, null);
        calloutCtr.addDisposableChildController(userSearchCtrl);
        calloutCtr.activate();
        listenTo(calloutCtr);
    } else if (source == deletStartLink) {
        // cancel if some one else is making sync or delete job
        if (!ldapLoginManager.acquireSyncLock()) {
            showError("delete.error.lock");
        } else {
            deletStartLink.setEnabled(false);
            // check and get LDAP connection
            LdapContext ctx = ldapLoginManager.bindSystem();
            if (ctx == null) {
                showError("delete.error.connection");
                return;
            }
            // get deleted users
            identitiesToDelete = ldapLoginManager.getIdentitysDeletedInLdap(ctx);
            try {
                ctx.close();
            } catch (NamingException e) {
                showError("delete.error.connection.close");
                logError("Could not close LDAP connection on manual delete sync", e);
            }
            if (identitiesToDelete != null && identitiesToDelete.size() != 0) {
                hasIdentitiesToDelete = true;
                /*
					 * start step which spawns the whole wizard
					 */
                Step start = new DeletStep00(ureq, hasIdentitiesToDelete, identitiesToDelete);
                /*
					 * wizard finish callback called after "finish" is called
					 */
                StepRunnerCallback finishCallback = new StepRunnerCallback() {

                    public Step execute(UserRequest uureq, WindowControl control, StepsRunContext runContext) {
                        hasIdentitiesToDeleteAfterRun = ((Boolean) runContext.get("hasIdentitiesToDelete")).booleanValue();
                        if (hasIdentitiesToDeleteAfterRun) {
                            @SuppressWarnings("unchecked") List<Identity> idToDelete = (List<Identity>) runContext.get("identitiesToDelete");
                            amountUsersToDelete = idToDelete.size();
                            // Delete all identities now and tell everybody that
                            // we are finished
                            ldapLoginManager.deletIdentities(idToDelete);
                            return StepsMainRunController.DONE_MODIFIED;
                        } else {
                            return StepsMainRunController.DONE_UNCHANGED;
                        }
                    // otherwise return without deleting anything
                    }
                };
                deleteStepController = new StepsMainRunController(ureq, getWindowControl(), start, finishCallback, null, translate("admin.deleteUser.title"), "o_sel_ldap_delete_user_wizard");
                listenTo(deleteStepController);
                getWindowControl().pushAsModalDialog(deleteStepController.getInitialComponent());
            } else {
                hasIdentitiesToDelete = false;
                showInfo("delete.step.noUsers");
                deletStartLink.setEnabled(true);
                ldapLoginManager.freeSyncLock();
            }
        }
    } else if (source == removeFallBackAuthsLink) {
        removeFallBackAuthsLink.setEnabled(false);
        ldapLoginManager.removeFallBackAuthentications();
        showInfo("opsuccess");
    }
}
Also used : CloseableCalloutWindowController(org.olat.core.gui.control.generic.closablewrapper.CloseableCalloutWindowController) Step(org.olat.core.gui.control.generic.wizard.Step) UserSearchController(org.olat.admin.user.UserSearchController) WindowControl(org.olat.core.gui.control.WindowControl) StepsRunContext(org.olat.core.gui.control.generic.wizard.StepsRunContext) LDAPEvent(org.olat.ldap.LDAPEvent) NamingException(javax.naming.NamingException) List(java.util.List) StepsMainRunController(org.olat.core.gui.control.generic.wizard.StepsMainRunController) Identity(org.olat.core.id.Identity) LdapContext(javax.naming.ldap.LdapContext) StepRunnerCallback(org.olat.core.gui.control.generic.wizard.StepRunnerCallback) UserRequest(org.olat.core.gui.UserRequest)

Aggregations

NamingException (javax.naming.NamingException)4 LdapContext (javax.naming.ldap.LdapContext)4 LDAPEvent (org.olat.ldap.LDAPEvent)4 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 AuthenticationException (javax.naming.AuthenticationException)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 UserSearchController (org.olat.admin.user.UserSearchController)2 UserRequest (org.olat.core.gui.UserRequest)2 WindowControl (org.olat.core.gui.control.WindowControl)2 CloseableCalloutWindowController (org.olat.core.gui.control.generic.closablewrapper.CloseableCalloutWindowController)2 Step (org.olat.core.gui.control.generic.wizard.Step)2 StepRunnerCallback (org.olat.core.gui.control.generic.wizard.StepRunnerCallback)2 StepsMainRunController (org.olat.core.gui.control.generic.wizard.StepsMainRunController)2 StepsRunContext (org.olat.core.gui.control.generic.wizard.StepsRunContext)2 Identity (org.olat.core.id.Identity)2 LDAPUser (org.olat.ldap.model.LDAPUser)2