Search in sources :

Example 86 with UserSession

use of org.olat.core.util.UserSession in project OpenOLAT by OpenOLAT.

the class MapperServiceTest method testCleanUpMapper_notSerializable_byMappers.

@Test
public void testCleanUpMapper_notSerializable_byMappers() {
    // number of currently hold mappers
    int numOfMappers = mapperService.inMemoryCount();
    // create a mapper
    UserSession session = createUserSession();
    DummyMapper mapper = new DummyMapper();
    MapperKey mapperKey = mapperService.register(session, mapper);
    dbInstance.commitAndCloseSession();
    // retrieve the mapper
    Mapper reloadedMapper = mapperService.getMapperById(session, mapperKey.getMapperId());
    Assert.assertNotNull(reloadedMapper);
    Assert.assertFalse(numOfMappers == mapperService.inMemoryCount());
    // cleanup
    mapperService.cleanUp(Collections.<MapperKey>singletonList(mapperKey));
    // check 1
    Mapper deletedMapper = mapperService.getMapperById(session, mapperKey.getMapperId());
    Assert.assertNull(deletedMapper);
}
Also used : UserSession(org.olat.core.util.UserSession) MapperKey(org.olat.core.dispatcher.mapper.manager.MapperKey) Test(org.junit.Test)

Example 87 with UserSession

use of org.olat.core.util.UserSession in project OpenOLAT by OpenOLAT.

the class MapperServiceTest method testChangingMapper_serializableSessionChanged.

@Test
public void testChangingMapper_serializableSessionChanged() {
    // create a mapper
    int initialNumOfMappers = mapperService.inMemoryCount();
    UserSession session = createUserSession();
    PersistentMapper mapper = new PersistentMapper(UUID.randomUUID().toString());
    MapperKey mapperKey = mapperService.register(session, mapper);
    dbInstance.commitAndCloseSession();
    // retrieve the mapper
    PersistentMapper reloadedMapper = (PersistentMapper) mapperService.getMapperById(session, mapperKey.getMapperId());
    Assert.assertNotNull(reloadedMapper);
    Assert.assertEquals(mapper, reloadedMapper);
    Assert.assertFalse(initialNumOfMappers == mapperService.inMemoryCount());
    // changing the key in the mapper
    String modKey = UUID.randomUUID().toString();
    reloadedMapper.setKey(modKey);
    // remove in memory mappers
    mapperService.cleanUp(Collections.<MapperKey>singletonList(mapperKey));
    mapperService.cleanUp(session.getSessionInfo().getSession().getId());
    Assert.assertEquals(initialNumOfMappers, mapperService.inMemoryCount());
    // reloaded episode 2
    UserSession session2 = createUserSession();
    PersistentMapper reloadedMapper2 = (PersistentMapper) mapperService.getMapperById(session2, mapperKey.getMapperId());
    Assert.assertNotNull(reloadedMapper2);
    Assert.assertEquals(modKey, reloadedMapper2.getKey());
}
Also used : UserSession(org.olat.core.util.UserSession) MapperKey(org.olat.core.dispatcher.mapper.manager.MapperKey) Test(org.junit.Test)

Example 88 with UserSession

use of org.olat.core.util.UserSession in project OpenOLAT by OpenOLAT.

the class ShareLinkController method event.

@Override
protected void event(UserRequest ureq, Component source, Event event) {
    UserSession usess = ureq.getUserSession();
    if (source == shareLinkVC && "setLandingPage".equals(event.getCommand()) && usess != null && usess.isAuthenticated()) {
        HistoryPoint p = usess.getLastHistoryPoint();
        if (p != null && StringHelper.containsNonWhitespace(p.getBusinessPath())) {
            List<ContextEntry> ces = p.getEntries();
            String landingPage = BusinessControlFactory.getInstance().getAsURIString(ces, true);
            int start = landingPage.indexOf("/url/");
            if (start != -1) {
                // start with / after /url
                landingPage = landingPage.substring(start + 4);
            }
            // update user prefs
            Preferences prefs = usess.getGuiPreferences();
            prefs.put(WindowManager.class, "landing-page", landingPage);
            prefs.save();
            getWindowControl().getWindowBackOffice().sendCommandTo(new JSCommand("showInfoBox(\"" + translate("info.header") + "\",\"" + translate("landingpage.set.message") + "\");"));
        }
    }
}
Also used : UserSession(org.olat.core.util.UserSession) JSCommand(org.olat.core.gui.control.winmgr.JSCommand) Preferences(org.olat.core.util.prefs.Preferences) HistoryPoint(org.olat.core.id.context.HistoryPoint) ContextEntry(org.olat.core.id.context.ContextEntry) HistoryPoint(org.olat.core.id.context.HistoryPoint)

