Search in sources :

Example 86 with UserSession

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

the class PersistenceSecurityImpl method setQueryParam.

@Override
public void setQueryParam(Query query, String paramName) {
    if (paramName.startsWith(CONSTRAINT_PARAM_SESSION_ATTR)) {
        UserSession userSession = userSessionSource.getUserSession();
        String attrName = paramName.substring(CONSTRAINT_PARAM_SESSION_ATTR.length());
        if (CONSTRAINT_PARAM_USER_LOGIN.equals(attrName)) {
            String userLogin = userSession.getSubstitutedUser() != null ? userSession.getSubstitutedUser().getLogin() : userSession.getUser().getLogin();
            query.setParameter(paramName, userLogin);
        } else if (CONSTRAINT_PARAM_USER_ID.equals(attrName)) {
            UUID userId = userSession.getSubstitutedUser() != null ? userSession.getSubstitutedUser().getId() : userSession.getUser().getId();
            query.setParameter(paramName, userId);
        } else if (CONSTRAINT_PARAM_USER_GROUP_ID.equals(attrName)) {
            Object groupId = userSession.getSubstitutedUser() == null ? userSession.getUser().getGroup().getId() : userSession.getSubstitutedUser().getGroup().getId();
            query.setParameter(paramName, groupId);
        } else {
            Serializable value = userSession.getAttribute(attrName);
            query.setParameter(paramName, value);
        }
    }
}
Also used : Serializable(java.io.Serializable) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 87 with UserSession

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

the class ServiceInterceptorTest method testNewThread.

@Test
public void testNewThread() throws Exception {
    ServiceInterceptorTestService service = AppBeans.get(ServiceInterceptorTestService.class);
    UserSessions userSessions = AppBeans.get(UserSessions.class);
    // workaround for test security setup
    Field startedField = AppContext.class.getDeclaredField("started");
    startedField.setAccessible(true);
    startedField.set(null, true);
    AppContext.setSecurityContext(AppContext.NO_USER_CONTEXT);
    UserSession userSession = new UserSession(AppContext.NO_USER_CONTEXT.getSessionId(), new User(), Collections.emptyList(), Locale.ENGLISH, true);
    userSessions.add(userSession);
    try {
        appender.getMessages().clear();
        service.declarativeTransactionNewThread();
        assertEquals(0, appender.getMessages().stream().filter(s -> s.contains("from another service")).count());
        appender.getMessages().clear();
        try {
            service.executeWithExceptionNewThread();
        } catch (Exception e) {
            assertTrue(e instanceof RemoteException && ((RemoteException) e).getFirstCauseException() instanceof TestingService.TestException);
        }
    } finally {
        userSessions.remove(userSession);
        startedField.set(null, false);
    }
}
Also used : Field(java.lang.reflect.Field) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession) TestingService(com.haulmont.cuba.core.app.TestingService) RemoteException(com.haulmont.cuba.core.global.RemoteException) UserSessions(com.haulmont.cuba.security.app.UserSessions) RemoteException(com.haulmont.cuba.core.global.RemoteException) Test(org.junit.Test)

Example 88 with UserSession

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

the class ConstraintTest method test.

@Test
public void test() throws LoginException {
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    UserSession userSession = lw.login(USER_LOGIN, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
    assertNotNull(userSession);
    List<ConstraintData> constraints = userSession.getConstraints("sys$Server");
    assertEquals(2, constraints.size());
    List<ConstraintData> roleConstraints = userSession.getConstraints("sec$UserRole");
    assertEquals(1, roleConstraints.size());
    UserSessionSource uss = AppBeans.get(UserSessionSource.class);
    UserSession savedUserSession = uss.getUserSession();
    ((TestUserSessionSource) uss).setUserSession(userSession);
    try {
        DataManager dm = AppBeans.get(DataManager.NAME);
        LoadContext loadContext = new LoadContext(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
        List<Server> list = dm.loadList(loadContext);
        for (Server server : list) {
            if (server.getId().equals(serverId))
                fail("Constraints have not taken effect for some reason");
        }
        // test constraint that contains session parameter
        loadContext = new LoadContext(UserRole.class).setQuery(new LoadContext.Query("select ur from sec$UserRole ur"));
        List<UserRole> userRoles = dm.loadList(loadContext);
        if (!userRoles.isEmpty()) {
            fail("Constraint with session attribute failed");
        }
    } finally {
        ((TestUserSessionSource) uss).setUserSession(savedUserSession);
    }
}
Also used : ConstraintData(com.haulmont.cuba.security.global.ConstraintData) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) Server(com.haulmont.cuba.core.entity.Server) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) LoginWorker(com.haulmont.cuba.security.app.LoginWorker) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 89 with UserSession

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

the class DataManagerCommitConstraintTest method testUpdateConstraintNotPassed.

@Test
public void testUpdateConstraintNotPassed() throws LoginException {
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    DataManager dataManager = AppBeans.get(DataManager.NAME);
    UserSession userSession = lw.login("constraintuserupdate", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
    assertNotNull(userSession);
    UserSessionSource uss = AppBeans.get(UserSessionSource.class);
    UserSession savedUserSession = uss.getUserSession();
    ((TestUserSessionSource) uss).setUserSession(userSession);
    try {
        dataManager = dataManager.secure();
        User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate1.getId()).setView(View.LOCAL));
        user.setName("newName");
        dataManager.commit(user);
        fail();
    } catch (RowLevelSecurityException e) {
        User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate1.getId()).setView(View.LOCAL));
        assertEquals(user.getName(), "oldName");
    } 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) Test(org.junit.Test)

Example 90 with UserSession

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

the class DataManagerCommitConstraintTest method testUpdateConstraintPassed.

@Test
public void testUpdateConstraintPassed() throws LoginException {
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    DataManager dataManager = AppBeans.get(DataManager.NAME);
    UserSession userSession = lw.login("constraintuserupdate", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
    assertNotNull(userSession);
    UserSessionSource uss = AppBeans.get(UserSessionSource.class);
    UserSession savedUserSession = uss.getUserSession();
    ((TestUserSessionSource) uss).setUserSession(userSession);
    try {
        dataManager = dataManager.secure();
        User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate2.getId()).setView(View.LOCAL));
        user.setName("newName");
        dataManager.commit(user);
        user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate2.getId()).setView(View.LOCAL));
        assertEquals(user.getName(), "newName");
        user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate3.getId()).setView(View.LOCAL));
        user.setName("newName");
        user.setActive(true);
        dataManager.commit(user);
        user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate3.getId()).setView(View.LOCAL));
        assertEquals(user.getName(), "newName");
    } 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) Test(org.junit.Test)

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