Search in sources :

Example 46 with RoleVO

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

the class RoleServiceTest method cannotInsertNewUserRole.

@Test
@ExpectValidationException("xid")
public void cannotInsertNewUserRole() {
    RoleVO vo = new RoleVO(Common.NEW_ID, PermissionHolder.USER_ROLE_XID, "user default");
    service.insert(vo);
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 47 with RoleVO

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

the class RoleServiceTest method canGetRoleUserHas.

@Test
public void canGetRoleUserHas() {
    RoleVO roleUserHas = insertNewVO(readUser);
    User testUser = createUser("test-user@example.com", "test-user@example.com", "test-user@example.com", "test-user@example.com", roleUserHas.getRole());
    runAs.runAs(testUser, () -> {
        RoleVO role = service.get(roleUserHas.getXid());
        assertEquals(role.getXid(), roleUserHas.getXid());
        assertEquals(role.getName(), roleUserHas.getName());
        assertEquals(role.getId(), roleUserHas.getId());
    });
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO) User(com.serotonin.m2m2.vo.User) Test(org.junit.Test)

Example 48 with RoleVO

use of com.serotonin.m2m2.vo.role.RoleVO 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 49 with RoleVO

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

the class DataSourceRT method handleRoleEvent.

/**
 * Override to handle any situations where you need to know that a role was modified.
 *  be sure to call super for this method as it handles the edit and read permissions
 *
 * @param event dao event
 */
public void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
    if (event.getType() == DaoEventType.DELETE) {
        Role deletedRole = event.getVo().getRole();
        vo.setEditPermission(vo.getEditPermission().withoutRole(deletedRole));
        vo.setReadPermission(vo.getReadPermission().withoutRole(deletedRole));
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role)

Example 50 with RoleVO

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

the class DataPointService method handleRoleEvent.

@EventListener
protected void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
    if (event.getType() == DaoEventType.DELETE) {
        Role deletedRole = event.getVo().getRole();
        for (DataPointRT rt : getRuntimeManager().getRunningDataPoints()) {
            DataPointVO point = rt.getVO();
            point.setReadPermission(point.getReadPermission().withoutRole(deletedRole));
            point.setEditPermission(point.getEditPermission().withoutRole(deletedRole));
            point.setSetPermission(point.getSetPermission().withoutRole(deletedRole));
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) EventListener(org.springframework.context.event.EventListener)

Aggregations

RoleVO (com.serotonin.m2m2.vo.role.RoleVO)58 Test (org.junit.Test)34 Role (com.serotonin.m2m2.vo.role.Role)33 HashSet (java.util.HashSet)17 RoleService (com.infiniteautomation.mango.spring.service.RoleService)14 User (com.serotonin.m2m2.vo.User)11 ArrayList (java.util.ArrayList)11 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)8 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)8 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)7 JsonValue (com.serotonin.json.type.JsonValue)7 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)7 Set (java.util.Set)7 Roles (com.infiniteautomation.mango.db.tables.Roles)6 JsonException (com.serotonin.json.JsonException)6 DSLContext (org.jooq.DSLContext)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)5 JsonObject (com.serotonin.json.type.JsonObject)5 ImportContext (com.infiniteautomation.mango.emport.ImportContext)4 JsonReader (com.serotonin.json.JsonReader)4