Search in sources :

Example 36 with RoleVO

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

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

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

the class PermissionService method loadRoleInheritance.

private RoleInheritance loadRoleInheritance(String xid) {
    RoleInheritance i = new RoleInheritance();
    RoleVO roleVo = roleDao.getByXid(xid);
    if (roleVo == null) {
        return null;
    }
    i.role = roleVo.getRole();
    i.inherited = roleDao.getFlatInheritance(roleVo.getRole());
    i.inheritedBy = roleDao.getRolesThatInherit(roleVo.getRole());
    return i;
}
Also used : RoleVO(com.serotonin.m2m2.vo.role.RoleVO)

Example 39 with RoleVO

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

the class SetPointEventHandlerDefinition method handleRoleEvent.

@Override
public void handleRoleEvent(SetPointEventHandlerVO vo, DaoEvent<? extends RoleVO> event) {
    // Remove and re-serialize our handler's script roles if it has a role that was deleted
    if (vo.getScriptRoles().getRoles().contains(event.getVo().getRole())) {
        switch(event.getType()) {
            case UPDATE:
                break;
            case DELETE:
                Set<Role> updated = new HashSet<>(vo.getScriptRoles().getRoles());
                updated.remove(event.getVo().getRole());
                Set<Role> allRoles = new HashSet<>();
                for (Role role : updated) {
                    allRoles.addAll(service.getAllInheritedRoles(role));
                }
                ScriptPermissions permission = new ScriptPermissions(allRoles, vo.getScriptRoles().getPermissionHolderName());
                vo.setScriptRoles(permission);
                eventHandlerService.update(vo.getId(), vo);
                break;
            default:
                break;
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) HashSet(java.util.HashSet)

Example 40 with RoleVO

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

the class RoleRestController method create.

@ApiOperation(value = "Create a Role", notes = "Admin only")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<RoleModel> create(@RequestBody RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
    RoleVO vo = service.insert(mapping.unmap(model, user, mapper));
    URI location = builder.path("/roles/{xid}").buildAndExpand(vo.getXid()).toUri();
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(location);
    return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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