Search in sources :

Example 1 with Role

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

the class RoleRestController method update.

@ApiOperation(value = "Update a Role List", notes = "Admin only")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}")
public ResponseEntity<RoleModel> update(@ApiParam(value = "XID of Role to update", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Role List of update", required = true, allowMultiple = false) @RequestBody RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
    RoleVO vo = service.update(xid, 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.OK);
}
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)

Example 2 with Role

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

the class RoleRestController method partialUpdate.

@ApiOperation(value = "Partially update a Role", notes = "Admin only")
@RequestMapping(method = RequestMethod.PATCH, value = "/{xid}")
public ResponseEntity<RoleModel> partialUpdate(@PathVariable String xid, @ApiParam(value = "Updated role", required = true) @PatchVORequestBody(service = RoleService.class, modelClass = RoleModel.class) RoleModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
    RoleVO vo = service.update(xid, 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.OK);
}
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)

Example 3 with Role

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

the class ScriptRestController method evalScript.

@Async
@ApiOperation(value = "Evaluate a filestore file as a script on the backend using a scripting engine")
@RequestMapping(method = RequestMethod.POST, value = "/eval-file-store/{fileStoreName}/**")
public CompletableFuture<Void> evalScript(@ApiParam(value = "File store name", required = true) @PathVariable(required = true) String fileStoreName, @ApiParam(value = "Script engine name", required = false) @RequestParam(required = false) String engineName, @ApiParam(value = "Script file character set", required = false, defaultValue = "UTF-8") @RequestParam(required = false, defaultValue = "UTF-8") String fileCharset, @ApiParam(value = "Script roles", required = false, allowMultiple = true) @RequestParam(required = false) String[] roles, @ApiIgnore @RemainingPath String path, @AuthenticationPrincipal PermissionHolder user, HttpServletRequest request, HttpServletResponse response) throws IOException {
    Path filePath = fileStoreService.getPathForRead(fileStoreName, path);
    if (!Files.exists(filePath)) {
        throw new NotFoundException();
    }
    if (engineName == null) {
        engineName = scriptService.findEngineForFile(filePath);
    }
    Charset fileCharsetParsed = Charset.forName(fileCharset);
    Set<Role> roleSet;
    if (roles != null) {
        roleSet = Arrays.stream(roles).map(xid -> this.roleService.get(xid).getRole()).collect(Collectors.toSet());
    } else {
        roleSet = user.getRoles();
    }
    EvalContext evalContext = new EvalContext();
    Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), Charset.forName(request.getCharacterEncoding())));
    Writer writer = new OutputStreamWriter(response.getOutputStream(), Charset.forName(response.getCharacterEncoding()));
    evalContext.setReader(reader);
    evalContext.setWriter(writer);
    evalContext.addBinding("reader", reader);
    evalContext.addBinding("writer", writer);
    if (permissionService.hasPermission(user, requestResponsePermission.getPermission())) {
        evalContext.addBinding("request", request);
        evalContext.addBinding("response", response);
    }
    this.scriptService.eval(new PathMangoScript(engineName, roleSet, filePath, fileCharsetParsed), evalContext);
    return CompletableFuture.completedFuture(null);
}
Also used : Path(java.nio.file.Path) RemainingPath(com.infiniteautomation.mango.rest.latest.resolver.RemainingPath) Role(com.serotonin.m2m2.vo.role.Role) InputStreamReader(java.io.InputStreamReader) EvalContext(com.infiniteautomation.mango.spring.script.EvalContext) BufferedReader(java.io.BufferedReader) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) PathMangoScript(com.infiniteautomation.mango.spring.script.PathMangoScript) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Async(org.springframework.scheduling.annotation.Async) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Role

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

the class Upgrade4 method upgrade.

@Override
protected void upgrade() throws Exception {
    try (OutputStream out = createUpdateLogOutputStream()) {
        // Get a reference to the superadmin permission in DB
        MangoPermission superadmin = getOrCreatePermissionNoCache(MangoPermission.superadminOnly());
        ejt.query("SELECT id, typeName, typeRef1 FROM events WHERE typeName=?", new Object[] { MaintenanceEventType.TYPE_NAME }, rs -> {
            int eventId = rs.getInt(1);
            Integer voId = rs.getInt(3);
            // Find the permission id for this me
            Integer permissionId = maintenanceEventPermissionMap.computeIfAbsent(voId, (k) -> {
                // Build the permission for this event
                Set<Role> allRequired = new HashSet<>();
                List<Integer> dataPointIds = ejt.queryForList(SELECT_POINT_IDS, new Object[] { k }, Integer.class);
                for (Integer dpId : dataPointIds) {
                    MangoPermission dataPointPermission = dataPointPermissionMap.computeIfAbsent(dpId, (pointId) -> {
                        Integer id = ejt.queryForInt("SELECT readPermissionId from dataPoints where id=?", new Object[] { pointId }, Common.NEW_ID);
                        if (id == Common.NEW_ID) {
                            return superadmin;
                        } else {
                            return permissionMap.computeIfAbsent(id, (pId) -> {
                                MangoPermission p = get(pId);
                                if (p == null) {
                                    return superadmin;
                                } else {
                                    return p;
                                }
                            });
                        }
                    });
                    dataPointPermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
                }
                List<Integer> dataSourceIds = ejt.queryForList(SELECT_DATA_SOURCE_IDS, new Object[] { k }, Integer.class);
                for (Integer dsId : dataSourceIds) {
                    MangoPermission dataSourcePermission = dataSourcePermissionMap.computeIfAbsent(dsId, (sourceId) -> {
                        Integer id = ejt.queryForInt("SELECT readPermissionId from dataSources where id=?", new Object[] { sourceId }, Common.NEW_ID);
                        if (id == Common.NEW_ID) {
                            return superadmin;
                        } else {
                            return permissionMap.computeIfAbsent(id, (pId) -> {
                                MangoPermission p = get(pId);
                                if (p == null) {
                                    return superadmin;
                                } else {
                                    return p;
                                }
                            });
                        }
                    });
                    dataSourcePermission.getRoles().stream().forEach(minterm -> allRequired.addAll(minterm));
                }
                MangoPermission permission;
                if (allRequired.size() == 0) {
                    permission = MangoPermission.superadminOnly();
                } else {
                    permission = MangoPermission.requireAllRoles(allRequired);
                }
                return getOrCreatePermission(permission).getId();
            });
            ejt.update("UPDATE events SET readPermissionId=? WHERE id=?", permissionId, eventId);
        });
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) OutputStream(java.io.OutputStream) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 5 with Role

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

the class RoleModelMapping method map.

@Override
public RoleModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
    RoleVO role = (RoleVO) from;
    RoleModel model = new RoleModel(role);
    if (role.getInherited() != null) {
        Set<String> inherited = new HashSet<>(role.getInherited().size());
        model.setInherited(inherited);
        for (Role inheritedRole : role.getInherited()) {
            inherited.add(inheritedRole.getXid());
        }
    }
    return model;
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) RoleVO(com.serotonin.m2m2.vo.role.RoleVO) HashSet(java.util.HashSet)

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