Search in sources :

Example 26 with Role

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

the class PermissionMigration method getOrCreatePermissionNoCache.

default MangoPermission getOrCreatePermissionNoCache(MangoPermission permission) {
    if (permission.getId() != null) {
        return permission;
    }
    return getTransactionTemplate().execute(tx -> {
        Set<Integer> mintermIds = new HashSet<>();
        Set<Set<Role>> minterms = new HashSet<>();
        for (Set<Role> minterm : permission.getRoles()) {
            Set<Role> savedRoles = new HashSet<>();
            for (Role role : minterm) {
                savedRoles.add(getOrCreateRole(role));
            }
            mintermIds.add(getOrCreateMinterm(savedRoles));
        }
        Integer id = getOrCreatePermission(mintermIds);
        MangoPermission saved = new MangoPermission(minterms);
        return saved.withId(id);
    });
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) Set(java.util.Set) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 27 with Role

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

the class LazyFieldJsonTest method testLazyPermissionFromJsonObject.

@Test
public void testLazyPermissionFromJsonObject() {
    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();
    LazyField<MangoPermission> permission = new LazyField<>(() -> 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(permission);
        writer.setPrettyIndent(0);
        writer.setPrettyOutput(true);
        writer.writeObject(value);
        String json = stringWriter.toString();
        JsonTypeReader typeReader = new JsonTypeReader(json);
        JsonValue read = typeReader.read();
        JsonArray root = read.toJsonArray();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
        LazyField<MangoPermission> readPermission = new LazyField<>();
        TypeDefinition lazyType = new TypeDefinition(LazyField.class, MangoPermission.class);
        context.getReader().readInto(lazyType, readPermission, root);
        assertEquals(permission.get(), readPermission.get());
    } catch (IOException | JsonException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : JsonException(com.serotonin.json.JsonException) LazyField(com.infiniteautomation.mango.util.LazyField) JsonValue(com.serotonin.json.type.JsonValue) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter) JsonTypeWriter(com.serotonin.json.type.JsonTypeWriter) TypeDefinition(com.serotonin.json.util.TypeDefinition) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) JsonArray(com.serotonin.json.type.JsonArray) 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) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) Test(org.junit.Test)

Example 28 with Role

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

the class SetPointEventHandlerServiceTest method createDataPoint.

DataPointVO createDataPoint(DataSourceVO dsVo, Set<Role> readRoles, Set<Role> setRoles) {
    DataPointVO point = new DataPointVO();
    point.setDataSourceId(dsVo.getId());
    point.setName("permissions_test_datasource");
    point.setReadPermission(MangoPermission.requireAnyRole(readRoles));
    point.setSetPermission(MangoPermission.requireAnyRole(setRoles));
    point.setPointLocator(new MockPointLocatorVO(DataType.NUMERIC, true));
    point = dataPointService.insert(point);
    return point;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockPointLocatorVO(com.serotonin.m2m2.vo.dataPoint.MockPointLocatorVO)

Example 29 with Role

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

the class MailingListServiceTest method testRemoveRole.

@Test
public void testRemoveRole() {
    MailingList vo = newVO(readUser);
    Set<Role> editRoles = Collections.singleton(editRole);
    vo.setEditPermission(MangoPermission.requireAnyRole(editRoles));
    service.insert(vo);
    roleService.delete(editRole.getXid());
    vo.setEditPermission(MangoPermission.superadminOnly());
    MailingList fromDb = service.get(vo.getId());
    assertVoEqual(vo, fromDb);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) Test(org.junit.Test)

Example 30 with Role

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

the class MailingListServiceTest method testAddOverprivledgedEditRole.

@Test(expected = PermissionException.class)
public void testAddOverprivledgedEditRole() {
    MailingList vo = newVO(readUser);
    Set<Role> editRoles = Collections.singleton(editRole);
    vo.setEditPermission(MangoPermission.requireAnyRole(editRoles));
    service.insert(vo);
    MailingList fromDb = service.get(vo.getId());
    assertVoEqual(vo, fromDb);
    runAs.runAs(readUser, () -> {
        service.update(vo.getXid(), 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