Search in sources :

Example 81 with UserSession

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

the class ExecutionsImpl method startExecution.

public ExecutionContext startExecution(String key, String group) {
    if (ExecutionContextHolder.getCurrentContext() != null) {
        throw new IllegalStateException("Execution context already started");
    }
    log.debug("Start execution context: group={}, key={}", group, key);
    ExecutionContextImpl context = new ExecutionContextImpl(Thread.currentThread(), key, group, timeSource.currentTimestamp());
    UserSession userSession = userSessionSource.getUserSession();
    CopyOnWriteArrayList<ExecutionContextImpl> executions = userSession.getLocalAttribute(EXECUTIONS_ATTR);
    if (executions == null) {
        userSession.setLocalAttributeIfAbsent(EXECUTIONS_ATTR, new CopyOnWriteArrayList<>());
        executions = userSession.getLocalAttribute(EXECUTIONS_ATTR);
    }
    executions.add(context);
    ExecutionContextHolder.setCurrentContext(context);
    return context;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession)

Example 82 with UserSession

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

the class ConfigCacheStrategy method updateCacheInBackground.

protected void updateCacheInBackground() {
    UserSession userSession = cacheUserSessionProvider.getUserSession();
    if (userSession == null) {
        // cache user session unavailable
        return;
    }
    try {
        AppContext.setSecurityContext(new SecurityContext(userSession));
        Map<String, String> cachedPropertiesFromServer = Collections.unmodifiableMap(configStorageService.getDbProperties());
        readWriteLock.writeLock().lock();
        try {
            cachedProperties = cachedPropertiesFromServer;
            lastUsedTs = System.currentTimeMillis();
        } finally {
            readWriteLock.writeLock().unlock();
        }
    } catch (NoUserSessionException e) {
        log.warn("Cache user session expired", e);
    } catch (Exception e) {
        log.error("Unable to update config storage cache", e);
    } finally {
        AppContext.setSecurityContext(null);
        backgroundUpdateTriggered = false;
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 83 with UserSession

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

the class FileUploadController method upload.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null)
        return;
    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        InputStream is = request.getInputStream();
        if (is == null) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        FileDescriptor fd = getFileDescriptor(request, response);
        if (fd == null)
            return;
        try {
            fileStorage.saveStream(fd, is);
        } catch (FileStorageException e) {
            log.error("Unable to upload file", e);
            response.sendError(e.getType().getHttpStatus());
        } finally {
            IOUtils.closeQuietly(is);
        }
    } finally {
        AppContext.setSecurityContext(null);
    }
}
Also used : InputStream(java.io.InputStream) UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 84 with UserSession

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

the class FileUploadController method getSession.

private UserSession getSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UUID sessionId;
    try {
        sessionId = UUID.fromString(request.getParameter("s"));
    } catch (Exception e) {
        log.error("Error parsing sessionId from URL param", e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    UserSession session = userSessions.getAndRefresh(sessionId);
    if (session == null)
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    return session;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) UUID(java.util.UUID) IOException(java.io.IOException) FileStorageException(com.haulmont.cuba.core.global.FileStorageException)

Example 85 with UserSession

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

the class LogDownloadController method getSession.

protected UserSession getSession(String sessionId, HttpServletResponse response) throws IOException {
    UUID sessionUUID;
    try {
        sessionUUID = UUID.fromString(sessionId);
    } catch (Exception e) {
        log.error("Error parsing sessionId from URL param", e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    UserSession session = userSessions.getAndRefresh(sessionUUID);
    if (session == null)
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    return session;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) UUID(java.util.UUID) LogFileNotFoundException(com.haulmont.cuba.core.sys.logging.LogFileNotFoundException) IOException(java.io.IOException)

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