Search in sources :

Example 36 with PermissionException

use of com.serotonin.m2m2.vo.permission.PermissionException in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method enableDisable.

@ApiOperation(value = "Enable/disable/restart a data source")
@RequestMapping(method = RequestMethod.PUT, value = "/enable-disable/{xid}")
public ResponseEntity<DataPointModel> enableDisable(@AuthenticationPrincipal User user, @PathVariable String xid, @ApiParam(value = "Enable or disable the data source", required = true, allowMultiple = false) @RequestParam(required = true) boolean enabled, @ApiParam(value = "Restart the data source, enabled must equal true", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart) {
    DataSourceVO<?> dsvo = DataSourceDao.instance.getByXid(xid);
    if (dsvo == null)
        throw new NotFoundRestException();
    try {
        Permissions.ensureDataSourcePermission(user, dsvo);
    } catch (PermissionException e) {
        throw new AccessDeniedException("User does not have permission to edit the data source", e);
    }
    if (enabled && restart) {
        dsvo.setEnabled(true);
        // saving will restart it
        Common.runtimeManager.saveDataSource(dsvo);
    } else if (dsvo.isEnabled() != enabled) {
        dsvo.setEnabled(enabled);
        Common.runtimeManager.saveDataSource(dsvo);
    }
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ResponseEntity(org.springframework.http.ResponseEntity) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 37 with PermissionException

use of com.serotonin.m2m2.vo.permission.PermissionException in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method getDataSource.

@ApiOperation(value = "Get data source by xid", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> getDataSource(HttpServletRequest request, @PathVariable String xid) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = DataSourceDao.instance.getByXid(xid);
        if (vo == null) {
            return new ResponseEntity<AbstractDataSourceModel<?>>(HttpStatus.NOT_FOUND);
        } else {
            try {
                if (Permissions.hasDataSourcePermission(user, vo))
                    return result.createResponseEntity(vo.asModel());
                else {
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            } catch (PermissionException e) {
                LOG.warn(e.getMessage(), e);
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        }
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with PermissionException

use of com.serotonin.m2m2.vo.permission.PermissionException in project ma-modules-public by infiniteautomation.

the class PointHierarchyRestController method getPath.

/**
 * Get a path to a folder
 * @param xid
 * @param request
 * @return
 */
@ApiOperation(value = "Get path to a point using point's XID", notes = "Points returned based on user priviledges")
@RequestMapping(method = RequestMethod.GET, value = "/path/{xid}", produces = { "application/json" })
public ResponseEntity<List<String>> getPath(@PathVariable String xid, HttpServletRequest request) {
    RestProcessResult<List<String>> result = new RestProcessResult<List<String>>(HttpStatus.OK);
    PointHierarchy ph = DataPointDao.instance.getPointHierarchy(true);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        try {
            if (!Permissions.hasDataPointReadPermission(user, vo)) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            } else {
                return result.createResponseEntity(ph.getPath(vo.getId()));
            }
        } catch (PermissionException e) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) PointHierarchy(com.serotonin.m2m2.vo.hierarchy.PointHierarchy) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 39 with PermissionException

use of com.serotonin.m2m2.vo.permission.PermissionException in project ma-modules-public by infiniteautomation.

the class DataPointSummaryStreamCallback method writeCsv.

@Override
protected void writeCsv(DataPointVO vo) throws IOException {
    try {
        if (Permissions.hasDataPointReadPermission(user, vo)) {
            DataPointSummary model = this.controller.createModel(vo);
            this.csvWriter.writeNext(model);
        }
    } catch (PermissionException e) {
    // Munched
    }
}
Also used : PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary)

Example 40 with PermissionException

use of com.serotonin.m2m2.vo.permission.PermissionException in project ma-core-public by infiniteautomation.

the class DataSourceEditDwr method exportDataPoint.

@DwrPermission(user = true)
public String exportDataPoint(int dataPointId) {
    DataSourceVO<?> ds = Common.getUser().getEditDataSource();
    DataPointVO dp = DataPointDao.instance.getDataPoint(dataPointId);
    if (dp == null)
        return null;
    if (dp.getDataSourceId() != ds.getId())
        throw new PermissionException(new TranslatableMessage("common.default", "Wrong data source"), Common.getUser());
    Map<String, Object> data = new LinkedHashMap<>();
    List<DataPointVO> dss = new ArrayList<>();
    dss.add(dp);
    data.put(ConfigurationExportData.DATA_POINTS, dss);
    return EmportDwr.export(data);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) LinkedHashMap(java.util.LinkedHashMap) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)42 User (com.serotonin.m2m2.vo.User)34 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)29 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)25 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)12 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)11 RTException (com.serotonin.m2m2.rt.RTException)11 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)11 RestValidationResult (com.infiniteautomation.mango.rest.v2.model.RestValidationResult)10 ArrayList (java.util.ArrayList)10 List (java.util.List)9 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)8 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)7 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)7 URI (java.net.URI)7 HashMap (java.util.HashMap)7 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)6