Search in sources :

Example 1 with UserSubstitution

use of com.haulmont.cuba.security.entity.UserSubstitution in project cuba by cuba-platform.

the class UserSubstitutionEditor method postValidate.

@Override
protected void postValidate(ValidationErrors errors) {
    super.postValidate(errors);
    UserSubstitution substitution = getItem();
    if (substitution.getStartDate() != null && substitution.getEndDate() != null) {
        if (substitution.getStartDate().getTime() > substitution.getEndDate().getTime()) {
            errors.add(startDateField, getMessage("dateOrderError"));
        }
    }
}
Also used : UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution)

Example 2 with UserSubstitution

use of com.haulmont.cuba.security.entity.UserSubstitution in project cuba by cuba-platform.

the class LoadSubstitutionsTest method testQuerySubstitutions.

@Test
public void testQuerySubstitutions() throws Exception {
    ViewRepository viewRepository = AppBeans.get(ViewRepository.NAME);
    View userView = new View(new View.ViewParams().src(viewRepository.getView(User.class, View.LOCAL)));
    View substitutedUserView = new View(User.class);
    substitutedUserView.addProperty("login");
    View substitutionsView = new View(UserSubstitution.class);
    substitutionsView.addProperty("substitutedUser", substitutedUserView);
    substitutionsView.addProperty("startDate");
    userView.addProperty("substitutions", substitutionsView);
    User loadedUser;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        loadedUser = em.find(User.class, user.getId(), userView);
        tx.commit();
    }
    assertNotNull(loadedUser);
    assertNotNull(loadedUser.getSubstitutions());
    Assert.assertEquals(1, loadedUser.getSubstitutions().size());
    UserSubstitution loadedSubstitution = loadedUser.getSubstitutions().iterator().next();
    assertEquals(user, loadedSubstitution.getUser());
    assertEquals(substitutedUser, loadedSubstitution.getSubstitutedUser());
}
Also used : ViewRepository(com.haulmont.cuba.core.global.ViewRepository) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) View(com.haulmont.cuba.core.global.View)

Example 3 with UserSubstitution

use of com.haulmont.cuba.security.entity.UserSubstitution in project cuba by cuba-platform.

the class LoadSubstitutionsTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"));
        user = new User();
        user.setLogin("user-" + user.getId());
        user.setGroup(group);
        em.persist(user);
        substitutedUser = new User();
        substitutedUser.setLogin("substitutedUser");
        substitutedUser.setGroup(group);
        em.persist(substitutedUser);
        userSubstitution = new UserSubstitution();
        userSubstitution.setUser(user);
        userSubstitution.setSubstitutedUser(substitutedUser);
        user.setSubstitutions(new ArrayList<>(ImmutableList.of(userSubstitution)));
        em.persist(userSubstitution);
        tx.commit();
    } finally {
        tx.end();
    }
    cont.setupLogging("com.haulmont.cuba.core.sys.FetchGroupManager", Level.TRACE);
}
Also used : Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution)

Example 4 with UserSubstitution

use of com.haulmont.cuba.security.entity.UserSubstitution 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 5 with UserSubstitution

use of com.haulmont.cuba.security.entity.UserSubstitution 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)

Aggregations

UserSubstitution (com.haulmont.cuba.security.entity.UserSubstitution)7 User (com.haulmont.cuba.security.entity.User)5 EntityManager (com.haulmont.cuba.core.EntityManager)4 Transaction (com.haulmont.cuba.core.Transaction)4 UserSession (com.haulmont.cuba.security.global.UserSession)3 Test (org.junit.Test)2 DataService (com.haulmont.cuba.core.app.DataService)1 View (com.haulmont.cuba.core.global.View)1 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)1 Group (com.haulmont.cuba.security.entity.Group)1 AppUI (com.haulmont.cuba.web.AppUI)1 CubaComboBox (com.haulmont.cuba.web.toolkit.ui.CubaComboBox)1 Label (com.vaadin.ui.Label)1