Search in sources :

Example 51 with UserSession

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

the class InMemoryConstraintTest method testLoadingLastPage.

@Test
public void testLoadingLastPage() throws LoginException {
    LoginWorker lw = AppBeans.get(LoginWorker.NAME);
    UserSession userSession = lw.login("constraintUser3", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
    assertNotNull(userSession);
    UserSessionSource uss = AppBeans.get(UserSessionSource.class);
    UserSession savedUserSession = uss.getUserSession();
    ((TestUserSessionSource) uss).setUserSession(userSession);
    try {
        DataManager dataManager = AppBeans.get(DataManager.NAME);
        dataManager = dataManager.secure();
        LoadContext loadContext = new LoadContext(User.class).setView(View.LOCAL);
        loadContext.setQuery(new LoadContext.Query("select u from sec$User u where (u.login like 'user%' or u.login like 'constraintUser%') order by u.login desc"));
        loadContext.getQuery().setMaxResults(30);
        loadContext.getQuery().setFirstResult(30);
        List<User> resultList = dataManager.loadList(loadContext);
        assertEquals(8, resultList.size());
        assertEquals("user133", resultList.get(0).getLogin());
        assertEquals("user132", resultList.get(1).getLogin());
        assertEquals("user131", resultList.get(2).getLogin());
    } 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)

Example 52 with UserSession

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

the class LoginTest method testUserSubstitutionSoftDelete.

@Test
public void testUserSubstitutionSoftDelete() throws Exception {
    // Create a substitution
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = new UserSubstitution();
            substitutionId = substitution.getId();
            substitution.setUser(em.getReference(User.class, user1Id));
            substitution.setSubstitutedUser(em.getReference(User.class, user2Id));
            em.persist(substitution);
        }
    });
    // Soft delete it
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = em.getReference(UserSubstitution.class, substitutionId);
            em.remove(substitution);
        }
    });
    // Log in
    UserSession session1 = loginWorker.login("user1", passwordEncryption.getPlainHash("1"), Locale.forLanguageTag("en"));
    userSessionSource.setUserSession(session1);
    // Try to substitute - fail
    User user2 = loadUser(user2Id);
    try {
        loginWorker.substituteUser(user2);
        fail();
    } catch (Exception e) {
    // ok
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) Test(org.junit.Test)

Example 53 with UserSession

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

the class LoginTest method testUserSubstitution.

@Test
public void testUserSubstitution() throws Exception {
    // Log in
    UserSession session1 = loginWorker.login("user1", passwordEncryption.getPlainHash("1"), Locale.forLanguageTag("en"));
    userSessionSource.setUserSession(session1);
    // Substitute a user that is not in our substitutions list - fail
    User user2 = loadUser(user2Id);
    try {
        loginWorker.substituteUser(user2);
        fail();
    } catch (Exception e) {
    // ok
    }
    // Create a substitution
    cont.persistence().createTransaction().execute(new Transaction.Runnable() {

        @Override
        public void run(EntityManager em) {
            UserSubstitution substitution = new UserSubstitution();
            substitutionId = substitution.getId();
            substitution.setUser(em.getReference(User.class, user1Id));
            substitution.setSubstitutedUser(em.getReference(User.class, user2Id));
            em.persist(substitution);
        }
    });
    // Try again - succeed
    UserSession session2 = loginWorker.substituteUser(user2);
    userSessionSource.setUserSession(session2);
    assertEquals(session1.getId(), session2.getId());
    assertEquals(user1Id, session2.getUser().getId());
    assertEquals(user2Id, session2.getSubstitutedUser().getId());
    // Switch back to the logged in user
    User user1 = loadUser(user1Id);
    UserSession session3 = loginWorker.substituteUser(user1);
    assertEquals(session1.getId(), session3.getId());
    assertEquals(user1Id, session3.getUser().getId());
    assertNull(session3.getSubstitutedUser());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSession(com.haulmont.cuba.security.global.UserSession) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) Test(org.junit.Test)

Example 54 with UserSession

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

the class ParentClassConstraintTest method testConstraintsOnParentAndCurrentMetaClass.

@Test
public void testConstraintsOnParentAndCurrentMetaClass() 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("constraintUser2", 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(0, 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 55 with UserSession

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

the class ParentClassConstraintTest method testConstraintsOnMappedSuperClass.

@Test
public void testConstraintsOnMappedSuperClass() 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("constraintUser3", 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(0, 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)

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