Search in sources :

Example 6 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method testGetAll.

@Override
@Test
public void testGetAll() {
    List<RoleVO> all = service.dao.getAll();
    for (RoleVO vo : all) {
        if (!StringUtils.equals(PermissionHolder.SUPERADMIN_ROLE_XID, vo.getXid()) && !StringUtils.equals(PermissionHolder.USER_ROLE_XID, vo.getXid()) && !StringUtils.equals(PermissionHolder.ANONYMOUS_ROLE_XID, vo.getXid())) {
            service.delete(vo.getId());
        }
    }
    List<RoleVO> vos = new ArrayList<>();
    vos.add(service.get(PermissionHolder.SUPERADMIN_ROLE_XID));
    vos.add(service.get(PermissionHolder.USER_ROLE_XID));
    vos.add(service.get(PermissionHolder.ANONYMOUS_ROLE_XID));
    for (int i = 0; i < 5; i++) {
        vos.add(insertNewVO(readUser));
    }
    all = service.dao.getAll();
    for (RoleVO vo : all) {
        RoleVO expected = null;
        for (RoleVO e : vos) {
            if (e.getId() == vo.getId()) {
                expected = e;
            }
        }
        assertNotNull("Didn't find expected VO", expected);
        assertVoEqual(expected, vo);
    }
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method userCanOnlySeeOwnRoles.

@Test
public void userCanOnlySeeOwnRoles() {
    RoleVO inheritedRole = insertNewVO(readUser);
    RoleVO directlyAssignedRole = newVO(readUser);
    directlyAssignedRole.setInherited(Collections.singleton(inheritedRole.getRole()));
    service.insert(directlyAssignedRole);
    RoleVO roleUserDoesNotHave = insertNewVO(readUser);
    User testUser = createUser("test-user@example.com", "test-user@example.com", "test-user@example.com", "test-user@example.com", directlyAssignedRole.getRole());
    runAs.runAs(testUser, () -> {
        Set<String> roleXids = service.list().stream().map(AbstractVO::getXid).collect(Collectors.toSet());
        Assert.assertTrue("Should see anonymous role", roleXids.contains(PermissionHolder.ANONYMOUS_ROLE_XID));
        Assert.assertTrue("Should see user role", roleXids.contains(PermissionHolder.USER_ROLE_XID));
        Assert.assertTrue("Should see directly assigned role", roleXids.contains(directlyAssignedRole.getXid()));
        Assert.assertTrue("Should see inherited role", roleXids.contains(inheritedRole.getXid()));
        Assert.assertFalse("Should not see role that user does not have", roleXids.contains(roleUserDoesNotHave.getXid()));
    });
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test)

Example 8 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleServiceTest method cannotModifySuperadminRole.

@Test
@ExpectValidationException("xid")
public void cannotModifySuperadminRole() {
    RoleVO vo = service.get(PermissionHolder.SUPERADMIN_ROLE_XID);
    RoleVO updated = new RoleVO(Common.NEW_ID, vo.getXid(), "Superadmin default changed");
    service.update(vo.getXid(), updated);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 9 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class PermissionServiceTest method randomRole.

Role randomRole() {
    RoleVO vo = new RoleVO(Common.NEW_ID, UUID.randomUUID().toString(), "Random permission");
    roleService.insert(vo);
    return new Role(vo);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO)

Example 10 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.

the class RoleDao method saveRelationalData.

@Override
public void saveRelationalData(RoleVO existing, RoleVO vo) {
    if (existing != null) {
        // Drop the mappings
        this.create.deleteFrom(RoleInheritance.ROLE_INHERITANCE).where(RoleInheritance.ROLE_INHERITANCE.roleId.eq(vo.getId())).execute();
    }
    if (vo.getInherited() != null && vo.getInherited().size() > 0) {
        List<Query> inserts = new ArrayList<>();
        for (Role role : vo.getInherited()) {
            inserts.add(DSL.insertInto(RoleInheritance.ROLE_INHERITANCE).columns(RoleInheritance.ROLE_INHERITANCE.roleId, RoleInheritance.ROLE_INHERITANCE.inheritedRoleId).values(vo.getId(), role.getId()));
        }
        create.batch(inserts).execute();
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) Query(org.jooq.Query) ArrayList(java.util.ArrayList)

Aggregations

RoleVO (com.serotonin.m2m2.vo.role.RoleVO)58 Test (org.junit.Test)34 Role (com.serotonin.m2m2.vo.role.Role)33 HashSet (java.util.HashSet)17 RoleService (com.infiniteautomation.mango.spring.service.RoleService)14 User (com.serotonin.m2m2.vo.User)11 ArrayList (java.util.ArrayList)11 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)8 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)7 JsonValue (com.serotonin.json.type.JsonValue)7 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)7 Set (java.util.Set)7 Roles (com.infiniteautomation.mango.db.tables.Roles)6 JsonException (com.serotonin.json.JsonException)6 DSLContext (org.jooq.DSLContext)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)5 JsonObject (com.serotonin.json.type.JsonObject)5 ImportContext (com.infiniteautomation.mango.emport.ImportContext)4 JsonReader (com.serotonin.json.JsonReader)4