Search in sources :

Example 16 with Server

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();
    }
}
Also used : Server(com.haulmont.cuba.core.entity.Server) List(java.util.List) Test(org.junit.Test)

Example 17 with Server

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Server(com.haulmont.cuba.core.entity.Server) Before(org.junit.Before)

Example 18 with Server

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) Server(com.haulmont.cuba.core.entity.Server) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) DataService(com.haulmont.cuba.core.app.DataService) Test(org.junit.Test)

Example 19 with Server

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);
    }
}
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 20 with Server

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();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Server(com.haulmont.cuba.core.entity.Server) Before(org.junit.Before)

Aggregations

Server (com.haulmont.cuba.core.entity.Server)33 Test (org.junit.Test)25 UUID (java.util.UUID)10 Entity (com.haulmont.cuba.core.entity.Entity)8 Before (org.junit.Before)4 UserSession (com.haulmont.cuba.security.global.UserSession)3 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)3 Date (java.util.Date)3 EntityManager (com.haulmont.cuba.core.EntityManager)2 Transaction (com.haulmont.cuba.core.Transaction)2 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)2 User (com.haulmont.cuba.security.entity.User)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 Instance (com.haulmont.chile.core.model.Instance)1 DataService (com.haulmont.cuba.core.app.DataService)1 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 EntitySnapshot (com.haulmont.cuba.core.entity.EntitySnapshot)1 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 EntityDiff (com.haulmont.cuba.core.entity.diff.EntityDiff)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1