Search in sources :

Example 6 with Role

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

the class DataManagerCommit2Test method setUp.

@Before
public void setUp() throws Exception {
    metadata = cont.metadata();
    persistence = cont.persistence();
    dataManager = AppBeans.get(DataManager.class);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        Group group = em.find(Group.class, groupId);
        Role role = em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93"));
        group2 = metadata.create(Group.class);
        group2.setName("Group2-" + group2.getId());
        em.persist(group2);
        User user = metadata.create(User.class);
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("login" + userId);
        user.setPassword("000");
        user.setGroup(group);
        em.persist(user);
        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setRole(role);
        userRole.setUser(user);
        em.persist(userRole);
        tx.commit();
    }
    view = new View(User.class, true).addProperty("login").addProperty("loginLowerCase").addProperty("name").addProperty("password").addProperty("group", new View(Group.class).addProperty("name")).addProperty("userRoles", new View(UserRole.class));
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) Before(org.junit.Before)

Example 7 with Role

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

the class DataManagerCommitTest method setUp.

@Before
public void setUp() throws Exception {
    dataManager = AppBeans.get(DataManager.class);
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = em.find(Group.class, groupId);
        Role role = em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93"));
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("login" + userId);
        user.setPassword("000");
        user.setGroup(group);
        em.persist(user);
        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setRole(role);
        userRole.setUser(user);
        em.persist(userRole);
        tx.commit();
    }
    view = new View(User.class, true).addProperty("login").addProperty("loginLowerCase").addProperty("name").addProperty("password").addProperty("group", new View(Group.class).addProperty("name")).addProperty("userRoles", new View(UserRole.class));
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) Before(org.junit.Before)

Example 8 with Role

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

the class DataManagerDistinctResultsTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = new Group();
        groupId = group.getId();
        group.setName("testGroup");
        em.persist(group);
        Role role1 = new Role();
        role1Id = role1.getId();
        role1.setName("role1");
        em.persist(role1);
        Role role2 = new Role();
        role2Id = role2.getId();
        role2.setName("role2");
        em.persist(role2);
        for (int i = 0; i < QTY; i++) {
            User user = new User();
            user.setName("user" + StringUtils.leftPad(String.valueOf(i), 2, '0'));
            user.setLogin(user.getName());
            user.setGroup(group);
            em.persist(user);
            UserRole userRole1 = new UserRole();
            userRole1.setUser(user);
            userRole1.setRole(role1);
            em.persist(userRole1);
            UserRole userRole2 = new UserRole();
            userRole2.setUser(user);
            userRole2.setRole(role2);
            em.persist(userRole2);
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) Before(org.junit.Before)

Example 9 with Role

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

the class GetReferenceIdTest method testWithFetchGroup.

@Test
public void testWithFetchGroup() throws Exception {
    User user = null;
    // not in a view
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))).setLoadPartialEntities(true));
        q.setParameter(1, this.user.getId());
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            PersistenceTools.RefId refId = cont.persistence().getTools().getReferenceId(user, "group");
            assertFalse(refId.isLoaded());
            try {
                refId.getValue();
                fail();
            } catch (IllegalStateException e) {
            // ok
            }
        }
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
    // in a view
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("group", new View(Group.class).addProperty("name")).addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))).setLoadPartialEntities(true));
        q.setParameter(1, this.user.getId());
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            PersistenceTools.RefId refId = cont.persistence().getTools().getReferenceId(user, "group");
            assertTrue(refId.isLoaded());
            assertEquals(TestSupport.COMPANY_GROUP_ID, refId.getValue());
        }
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 10 with Role

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

the class RelationsTest method createRole.

public UUID createRole() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        Role role = new Role();
        role.setName("RelationTest");
        em.persist(role);
        UserRole userRole = new UserRole();
        userRole.setUser(user);
        userRole.setRole(role);
        em.persist(userRole);
        tx.commit();
        return role.getId();
    } finally {
        tx.end();
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserRole(com.haulmont.cuba.security.entity.UserRole)

Aggregations

Role (com.haulmont.cuba.security.entity.Role)29 Test (org.junit.Test)18 Datasource (com.haulmont.cuba.gui.data.Datasource)12 Component (com.haulmont.cuba.gui.components.Component)10 RoleType (com.haulmont.cuba.security.entity.RoleType)10 Assert (org.junit.Assert)10 Ignore (org.junit.Ignore)10 User (com.haulmont.cuba.security.entity.User)9 UserRole (com.haulmont.cuba.security.entity.UserRole)9 OptionsGroup (com.haulmont.cuba.gui.components.OptionsGroup)7 OptionsList (com.haulmont.cuba.gui.components.OptionsList)6 Group (com.haulmont.cuba.security.entity.Group)6 TestCase.assertEquals (junit.framework.TestCase.assertEquals)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 EntityManager (com.haulmont.cuba.core.EntityManager)4 Transaction (com.haulmont.cuba.core.Transaction)4 Permission (com.haulmont.cuba.security.entity.Permission)4 Before (org.junit.Before)4 UUID (java.util.UUID)3 View (com.haulmont.cuba.core.global.View)2