Search in sources :

Example 61 with Role

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

the class PermissionServiceTest method ensureHasAnyPermissionFailNullEntry.

@Test(expected = IllegalArgumentException.class)
public void ensureHasAnyPermissionFailNullEntry() {
    User testUser = this.createTestUser(roleService.getSuperadminRole());
    permissionService.ensurePermission(testUser, MangoPermission.requireAnyRole((Role) null));
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test)

Example 62 with Role

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

the class PermissionServiceTest method implodeRoles.

@Test
public void implodeRoles() {
    Role perm1 = this.randomRole();
    Role perm2 = this.randomRole();
    Set<Role> permSet = new HashSet<>();
    permSet.add(perm1);
    permSet.add(perm2);
    String joinedPerms = PermissionService.implodeRoles(permSet);
    assertTrue(joinedPerms.contains(perm1.getXid()));
    assertTrue(joinedPerms.contains(perm2.getXid()));
    assertTrue(joinedPerms.contains(","));
    assertFalse(joinedPerms.contains(" "));
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 63 with Role

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

the class MangoTestBase method createRole.

/**
 * Create a role with inherited roles (
 */
protected RoleVO createRole(String xid, String name, Role... inherited) {
    RoleService service = Common.getBean(RoleService.class);
    RoleVO role = new RoleVO(Common.NEW_ID, xid, name, new HashSet<>(Arrays.asList(inherited)));
    return service.insert(role);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService)

Example 64 with Role

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

the class UsersServiceTest method canEditPermissions.

@Test
public void canEditPermissions() {
    Role otherRole = createRole(randomXid(), "Some other role").getRole();
    User otherUser = insertUser();
    User user = insertUser(editRole, readRole, otherRole);
    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)

Example 65 with Role

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

the class UsersServiceTest method testChangeUsernameWithoutPermission.

@Test
@ExpectValidationException("username")
public void testChangeUsernameWithoutPermission() {
    setEditSelfPermission(MangoPermission.requireAnyRole(readRole));
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(ChangeOwnUsernamePermissionDefinition.PERMISSION);
    Set<Set<Role>> roleSet = def.getPermission().getRoles();
    Set<Set<Role>> newRoles = new HashSet<>();
    newRoles.add(Collections.singleton(editRole));
    for (Set<Role> roles : roleSet) {
        if (roles.contains(PermissionHolder.USER_ROLE)) {
            // skip the user role
            continue;
        }
        newRoles.add(roles);
    }
    systemPermissionService.update(new MangoPermission(newRoles), def);
    User vo = newVO(readUser);
    vo.setRoles(Collections.singleton(readRole));
    service.insert(vo);
    User saved = service.get(vo.getId());
    runAs.runAs(saved, () -> {
        saved.setUsername(randomXid());
        service.update(saved.getId(), saved);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ChangeOwnUsernamePermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.ChangeOwnUsernamePermissionDefinition) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) HashSet(java.util.HashSet) Set(java.util.Set) User(com.serotonin.m2m2.vo.User) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet) 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