Search in sources :

Example 66 with UserSession

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

the class DefaultApp method connectionStateChanged.

@Override
public void connectionStateChanged(StateChangeEvent event) {
    Connection connection = event.getSource();
    log.debug("connectionStateChanged connected: {}, authenticated: {}", connection.isConnected(), connection.isAuthenticated());
    cleanupBackgroundTasks();
    closeAllWindows();
    clearSettingsCache();
    if (connection.isConnected()) {
        UserSession userSession = connection.getSessionNN();
        setLocale(userSession.getLocale());
        // substitution listeners are cleared by connection on logout
        connection.addUserSubstitutionListener(this);
        preventSessionFixation(connection, userSession);
        initExceptionHandlers(true);
        initializeUi();
        if (linkHandler != null && linkHandler.canHandleLink()) {
            linkHandler.handle();
            linkHandler = null;
        }
        afterLoggedIn();
        publishAppLoggedInEvent();
    } else {
        initExceptionHandlers(false);
        VaadinRequest currentRequest = VaadinService.getCurrentRequest();
        if (currentRequest != null) {
            Locale requestLocale = currentRequest.getLocale();
            setLocale(resolveLocale(requestLocale));
        }
        try {
            connection.login(new AnonymousUserCredentials(getLocale()));
        } catch (LoginException e) {
            throw new RuntimeException("Unable to login as anonymous!");
        }
        publishAppLoggedOutEvent(event.getPreviousSession());
    }
}
Also used : Locale(java.util.Locale) UserSession(com.haulmont.cuba.security.global.UserSession) Connection(com.haulmont.cuba.web.Connection) LoginException(com.haulmont.cuba.security.global.LoginException) AnonymousUserCredentials(com.haulmont.cuba.web.security.AnonymousUserCredentials)

Example 67 with UserSession

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

the class BaseIdpSessionFilter method getWebAppUrl.

protected String getWebAppUrl() {
    if (webAppUrl == null) {
        synchronized (this) {
            if (webAppUrl == null) {
                UserSession systemSession;
                try {
                    systemSession = trustedClientService.getSystemSession(webAuthConfig.getTrustedClientPassword());
                } catch (LoginException e) {
                    throw new RuntimeException("Unable to get systemSession", e);
                }
                // webAppUrl can be overridden in DB, thus we need SecurityContext to obtain it from middleware
                withSecurityContext(new SecurityContext(systemSession), () -> {
                    String webAppUrl = globalConfig.getWebAppUrl();
                    if (!webAppUrl.endsWith("/")) {
                        webAppUrl += "/";
                    }
                    this.webAppUrl = webAppUrl;
                });
            }
        }
    }
    return this.webAppUrl;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) AppContext.withSecurityContext(com.haulmont.cuba.core.sys.AppContext.withSecurityContext) LoginException(com.haulmont.cuba.security.global.LoginException)

Example 68 with UserSession

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

the class WebUserSessionSource method getUserSessionFromMiddleware.

protected UserSession getUserSessionFromMiddleware(UUID sessionId) {
    UserSession userSession = null;
    HttpServletRequest request = null;
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        request = ((ServletRequestAttributes) requestAttributes).getRequest();
    }
    if (request != null) {
        userSession = (UserSession) request.getAttribute(REQUEST_ATTR);
    }
    if (userSession != null) {
        return userSession;
    }
    userSession = userSessionService.getUserSession(sessionId);
    if (request != null) {
        request.setAttribute(REQUEST_ATTR, userSession);
    }
    return userSession;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserSession(com.haulmont.cuba.security.global.UserSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 69 with UserSession

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

the class UserSwitchLinkHandlerProcessor method handle.

@Override
public void handle(ExternalLinkContext linkContext) {
    UUID userId = getUUID(linkContext.getRequestParams().get("user"));
    assert userId != null;
    UserSession userSession = App.getInstance().getConnection().getSession();
    if (userSession == null) {
        log.warn("No user session");
        return;
    }
    if (!userSession.getCurrentOrSubstitutedUser().getId().equals(userId)) {
        substituteUserAndOpenWindow(linkContext, userId);
    } else {
        screenHandler.handle(linkContext);
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) UUID(java.util.UUID)

Example 70 with UserSession

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

the class MainTabSheetActionHandler method getActions.

@Override
public Action[] getActions(Object target, Object sender) {
    if (!initialized) {
        Messages messages = AppBeans.get(Messages.NAME);
        closeAllTabs = new com.vaadin.event.Action(messages.getMainMessage("actions.closeAllTabs"));
        closeOtherTabs = new com.vaadin.event.Action(messages.getMainMessage("actions.closeOtherTabs"));
        closeCurrentTab = new com.vaadin.event.Action(messages.getMainMessage("actions.closeCurrentTab"));
        showInfo = new com.vaadin.event.Action(messages.getMainMessage("actions.showInfo"));
        analyzeLayout = new com.vaadin.event.Action(messages.getMainMessage("actions.analyzeLayout"));
        saveSettings = new com.vaadin.event.Action(messages.getMainMessage("actions.saveSettings"));
        restoreToDefaults = new com.vaadin.event.Action(messages.getMainMessage("actions.restoreToDefaults"));
        initialized = true;
    }
    List<Action> actions = new ArrayList<>(5);
    actions.add(closeCurrentTab);
    actions.add(closeOtherTabs);
    actions.add(closeAllTabs);
    if (target != null) {
        Configuration configuration = AppBeans.get(Configuration.NAME);
        ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
        if (clientConfig.getManualScreenSettingsSaving()) {
            actions.add(saveSettings);
            actions.add(restoreToDefaults);
        }
        UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
        UserSession userSession = sessionSource.getUserSession();
        if (userSession.isSpecificPermitted(ShowInfoAction.ACTION_PERMISSION) && findEditor((Layout) target) != null) {
            actions.add(showInfo);
        }
        if (clientConfig.getLayoutAnalyzerEnabled()) {
            actions.add(analyzeLayout);
        }
    }
    return actions.toArray(new com.vaadin.event.Action[actions.size()]);
}
Also used : Action(com.vaadin.event.Action) Action(com.vaadin.event.Action) ShowInfoAction(com.haulmont.cuba.gui.components.ShowInfoAction) UserSession(com.haulmont.cuba.security.global.UserSession) ArrayList(java.util.ArrayList) ClientConfig(com.haulmont.cuba.client.ClientConfig)

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