Search in sources :

Example 31 with UserSession

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

the class UserSessionManager method processSignOnOffEvent.

private void processSignOnOffEvent(SignOnOffEvent se) {
    try {
        boolean debug = log.isDebug();
        if (debug)
            log.debug("event() START");
        if (debug)
            log.debug("event() is SignOnOffEvent. isSignOn=" + se.isSignOn());
        if (!se.isEventOnThisNode()) {
            // - Single OLAT Instance is never passing by here.
            if (se.isSignOn()) {
                // -> remember other nodes logged usernames
                if (debug)
                    log.debug("event() adding to authUsersNamesOtherNodes: " + se.getIdentityKey());
                authUsersNamesOtherNodes.add(se.getIdentityKey());
                UserSession usess = getUserSessionForGui(se.getIdentityKey());
                if (usess != null && usess.getSessionInfo() != null && se.getIdentityKey().equals(usess.getSessionInfo().getIdentityKey()) && !usess.getSessionInfo().isWebDAV() && !usess.getRoles().isGuestOnly()) {
                    // if this listening UserSession instance is from the same user
                    // and it is not a WebDAV Session, and it is not GuestSession
                    // => log user off on this node
                    signOffAndClearWithout(usess);
                    usess.init();
                }
            } else {
                // -> remove from other nodes logged on list.
                if (debug)
                    log.debug("event() removing from authUsersNamesOtherNodes: " + se.getIdentityKey());
                authUsersNamesOtherNodes.remove(se.getIdentityKey());
            }
        }
        if (debug)
            log.debug("event() END");
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : UserSession(org.olat.core.util.UserSession) AssertException(org.olat.core.logging.AssertException)

Example 32 with UserSession

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

the class UserSessionManager method getUserSessionForGui.

/**
 * Lookup non-webdav, non-REST UserSession for identity key.
 * @param identityKey
 * @return user-session or null when no session was founded.
 */
private UserSession getUserSessionForGui(Long identityKey) {
    UserSession identitySession = null;
    if (identityKey != null) {
        // do not call from somewhere else then signOffAndClear!!
        Optional<UserSession> optionalSession = authUserSessions.stream().filter(userSession -> {
            Identity identity = userSession.getIdentity();
            if (identity != null && identityKey.equals(identity.getKey()) && userSession.getSessionInfo() != null && !userSession.getSessionInfo().isWebDAV() && !userSession.getSessionInfo().isREST()) {
                return true;
            }
            return false;
        }).findFirst();
        identitySession = optionalSession.isPresent() ? optionalSession.get() : null;
    }
    return identitySession;
}
Also used : IdentityEnvironment(org.olat.core.id.IdentityEnvironment) SignOnOffEvent(org.olat.core.util.SignOnOffEvent) AssertException(org.olat.core.logging.AssertException) Settings(org.olat.core.helpers.Settings) CoreLoggingResourceable(org.olat.core.logging.activity.CoreLoggingResourceable) Preferences(org.olat.core.util.prefs.Preferences) CacheWrapper(org.olat.core.util.cache.CacheWrapper) Autowired(org.springframework.beans.factory.annotation.Autowired) ThreadLocalUserActivityLogger(org.olat.core.logging.activity.ThreadLocalUserActivityLogger) ThreadLocalUserActivityLoggerInstaller(org.olat.core.logging.activity.ThreadLocalUserActivityLoggerInstaller) TreeSet(java.util.TreeSet) UserSession(org.olat.core.util.UserSession) HashSet(java.util.HashSet) Event(org.olat.core.gui.control.Event) OLATResourceable(org.olat.core.id.OLATResourceable) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GenericEventListener(org.olat.core.util.event.GenericEventListener) OresHelper(org.olat.core.util.resource.OresHelper) SessionInfo(org.olat.core.util.SessionInfo) Service(org.springframework.stereotype.Service) Disposable(org.olat.core.gui.control.Disposable) OLog(org.olat.core.logging.OLog) UserActivityLoggerImpl(org.olat.core.logging.activity.UserActivityLoggerImpl) HttpSession(javax.servlet.http.HttpSession) Iterator(java.util.Iterator) OlatLoggingAction(org.olat.core.logging.activity.OlatLoggingAction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) List(java.util.List) DB(org.olat.core.commons.persistence.DB) Identity(org.olat.core.id.Identity) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Comparator(java.util.Comparator) Tracing(org.olat.core.logging.Tracing) CoordinatorManager(org.olat.core.util.coordinate.CoordinatorManager) Roles(org.olat.core.id.Roles) HistoryManager(org.olat.core.id.context.HistoryManager) UserSession(org.olat.core.util.UserSession) Identity(org.olat.core.id.Identity)

Example 33 with UserSession

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

the class UserSessionManager method invalidateOldestSessions.

/**
 * Invalidate a given number of oldest (last-click-time) sessions except admin-sessions.
 * @param nbrSessions  number of sessions whisch will be invalidated
 * @return  Number of invalidated sessions.
 */
public int invalidateOldestSessions(int nbrSessions) {
    int invalidateCounter = 0;
    // 1. Copy authUserSessions in sorted TreeMap
    // This is the Comparator that will be used to sort the TreeSet:
    Comparator<UserSession> sessionComparator = new Comparator<UserSession>() {

        @Override
        public int compare(UserSession o1, UserSession o2) {
            Long long1 = new Long((o1).getSessionInfo().getLastClickTime());
            Long long2 = new Long((o2).getSessionInfo().getLastClickTime());
            return long1.compareTo(long2);
        }
    };
    // clusterNOK ?? invalidate only locale sessions ?
    TreeSet<UserSession> sortedSet = new TreeSet<UserSession>(sessionComparator);
    sortedSet.addAll(authUserSessions);
    int i = 0;
    for (Iterator<UserSession> iterator = sortedSet.iterator(); iterator.hasNext() && i++ < nbrSessions; ) {
        try {
            UserSession userSession = iterator.next();
            if (!userSession.getRoles().isOLATAdmin() && !userSession.getSessionInfo().isWebDAV()) {
                internSignOffAndClear(userSession);
                invalidateCounter++;
            }
        } catch (Throwable th) {
            log.warn("Error signOffAndClear ", th);
        }
    }
    return invalidateCounter;
}
Also used : TreeSet(java.util.TreeSet) UserSession(org.olat.core.util.UserSession) Comparator(java.util.Comparator)

Example 34 with UserSession

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

the class UserSessionManager method getUserSessionIfAlreadySet.

/**
 * Return the UserSession of the given request if it is already set or null otherwise
 * @param hreq
 * @return
 */
public UserSession getUserSessionIfAlreadySet(HttpServletRequest hreq) {
    HttpSession session = hreq.getSession(false);
    if (session == null) {
        return null;
    }
    UserSession us = (UserSession) session.getAttribute(USERSESSIONKEY);
    setHttpSessionTimeout(session, us);
    return us;
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserSession(org.olat.core.util.UserSession)

Example 35 with UserSession

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

the class UserSessionManager method getUserSession.

/**
 * @param session
 * @return associated user session
 */
public UserSession getUserSession(HttpSession session) {
    UserSession us = (UserSession) session.getAttribute(USERSESSIONKEY);
    if (us == null) {
        synchronized (session) {
            // o_clusterOK by:fj
            us = (UserSession) session.getAttribute(USERSESSIONKEY);
            if (us == null) {
                us = new UserSession();
                // triggers the
                session.setAttribute(USERSESSIONKEY, us);
            // valueBoundEvent -> nothing
            // more to do here
            }
        }
    }
    // set a possible changed session timeout interval
    setHttpSessionTimeout(session, us);
    return us;
}
Also used : UserSession(org.olat.core.util.UserSession)

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