use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method testSuspend.
@Test
public void testSuspend() {
Transaction tx = cont.persistence().getTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Server server = new Server();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
Transaction tx1 = cont.persistence().createTransaction();
try {
EntityManager em1 = cont.persistence().getEntityManager();
assertTrue(em != em1);
Query query = em1.createQuery("select s from sys$Server s");
List list = query.getResultList();
assertNotNull(list);
tx1.commit();
} finally {
tx1.end();
}
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class ConstraintTest method setUp.
@Before
public void setUp() throws Exception {
passwordEncryption = AppBeans.get(PasswordEncryption.class);
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Server server = new Server();
server.setName("someServer");
server.setRunning(false);
serverId = server.getId();
em.persist(server);
Group parentGroup = new Group();
parentGroupId = parentGroup.getId();
parentGroup.setName("testParentGroup");
em.persist(parentGroup);
tx.commitRetaining();
em = cont.persistence().getEntityManager();
Constraint parentConstraint = new Constraint();
parentConstraintId = parentConstraint.getId();
parentConstraint.setEntityName("sys$Server");
parentConstraint.setWhereClause("{E}.running = true");
parentConstraint.setGroup(parentGroup);
em.persist(parentConstraint);
Group group = new Group();
groupId = group.getId();
group.setName("testGroup");
group.setParent(parentGroup);
em.persist(group);
Constraint serverConstraint = new Constraint();
serverConstraintId = serverConstraint.getId();
serverConstraint.setEntityName("sys$Server");
serverConstraint.setWhereClause("{E}.name = 'localhost'");
serverConstraint.setGroup(group);
em.persist(serverConstraint);
Constraint userRoleConstraint = new Constraint();
userRoleConstraintId = userRoleConstraint.getId();
userRoleConstraint.setEntityName("sec$UserRole");
userRoleConstraint.setWhereClause("{E}.user.id = :session$userId");
userRoleConstraint.setGroup(group);
em.persist(userRoleConstraint);
User user = new User();
userId = user.getId();
user.setLogin(USER_LOGIN);
String pwd = passwordEncryption.getPasswordHash(userId, USER_PASSW);
user.setPassword(pwd);
user.setGroup(group);
em.persist(user);
Group otherGroup = new Group();
otherGroupId = otherGroup.getId();
otherGroup.setName("otherGroup");
otherGroup.setParent(parentGroup);
em.persist(otherGroup);
User user2 = new User();
user2.setGroup(otherGroup);
user2Id = user2.getId();
user2.setLogin("someOtherUser");
em.persist(user2);
UserRole userRole = new UserRole();
userRoleId = userRole.getId();
userRole.setUser(user2);
Role role = new Role();
role.setName("TestRole");
roleId = role.getId();
em.persist(role);
userRole.setRole(role);
em.persist(userRole);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class DataManagerSecurityTest method test.
@Test
public void test() throws Exception {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
UserSession userSession = lw.login(USER_NAME, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
DataManager dm = AppBeans.get(DataManager.NAME);
LoadContext<Server> loadContext = LoadContext.create(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
List<Server> list = dm.loadList(loadContext);
assertFalse("Permission took effect when calling DataManager inside middleware", list.isEmpty());
DataService ds = AppBeans.get(DataService.NAME);
loadContext = LoadContext.create(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
list = ds.loadList(loadContext);
assertTrue("Permission did not take effect when calling DataService", list.isEmpty());
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.core.entity.Server 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);
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class DataManagerSecurityTest method setUp.
@Before
public void setUp() throws Exception {
passwordEncryption = AppBeans.get(PasswordEncryption.class);
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
server = new Server();
server.setName("someServer");
server.setRunning(false);
em.persist(server);
role = new Role();
role.setName("testRole1");
em.persist(role);
permission = new Permission();
permission.setRole(role);
permission.setType(PermissionType.ENTITY_OP);
permission.setTarget(PERM_TARGET);
permission.setValue(0);
em.persist(permission);
group = new Group();
group.setName("testGroup");
em.persist(group);
user = new User();
user.setName(USER_NAME);
user.setLogin(USER_NAME);
String pwd = passwordEncryption.getPasswordHash(user.getId(), USER_PASSW);
user.setPassword(pwd);
user.setGroup(group);
em.persist(user);
userRole = new UserRole();
userRole.setUser(user);
userRole.setRole(role);
em.persist(userRole);
tx.commit();
}
}
Aggregations