Search in sources :

Example 16 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class CubaRemoteInvocationFactory method createRemoteInvocation.

@Override
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
    SecurityContext securityContext = AppContext.getSecurityContext();
    CubaRemoteInvocation remoteInvocation = new CubaRemoteInvocation(methodInvocation, securityContext == null ? null : securityContext.getSessionId());
    if (securityContext != null) {
        UserSession session = securityContext.getSession();
        if (session instanceof ClientBasedSession && ((ClientBasedSession) session).hasRequestScopedInfo()) {
            remoteInvocation.setLocale(session.getLocale() != null ? session.getLocale().toLanguageTag() : null);
            remoteInvocation.setTimeZone(session.getTimeZone());
            remoteInvocation.setAddress(session.getAddress());
            remoteInvocation.setClientInfo(session.getClientInfo());
        }
    }
    return remoteInvocation;
}
Also used : ClientBasedSession(com.haulmont.cuba.security.global.ClientBasedSession) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext)

Example 17 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class StickySessionServerSelector method getSessionUrlsHolder.

@Nullable
protected SessionUrlsHolder getSessionUrlsHolder() {
    SecurityContext securityContext = AppContext.getSecurityContext();
    if (securityContext == null)
        return null;
    UserSession session = securityContext.getSession();
    if (session == null || session instanceof ClientBasedSession && ((ClientBasedSession) session).hasRequestScopedInfo())
        return anonymousSessionUrlsHolder;
    return new UserSessionUrlsHolder(session);
}
Also used : ClientBasedSession(com.haulmont.cuba.security.global.ClientBasedSession) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) Nullable(javax.annotation.Nullable)

Example 18 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class CubaRemoteInvocationExecutor method invoke.

@Override
public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    if (invocation instanceof CubaRemoteInvocation) {
        CubaRemoteInvocation cubaInvocation = (CubaRemoteInvocation) invocation;
        UUID sessionId = cubaInvocation.getSessionId();
        if (sessionId != null) {
            UserSession session = userSessions.getAndRefresh(sessionId);
            if (session == null) {
                ServerConfig serverConfig = configuration.getConfig(ServerConfig.class);
                String sessionProviderUrl = serverConfig.getUserSessionProviderUrl();
                if (StringUtils.isNotBlank(sessionProviderUrl)) {
                    log.debug("User session {} not found, trying to get it from {}", sessionId, sessionProviderUrl);
                    try {
                        HttpServiceProxy proxyFactory = new HttpServiceProxy(getServerSelector(sessionProviderUrl));
                        proxyFactory.setServiceUrl("cuba_TrustedClientService");
                        proxyFactory.setServiceInterface(TrustedClientService.class);
                        proxyFactory.afterPropertiesSet();
                        TrustedClientService trustedClientService = (TrustedClientService) proxyFactory.getObject();
                        if (trustedClientService != null) {
                            UserSession userSession = trustedClientService.findSession(serverConfig.getTrustedClientPassword(), sessionId);
                            if (userSession != null) {
                                userSessions.add(userSession);
                            } else {
                                log.debug("User session {} not found on {}", sessionId, sessionProviderUrl);
                            }
                        }
                    } catch (Exception e) {
                        log.error("Error getting user session from {}", sessionProviderUrl, e);
                    }
                }
            }
            AppContext.setSecurityContext(new SecurityContext(sessionId));
        }
        if (cubaInvocation.getLocale() != null) {
            Locale requestLocale = Locale.forLanguageTag(cubaInvocation.getLocale());
            if (!globalConfig.getAvailableLocales().containsValue(requestLocale)) {
                requestLocale = null;
            }
            UserInvocationContext.setRequestScopeInfo(sessionId, requestLocale, cubaInvocation.getTimeZone(), cubaInvocation.getAddress(), cubaInvocation.getClientInfo());
        }
    }
    Object result;
    try {
        result = invocation.invoke(targetObject);
    } finally {
        AppContext.setSecurityContext(null);
        UserInvocationContext.clearRequestScopeInfo();
    }
    return result;
}
Also used : Locale(java.util.Locale) ServerConfig(com.haulmont.cuba.core.app.ServerConfig) TrustedClientService(com.haulmont.cuba.security.app.TrustedClientService) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) UUID(java.util.UUID) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 19 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class UserSessionServiceBean method postMessage.

@Override
public void postMessage(List<UUID> sessionIds, String message) {
    long time = timeSource.currentTimeMillis();
    for (UUID sessionId : sessionIds) {
        UserSession userSession = userSessions.get(sessionId);
        if (userSession != null && !userSession.isSystem()) {
            userSession.setAttribute(MESSAGE_ATTR_PREFIX + time, message);
            userSessions.propagate(sessionId);
        }
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession)

Example 20 with UserSession

use of com.haulmont.cuba.security.global.UserSession in project cuba by cuba-platform.

the class UserSessionServiceBean method setSessionLocale.

@Override
public void setSessionLocale(UUID sessionId, Locale locale) {
    UserSession userSession = userSessions.getNN(sessionId);
    checkSession(userSession);
    userSession.setLocale(locale);
    userSessions.propagate(sessionId);
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession)

Aggregations

UserSession (com.haulmont.cuba.security.global.UserSession)127 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)29 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)25 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)24 LoginException (com.haulmont.cuba.security.global.LoginException)23 Test (org.junit.Test)19 User (com.haulmont.cuba.security.entity.User)17 UUID (java.util.UUID)16 IOException (java.io.IOException)14 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)12 ArrayList (java.util.ArrayList)11 Locale (java.util.Locale)11 List (java.util.List)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)7 LogFileNotFoundException (com.haulmont.cuba.core.sys.logging.LogFileNotFoundException)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4