Search in sources :

Example 56 with UserSession

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

the class ParentClassConstraintTest method testConstraintsOnParentClass.

@Test
public void testConstraintsOnParentClass() throws LoginException {
    DataManager dataManager = AppBeans.get(DataManager.NAME);
    LoadContext<SearchFolder> loadContext = new LoadContext<>(SearchFolder.class).setView(View.LOCAL);
    loadContext.setQueryString("select f from sec$SearchFolder f");
    List resultList = dataManager.loadList(loadContext);
    assertEquals(2, resultList.size());
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    UserSession userSession = lw.login("constraintUser1", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
    assertNotNull(userSession);
    UserSessionSource uss = AppBeans.get(UserSessionSource.class);
    UserSession savedUserSession = uss.getUserSession();
    ((TestUserSessionSource) uss).setUserSession(userSession);
    try {
        dataManager = AppBeans.get(DataManager.NAME);
        dataManager = dataManager.secure();
        loadContext = new LoadContext<>(SearchFolder.class).setView(View.LOCAL);
        loadContext.setQueryString("select f from sec$SearchFolder f");
        resultList = dataManager.loadList(loadContext);
        assertEquals(1, resultList.size());
    } finally {
        ((TestUserSessionSource) uss).setUserSession(savedUserSession);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) ArrayList(java.util.ArrayList) List(java.util.List)

Example 57 with UserSession

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

the class ExecutionsImpl method cancelExecution.

public void cancelExecution(UUID userSessionId, String group, String key) {
    UserSession userSession = userSessionsAPI.getAndRefresh(userSessionId, false);
    if (userSession == null) {
        log.warn("User session {} not found, execution context: group={}, key={}", userSessionId, group, key);
        throw new IllegalStateException(String.format("User session {%s} not found", userSessionId));
    }
    log.debug("Try to cancel resources for execution context: group={}, key={} and user session {}", group, key, userSessionId);
    cancelLocally(userSession, group, key);
    if (clusterManager.isStarted()) {
        log.debug("Send cancel message in cluster for execution context: group={}, key={}", group, key);
        clusterManager.send(new CancelExecutionMessage(userSessionId, group, key));
    }
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession)

Example 58 with UserSession

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

the class QueryResultsManager method deleteForInactiveSessions.

@Override
public void deleteForInactiveSessions() {
    if (!AppContext.isStarted() || !clusterManager.isMaster() || !configuration.getConfig(GlobalConfig.class).getAllowQueryFromSelected())
        return;
    log.debug("Delete query results for inactive user sessions");
    StringBuilder sb = new StringBuilder("delete from SYS_QUERY_RESULT");
    Collection<UserSession> userSessionEntities = userSessions.getUserSessionsStream().collect(Collectors.toList());
    DbTypeConverter converter = persistence.getDbTypeConverter();
    if (!userSessionEntities.isEmpty()) {
        sb.append(" where SESSION_ID not in (");
        for (Iterator<UserSession> it = userSessionEntities.iterator(); it.hasNext(); ) {
            UserSession userSession = it.next();
            UUID userSessionId = userSession.getId();
            String userSessionIdStr = converter.getSqlObject(userSessionId).toString();
            sb.append("'").append(userSessionIdStr).append("'");
            if (it.hasNext())
                sb.append(",");
        }
        sb.append(")");
    }
    QueryRunner runner = new QueryRunner(persistence.getDataSource());
    try {
        runner.update(sb.toString());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) UserSession(com.haulmont.cuba.security.global.UserSession) DbTypeConverter(com.haulmont.cuba.core.sys.persistence.DbTypeConverter) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 59 with UserSession

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

the class AppLoginWindow method setRememberMeCookies.

protected void setRememberMeCookies() {
    if (connection.isAuthenticated()) {
        if (webConfig.getRememberMeEnabled()) {
            if (Boolean.TRUE.equals(rememberMeCheckBox.getValue())) {
                if (!loginByRememberMe) {
                    app.addCookie(COOKIE_REMEMBER_ME, Boolean.TRUE.toString());
                    String login = loginField.getValue();
                    String encodedLogin = URLEncodeUtils.encodeUtf8(login);
                    app.addCookie(COOKIE_LOGIN, StringEscapeUtils.escapeJava(encodedLogin));
                    UserSession session = connection.getSession();
                    if (session == null) {
                        throw new IllegalStateException("Unable to get session after login");
                    }
                    User user = session.getUser();
                    String rememberMeToken = userManagementService.generateRememberMeToken(user.getId());
                    app.addCookie(COOKIE_PASSWORD, rememberMeToken);
                }
            } else {
                app.removeCookie(COOKIE_REMEMBER_ME);
                app.removeCookie(COOKIE_LOGIN);
                app.removeCookie(COOKIE_PASSWORD);
            }
        }
    }
}
Also used : User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 60 with UserSession

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

the class ScreenHistoryDatasource method loadData.

@Override
protected void loadData(Map<String, Object> params) {
    UserSession userSession = AppBeans.get(UserSessionSource.class).getUserSession();
    User user;
    if (userSession.getSubstitutedUser() != null) {
        user = userSession.getSubstitutedUser();
    } else {
        user = userSession.getUser();
    }
    Map<String, Object> modifiedParams = new HashMap<>();
    modifiedParams.putAll(params);
    modifiedParams.put("userId", user);
    super.loadData(modifiedParams);
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) User(com.haulmont.cuba.security.entity.User) HashMap(java.util.HashMap) 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