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"));
}
}
}
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());
}
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);
}
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
}
}
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());
}
Aggregations