Search in sources :

Example 1 with Connection

use of com.haulmont.cuba.web.Connection 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 2 with Connection

use of com.haulmont.cuba.web.Connection in project cuba by cuba-platform.

the class IdpLoginLifecycleManager method onAppStarted.

@Order(Events.HIGHEST_PLATFORM_PRECEDENCE + 10)
@EventListener
protected void onAppStarted(AppStartedEvent event) throws LoginException {
    Connection connection = event.getApp().getConnection();
    // can be already authenticated by another event listener
    if (webIdpConfig.getIdpEnabled() && !connection.isAuthenticated()) {
        VaadinRequest currentRequest = VaadinService.getCurrentRequest();
        if (currentRequest != null) {
            Principal principal = currentRequest.getUserPrincipal();
            if (principal instanceof IdpSessionPrincipal) {
                IdpSession idpSession = ((IdpSessionPrincipal) principal).getIdpSession();
                Locale locale = event.getApp().getLocale();
                ExternalUserCredentials credentials = new ExternalUserCredentials(principal.getName(), locale);
                credentials.setSessionAttributes(ImmutableMap.of(IdpService.IDP_USER_SESSION_ATTRIBUTE, idpSession.getId()));
                connection.login(credentials);
            }
        }
    }
}
Also used : Locale(java.util.Locale) ExternalUserCredentials(com.haulmont.cuba.web.security.ExternalUserCredentials) Connection(com.haulmont.cuba.web.Connection) IdpSession(com.haulmont.cuba.security.global.IdpSession) VaadinRequest(com.vaadin.server.VaadinRequest) Principal(java.security.Principal) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 3 with Connection

use of com.haulmont.cuba.web.Connection in project cuba by cuba-platform.

the class ChangePasswordAfterLoginListener method onApplicationEvent.

@Override
public void onApplicationEvent(AppLoggedInEvent event) {
    App app = event.getApp();
    Connection connection = app.getConnection();
    if (connection.isAuthenticated() && !isLoggedInWithExternalAuth(connection.getSessionNN())) {
        User user = connection.getSessionNN().getUser();
        // Change password on logon
        if (Boolean.TRUE.equals(user.getChangePasswordAtNextLogon())) {
            WebWindowManager wm = app.getWindowManager();
            for (Window window : wm.getOpenWindows()) {
                window.setEnabled(false);
            }
            WindowInfo changePasswordDialog = windowConfig.getWindowInfo("sec$User.changePassword");
            Window changePasswordWindow = wm.openWindow(changePasswordDialog, WindowManager.OpenType.DIALOG.closeable(false), ParamsMap.of("cancelEnabled", Boolean.FALSE));
            changePasswordWindow.addCloseListener(actionId -> {
                for (Window window : wm.getOpenWindows()) {
                    window.setEnabled(true);
                }
            });
        }
    }
}
Also used : App(com.haulmont.cuba.web.App) Window(com.haulmont.cuba.gui.components.Window) User(com.haulmont.cuba.security.entity.User) WebWindowManager(com.haulmont.cuba.web.WebWindowManager) Connection(com.haulmont.cuba.web.Connection) WindowInfo(com.haulmont.cuba.gui.config.WindowInfo)

Example 4 with Connection

use of com.haulmont.cuba.web.Connection in project cuba by cuba-platform.

the class LegacyLoginEventsForwarder method redirectToExternalAuthentication.

@Order(Events.HIGHEST_PLATFORM_PRECEDENCE + 10)
@EventListener
protected void redirectToExternalAuthentication(AppLoggedOutEvent event) {
    Connection connection = event.getApp().getConnection();
    if (webAuthConfig.getExternalAuthentication() && event.getLoggedOutSession() != null && isLoggedInWithExternalAuth(event.getLoggedOutSession())) {
        String loggedOutUrl = connection.logoutExternalAuthentication();
        event.setRedirectUrl(loggedOutUrl);
    }
}
Also used : Connection(com.haulmont.cuba.web.Connection) Order(org.springframework.core.annotation.Order) EventListener(org.springframework.context.event.EventListener)

Example 5 with Connection

use of com.haulmont.cuba.web.Connection in project cuba by cuba-platform.

the class WebUserSessionSource method checkCurrentUserSession.

@Override
public boolean checkCurrentUserSession() {
    if (App.isBound()) {
        App app = App.getInstance();
        Connection connection = app.getConnection();
        return connection.isConnected() && connection.getSession() != null;
    } else {
        SecurityContext securityContext = AppContext.getSecurityContext();
        if (securityContext == null) {
            return false;
        }
        if (securityContext.getSession() != null) {
            return true;
        } else {
            try {
                getUserSessionFromMiddleware(securityContext.getSessionId());
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
}
Also used : App(com.haulmont.cuba.web.App) Connection(com.haulmont.cuba.web.Connection) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext)

Aggregations

Connection (com.haulmont.cuba.web.Connection)8 App (com.haulmont.cuba.web.App)3 EventListener (org.springframework.context.event.EventListener)3 Order (org.springframework.core.annotation.Order)3 UserSession (com.haulmont.cuba.security.global.UserSession)2 WebWindowManager (com.haulmont.cuba.web.WebWindowManager)2 Locale (java.util.Locale)2 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 Window (com.haulmont.cuba.gui.components.Window)1 WindowInfo (com.haulmont.cuba.gui.config.WindowInfo)1 User (com.haulmont.cuba.security.entity.User)1 IdpSession (com.haulmont.cuba.security.global.IdpSession)1 LoginException (com.haulmont.cuba.security.global.LoginException)1 AnonymousUserCredentials (com.haulmont.cuba.web.security.AnonymousUserCredentials)1 ExternalUserCredentials (com.haulmont.cuba.web.security.ExternalUserCredentials)1 VaadinRequest (com.vaadin.server.VaadinRequest)1 Principal (java.security.Principal)1