Search in sources :

Example 31 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

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 32 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class AbstractVOServiceWithPermissionsTest method addRoleToCreatePermission.

void addRoleToCreatePermission(Role vo) {
    String permissionType = getCreatePermissionType();
    if (permissionType != null) {
        PermissionDefinition def = ModuleRegistry.getPermissionDefinition(getCreatePermissionType());
        Set<Set<Role>> roleSet = def.getPermission().getRoles();
        Set<Set<Role>> newRoles = new HashSet<>();
        newRoles.add(Collections.singleton(vo));
        for (Set<Role> roles : roleSet) {
            newRoles.add(new HashSet<>(roles));
        }
        Common.getBean(SystemPermissionService.class).update(new MangoPermission(newRoles), def);
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) Set(java.util.Set) HashSet(java.util.HashSet) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 33 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class AbstractVOServiceWithPermissionsTest method testAddEditRoleUserDoesNotHave.

/**
 * There will be 2 validation messages about this, must retain permission AND cannot add/remove a role you do not have
 */
@Test
public void testAddEditRoleUserDoesNotHave() {
    validation.expectValidationException(getEditPermissionContextKey());
    VO vo = newVO(editUser);
    setReadPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    setEditPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    service.insert(vo);
    runAs.runAs(readUser, () -> {
        VO fromDb = service.get(vo.getId());
        assertVoEqual(vo, fromDb);
        setEditPermission(MangoPermission.superadminOnly(), fromDb);
        service.update(fromDb.getId(), fromDb);
    });
}
Also used : AbstractVO(com.serotonin.m2m2.vo.AbstractVO) Test(org.junit.Test)

Example 34 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class AbstractRoleBasedTest method setupRoles.

protected void setupRoles() {
    roleService = Common.getBean(RoleService.class);
    // Add some roles
    RoleVO temp = new RoleVO(Common.NEW_ID, "read-role", "Role to allow reading.");
    roleService.insert(temp);
    readRole = new Role(temp);
    temp = new RoleVO(Common.NEW_ID, "edit-role", "Role to allow editing.");
    roleService.insert(temp);
    editRole = new Role(temp);
    temp = new RoleVO(Common.NEW_ID, "set-role", "Role to allow setting.");
    roleService.insert(temp);
    setRole = new Role(temp);
    temp = new RoleVO(Common.NEW_ID, "delete-role", "Role to allow deleting.");
    roleService.insert(temp);
    deleteRole = new Role(temp);
    readUser = createUser("readUser", "readUser", "password", "readUser@example.com", readRole);
    editUser = createUser("editUser", "editUser", "password", "editUser@example.com", editRole);
    setUser = createUser("setUser", "setUser", "password", "setUser@example.com", setRole);
    deleteUser = createUser("deleteUser", "deleteUser", "password", "deleteUser@example.com", deleteRole);
    allUser = createUser("allUser", "allUser", "password", "allUser@example.com", readRole, editRole, setRole, deleteRole);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService)

Example 35 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class UsersServiceTest method mustRetainAccess.

@Test
@ExpectValidationException({ "readPermission", "editPermission" })
public void mustRetainAccess() {
    Role otherRole = createRole(randomXid(), "Some other role").getRole();
    User otherUser = insertUser();
    User user = insertUser(editRole, readRole);
    runAs.runAs(user, () -> {
        otherUser.setReadPermission(MangoPermission.requireAnyRole(otherRole));
        otherUser.setEditPermission(MangoPermission.requireAnyRole(otherRole));
        service.update(otherUser.getId(), otherUser);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Aggregations

Role (com.serotonin.m2m2.vo.role.Role)102 Test (org.junit.Test)59 HashSet (java.util.HashSet)40 Set (java.util.Set)38 User (com.serotonin.m2m2.vo.User)33 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)23 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)22 Collectors (java.util.stream.Collectors)18 Common (com.serotonin.m2m2.Common)17 MangoTestBase (com.serotonin.m2m2.MangoTestBase)15 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 List (java.util.List)15 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)12 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)12 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)11 DSLContext (org.jooq.DSLContext)11