Search in sources :

Example 1 with Role

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

the class TestUserSessionSource method getUserSession.

@Override
public synchronized UserSession getUserSession() {
    if (session == null) {
        User user = new User();
        user.setId(UUID.fromString(USER_ID));
        user.setLogin("test_admin");
        user.setName("Test Administrator");
        user.setPassword(DigestUtils.md5Hex("test_admin"));
        session = new UserSession(UUID.randomUUID(), user, Collections.<Role>emptyList(), Locale.ENGLISH, false);
    }
    return session;
}
Also used : Role(com.haulmont.cuba.security.entity.Role) User(com.haulmont.cuba.security.entity.User) UserSession(com.haulmont.cuba.security.global.UserSession)

Example 2 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 3 with Role

use of com.haulmont.cuba.security.entity.Role 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.jupiter.api.Test)

Example 4 with Role

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

the class DataManagerDistinctResultsTest method setUp.

@BeforeEach
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) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with Role

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

the class PermissionsCollectionDatasource method loadData.

@Override
protected void loadData(Map<String, Object> params) {
    Role role = (Role) params.get("role");
    PermissionType permissionType = (PermissionType) params.get("permissionType");
    Boolean permissionsLoaded = (Boolean) params.get("permissionsLoaded");
    if (role != null && permissionType != null && role.isPredefined()) {
        Collection<Permission> permissions;
        if (BooleanUtils.isTrue(permissionsLoaded)) {
            permissions = role.getPermissions().stream().filter(permission -> permissionType.equals(permission.getType())).collect(Collectors.toList());
        } else {
            permissions = rolesService.getPermissions(role.getName(), permissionType);
        }
        data.clear();
        for (Permission entity : permissions) {
            data.put(entity.getId(), entity);
        }
    } else {
        super.loadData(params);
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) PermissionType(com.haulmont.cuba.security.entity.PermissionType) Permission(com.haulmont.cuba.security.entity.Permission)

Aggregations

Role (com.haulmont.cuba.security.entity.Role)34 Test (org.junit.jupiter.api.Test)20 Datasource (com.haulmont.cuba.gui.data.Datasource)12 HasValue (com.haulmont.cuba.gui.components.HasValue)10 RoleType (com.haulmont.cuba.security.entity.RoleType)10 User (com.haulmont.cuba.security.entity.User)10 UserRole (com.haulmont.cuba.security.entity.UserRole)10 Consumer (java.util.function.Consumer)10 Assertions (org.junit.jupiter.api.Assertions)10 Disabled (org.junit.jupiter.api.Disabled)10 OptionsGroup (com.haulmont.cuba.gui.components.OptionsGroup)7 OptionsList (com.haulmont.cuba.gui.components.OptionsList)6 Group (com.haulmont.cuba.security.entity.Group)6 EntityManager (com.haulmont.cuba.core.EntityManager)5 Permission (com.haulmont.cuba.security.entity.Permission)5 TestCase.assertEquals (junit.framework.TestCase.assertEquals)5 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)5 Transaction (com.haulmont.cuba.core.Transaction)4 UUID (java.util.UUID)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3