Example 89 with UserSession

use of org.olat.core.util.UserSession in project OpenOLAT by OpenOLAT.

the class LDAPAuthenticationController method event.

@Override
protected void event(UserRequest ureq, Controller source, Event event) {
    LDAPError ldapError = new LDAPError();
    if (source == loginForm && event == Event.DONE_EVENT) {
        String login = loginForm.getLogin();
        String pass = loginForm.getPass();
        if (loginModule.isLoginBlocked(login)) {
            // do not proceed when already blocked
            showError("login.blocked", loginModule.getAttackPreventionTimeoutMin().toString());
            getLogger().audit("Login attempt on already blocked login for " + login + ". IP::" + ureq.getHttpReq().getRemoteAddr(), null);
            return;
        }
        authenticatedIdentity = ldapLoginManager.authenticate(login, pass, ldapError);
        if (!ldapError.isEmpty()) {
            final String errStr = ldapError.get();
            if ("login.notauthenticated".equals(errStr)) {
                // user exists in LDAP, authentication was ok, but user
                // has not got the OLAT service or has not been created by now
                getWindowControl().setError(translate("login.notauthenticated"));
                return;
            } else {
                // tell about the error again
                ldapError.insert(errStr);
            }
        }
        if (authenticatedIdentity != null) {
            provider = LDAPAuthenticationController.PROVIDER_LDAP;
            try {
                // prevents database timeout
                dbInstance.commitAndCloseSession();
            } catch (Exception e) {
                log.error("", e);
            }
        } else {
            // try fallback to OLAT provider if configured
            if (ldapLoginModule.isCacheLDAPPwdAsOLATPwdOnLogin()) {
                authenticatedIdentity = olatAuthenticationSpi.authenticate(null, login, pass);
            }
            if (authenticatedIdentity != null) {
                provider = BaseSecurityModule.getDefaultAuthProviderIdentifier();
            }
        }
        // Still not found? register for hacking attempts
        if (authenticatedIdentity == null) {
            if (loginModule.registerFailedLoginAttempt(login)) {
                logAudit("Too many failed login attempts for " + login + ". Login blocked. IP::" + ureq.getHttpReq().getRemoteAddr(), null);
                showError("login.blocked", loginModule.getAttackPreventionTimeoutMin().toString());
            } else {
                showError("login.error", ldapError.get());
            }
            return;
        } else {
            try {
                String language = authenticatedIdentity.getUser().getPreferences().getLanguage();
                UserSession usess = ureq.getUserSession();
                if (StringHelper.containsNonWhitespace(language)) {
                    usess.setLocale(I18nManager.getInstance().getLocaleOrDefault(language));
                }
            } catch (Exception e) {
                logError("Cannot set the user language", e);
            }
        }
        loginModule.clearFailedLoginAttempts(login);
        // Check if disclaimer has been accepted
        if (registrationManager.needsToConfirmDisclaimer(authenticatedIdentity)) {
            // accept disclaimer first
            removeAsListenerAndDispose(disclaimerCtr);
            disclaimerCtr = new DisclaimerController(ureq, getWindowControl());
            listenTo(disclaimerCtr);
            removeAsListenerAndDispose(cmc);
            cmc = new CloseableModalController(getWindowControl(), translate("close"), disclaimerCtr.getInitialComponent());
            listenTo(cmc);
            cmc.activate();
        } else {
            // disclaimer acceptance not required
            doLoginAndRegister(authenticatedIdentity, ureq, provider);
        }
    }
    if (source == subController) {
        if (event == Event.CANCELLED_EVENT || event == Event.DONE_EVENT) {
            cmc.deactivate();
        }
    } else if (source == disclaimerCtr) {
        cmc.deactivate();
        if (event == Event.DONE_EVENT) {
            // User accepted disclaimer, do login now
            registrationManager.setHasConfirmedDislaimer(authenticatedIdentity);
            doLoginAndRegister(authenticatedIdentity, ureq, provider);
        } else if (event == Event.CANCELLED_EVENT) {
            // User did not accept, workflow ends here
            showWarning("disclaimer.form.cancelled");
        }
    } else if (source == cmc) {
        // User did close disclaimer window, workflow ends here
        showWarning("disclaimer.form.cancelled");
    }
}
Also used : DisclaimerController(org.olat.registration.DisclaimerController) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) UserSession(org.olat.core.util.UserSession) LDAPError(org.olat.ldap.LDAPError) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 90 with UserSession

