Search in sources :

Example 6 with UserRole

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

the class EntityManagerTest method testMergeWithOneToManyCollection.

@Test
public void testMergeWithOneToManyCollection() throws Exception {
    User user = null;
    UserRole userRole1 = null, userRole2 = null;
    Role role1 = null, role2 = null;
    try {
        // creates user with one role
        Transaction tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            user = new User();
            user.setName("testMerge1");
            user.setLogin("testMerge1");
            user.setPassword("testMerge1");
            user.setGroup(em.getReference(Group.class, groupId));
            em.persist(user);
            role1 = new Role();
            role1.setName("role1");
            em.persist(role1);
            role2 = new Role();
            role2.setName("role2");
            em.persist(role2);
            userRole1 = new UserRole();
            userRole1.setRole(role1);
            userRole1.setUser(user);
            em.persist(userRole1);
            tx.commit();
        } finally {
            tx.end();
        }
        // creates new user role and merge it
        tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            userRole2 = new UserRole();
            userRole2.setRole(role2);
            userRole2.setUser(user);
            em.merge(userRole2);
            tx.commit();
        } finally {
            tx.end();
        }
        // check user versions, version shouldn't be changed
        tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            assertEquals((Integer) 1, em.find(User.class, user.getId()).getVersion());
            tx.commit();
        } finally {
            tx.end();
        }
    } finally {
        cont.deleteRecord(userRole1);
        cont.deleteRecord(userRole2);
        cont.deleteRecord(user);
        cont.deleteRecord(role1);
        cont.deleteRecord(role2);
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) 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) UserRole(com.haulmont.cuba.security.entity.UserRole) Test(org.junit.Test)

Example 7 with UserRole

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

the class SerializationTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setPosition("manager");
        user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        em.persist(user);
        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setUser(user);
        userRole.setRole(em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93")));
        em.persist(userRole);
        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) 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)

Example 8 with UserRole

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

the class UserEntityListener method addDefaultRoles.

protected void addDefaultRoles(User user, EntityManager entityManager) {
    List<Role> defaultRoles = entityManager.createQuery("select r from sec$Role r where r.defaultRole = true", Role.class).getResultList();
    if (user.getUserRoles() == null)
        user.setUserRoles(new ArrayList<>());
    for (Role defaultRole : defaultRoles) {
        if (user.getUserRoles().stream().noneMatch(userRole -> userRole.getRole().equals(defaultRole))) {
            UserRole userRole = metadata.create(UserRole.class);
            userRole.setUser(user);
            userRole.setRole(defaultRole);
            entityManager.persist(userRole);
            user.getUserRoles().add(userRole);
        }
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) UserRole(com.haulmont.cuba.security.entity.UserRole) ArrayList(java.util.ArrayList)

Example 9 with UserRole

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

the class DynamicAttributesTest method testDynamicAttributesWithNestedDynamicAttributes.

@Test
public void testDynamicAttributesWithNestedDynamicAttributes() {
    View view = new View(User.class, "testView").addProperty("login").addProperty("userRoles", new View(UserRole.class, "testView"));
    User loadedUser = dataManager.load(LoadContext.create(User.class).setId(user.getId()).setLoadDynamicAttributes(true).setView(view));
    assertEquals("userName", loadedUser.getValue("+userAttribute"));
    assertEquals("userRole", loadedUser.getUserRoles().get(0).getValue("+userRoleAttribute"));
}
Also used : User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole)

Example 10 with UserRole

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

the class UserRoleTest method test.

@Test
public void test() {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Role role = new Role();
        role.setName("testRole1");
        em.persist(role);
        Group group = new Group();
        group.setName("testGroup1");
        em.persist(group);
        User user = new User();
        UUID userId = user.getId();
        user.setLogin("testUser1");
        user.setName("Test User 1");
        user.setGroup(group);
        em.persist(user);
        UserRole userRole = new UserRole();
        userRole.setUser(user);
        userRole.setRole(role);
        em.persist(userRole);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId);
        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            Role r = ur.getRole();
            assertEquals(role.getName(), r.getName());
        }
    } 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) 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) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

UserRole (com.haulmont.cuba.security.entity.UserRole)10 Role (com.haulmont.cuba.security.entity.Role)9 User (com.haulmont.cuba.security.entity.User)9 Group (com.haulmont.cuba.security.entity.Group)7 EntityManager (com.haulmont.cuba.core.EntityManager)4 Transaction (com.haulmont.cuba.core.Transaction)4 Before (org.junit.Before)4 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 EntityFetchGroup (org.eclipse.persistence.internal.queries.EntityFetchGroup)1