Search in sources :

Example 81 with Role

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

the class LazyFieldJsonTest method testLazyPermissionInObject.

@Test
public void testLazyPermissionInObject() {
    RoleService roleService = Common.getBean(RoleService.class);
    PermissionService permissionService = Common.getBean(PermissionService.class);
    Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
    Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
    LazyContainer container = new LazyContainer();
    container.supplyPermission(() -> MangoPermission.builder().minterm(role1, role2).build());
    try (StringWriter stringWriter = new StringWriter()) {
        JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
        JsonValue value = typeWriter.writeObject(container);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        String json = stringWriter.toString();
        JsonTypeReader typeReader = new JsonTypeReader(json);
        JsonValue read = typeReader.read();
        JsonObject root = read.toJsonObject();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
        LazyContainer readContainer = new LazyContainer();
        context.getReader().readInto(readContainer, root);
        assertEquals(container.getPermission(), readContainer.getPermission());
    } catch (IOException | JsonException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) ImportContext(com.infiniteautomation.mango.emport.ImportContext) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) RoleService(com.infiniteautomation.mango.spring.service.RoleService) StringWriter(java.io.StringWriter) JsonReader(com.serotonin.json.JsonReader) JsonTypeReader(com.serotonin.json.type.JsonTypeReader) Test(org.junit.Test)

Example 82 with Role

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

the class SetPointEventHandlerServiceTest method addRoleToCreatePermission.

void addRoleToCreatePermission(Role vo) {
    PermissionDefinition def = ModuleRegistry.getPermissionDefinition(EventHandlerCreatePermission.PERMISSION);
    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) HashSet(java.util.HashSet) Set(java.util.Set) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 83 with Role

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

the class SetPointEventHandlerServiceTest method createDataSource.

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

Example 84 with Role

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

the class MailingListServiceTest method testAddMissingEditRole.

@Test
@ExpectValidationException("editPermission")
public void testAddMissingEditRole() {
    MailingList vo = newVO(readUser);
    Role role = new Role(10000, "new-role");
    Set<Role> editRoles = Collections.singleton(role);
    vo.setEditPermission(MangoPermission.requireAnyRole(editRoles));
    service.insert(vo);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 85 with Role

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

the class MailingListServiceTest method testInvalidPermission.

@Test(expected = PermissionException.class)
public void testInvalidPermission() {
    MailingList vo = newVO(readUser);
    Set<Role> editRoles = Collections.singleton(editRole);
    vo.setEditPermission(MangoPermission.requireAnyRole(editRoles));
    runAs.runAs(readUser, () -> {
        service.insert(vo);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) 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