use of org.olat.core.util.UserSession in project OpenOLAT by OpenOLAT.

the class OLATAuthenticationController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
 */
@Override
public void event(UserRequest ureq, Controller source, Event event) {
    if (source == loginForm && event == Event.DONE_EVENT) {
        String login = loginForm.getLogin();
        String pass = loginForm.getPass();
        if (loginModule.isLoginBlocked(login)) {
            // do not proceed when blocked
            showError("login.blocked", loginModule.getAttackPreventionTimeoutMin().toString());
            getLogger().audit("Login attempt on already blocked login for " + login + ". IP::" + ureq.getHttpReq().getRemoteAddr(), null);
            return;
        }
        authenticatedIdentity = olatAuthenticationSpi.authenticate(null, login, pass);
        if (authenticatedIdentity == null) {
            if (loginModule.registerFailedLoginAttempt(login)) {
                getLogger().audit("Too many failed login attempts for " + login + ". Login blocked. IP::" + ureq.getHttpReq().getRemoteAddr(), null);
                showError("login.blocked", loginModule.getAttackPreventionTimeoutMin().toString());
                return;
            } else {
                showError("login.error", WebappHelper.getMailConfig("mailReplyTo"));
                return;
            }
        } else {
            try {
                String language = authenticatedIdentity.getUser().getPreferences().getLanguage();
                UserSession usess = ureq.getUserSession();
                if (StringHelper.containsNonWhitespace(language)) {
                    usess.setLocale(I18nManager.getInstance().getLocaleOrDefault(language));
                }
            } catch (Exception e) {
                logError("Cannot set the user language", e);
            }
        }
        loginModule.clearFailedLoginAttempts(login);
        // Check if disclaimer has been accepted
        if (registrationManager.needsToConfirmDisclaimer(authenticatedIdentity)) {
            // accept disclaimer first
            removeAsListenerAndDispose(disclaimerCtr);
            disclaimerCtr = new DisclaimerController(ureq, getWindowControl());
            listenTo(disclaimerCtr);
            removeAsListenerAndDispose(cmc);
            cmc = new CloseableModalController(getWindowControl(), translate("close"), disclaimerCtr.getInitialComponent());
            listenTo(cmc);
            cmc.activate();
        } else {
            // disclaimer acceptance not required
            authenticated(ureq, authenticatedIdentity);
        }
    } else if (source == disclaimerCtr) {
        cmc.deactivate();
        if (event == Event.DONE_EVENT) {
            // disclaimer accepted
            registrationManager.setHasConfirmedDislaimer(authenticatedIdentity);
            authenticated(ureq, authenticatedIdentity);
        }
    } else if (cmc == source) {
        cleanUp();
    }
    if (source == subController) {
        if (event == Event.CANCELLED_EVENT) {
            cmc.deactivate();
            cleanUp();
        }
    }
}
Also used : DisclaimerController(org.olat.registration.DisclaimerController) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) UserSession(org.olat.core.util.UserSession) OLATSecurityException(org.olat.core.logging.OLATSecurityException)

Aggregations

UserSession (org.olat.core.util.UserSession)146 UserSessionManager (org.olat.core.util.session.UserSessionManager)26 Identity (org.olat.core.id.Identity)22 Roles (org.olat.core.id.Roles)20 SessionInfo (org.olat.core.util.SessionInfo)20 HttpSession (javax.servlet.http.HttpSession)18 UserRequest (org.olat.core.gui.UserRequest)18 Test (org.junit.Test)16 MapperKey (org.olat.core.dispatcher.mapper.manager.MapperKey)16 UserRequestImpl (org.olat.core.gui.UserRequestImpl)16 ContextEntry (org.olat.core.id.context.ContextEntry)14 IOException (java.io.IOException)12 AssertException (org.olat.core.logging.AssertException)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 Window (org.olat.core.gui.components.Window)10 UnknownHostException (java.net.UnknownHostException)8 ArrayList (java.util.ArrayList)8 ChiefController (org.olat.core.gui.control.ChiefController)8 Preferences (org.olat.core.util.prefs.Preferences)8 InetAddress (java.net.InetAddress)6