Search in sources :

Example 41 with Role

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

the class UsersServiceTest method testChangeUsernameWithPermission.

@Test
public void testChangeUsernameWithPermission() {
    // Add read role to change username permission
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(ChangeOwnUsernamePermissionDefinition.PERMISSION);
    Set<Set<Role>> roleSet = def.getPermission().getRoles();
    Set<Set<Role>> newRoles = new HashSet<>();
    newRoles.add(Collections.singleton(readRole));
    for (Set<Role> roles : roleSet) {
        newRoles.add(new HashSet<>(roles));
    }
    systemPermissionService.update(new MangoPermission(newRoles), def);
    // Ensure they can edit self
    setEditSelfPermission(MangoPermission.requireAnyRole(readRole));
    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)

Example 42 with Role

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

the class PermissionServiceTest method createDataSource.

MockDataSourceVO createDataSource(Set<Role> editRoles) {
    MockDataSourceVO dsVo = new MockDataSourceVO();
    dsVo.setName("permissions_test_datasource");
    dsVo.setEditPermission(MangoPermission.requireAnyRole(editRoles));
    return (MockDataSourceVO) dataSourceService.insert(dsVo);
}
Also used : MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)

Example 43 with Role

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

the class PermissionServiceTest method ensureHasAllPermissionsFailHasNeither.

@Test(expected = PermissionException.class)
public void ensureHasAllPermissionsFailHasNeither() {
    Role perm1 = this.randomRole();
    Role perm2 = this.randomRole();
    User testUser = this.createTestUser(Sets.newHashSet(randomRole(), randomRole()));
    permissionService.ensurePermission(testUser, MangoPermission.requireAllRoles(perm1, perm2));
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test)

Example 44 with Role

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

the class PermissionServiceTest method ensureHasSingleRoleFailEmptyRoles.

@Test(expected = PermissionException.class)
public void ensureHasSingleRoleFailEmptyRoles() {
    User testUser = this.createTestUser();
    Role unsavedRole = new Role(Common.NEW_ID, UUID.randomUUID().toString());
    permissionService.ensurePermission(testUser, MangoPermission.requireAnyRole(unsavedRole));
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test)

Example 45 with Role

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

the class RoleServiceTest method adminCanSeeAllRoles.

@Test
public void adminCanSeeAllRoles() {
    RoleVO vo = insertNewVO(readUser);
    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 superadmin role", roleXids.contains(PermissionHolder.SUPERADMIN_ROLE_XID));
    Assert.assertTrue("Superadmin should see all roles", roleXids.contains(vo.getXid()));
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test)

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