use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class EventTypesRestController method getEventTypesForSubtype.
/**
* Generate a list of all event types generalized by sub-type
*/
private List<EventTypeVOModel<?, ?, ?>> getEventTypesForSubtype(String typeName, String subtype, PermissionHolder user) throws NotFoundException {
// track if the type was a default type
List<EventTypeVOModel<?, ?, ?>> types = new ArrayList<>();
boolean found = false;
switch(typeName) {
case EventTypeNames.DATA_POINT:
// There is no subtype for data points
if (subtype != null)
throw new BadRequestException();
// Get Event Detectors, ensure only 1 data point in list
// TODO via query instead
List<AbstractPointEventDetectorVO> detectors = this.eventDetectorDao.getAllPointEventDetectors();
Map<Integer, DataPointVO> uniquePointsMap = new HashMap<>();
for (AbstractPointEventDetectorVO detector : detectors) {
uniquePointsMap.put(detector.getDataPoint().getId(), detector.getDataPoint());
}
for (DataPointVO vo : uniquePointsMap.values()) {
// Shortcut to check permissions via event type
if (dataPointService.hasReadPermission(user, vo)) {
DataPointEventTypeModel model = new DataPointEventTypeModel(new DataPointEventType(vo.getDataSourceId(), vo.getId(), 0, null), modelMapper.map(vo, DataPointModel.class, user));
types.add(new EventTypeVOModel<DataPointEventType, DataPointModel, AbstractPointEventDetectorModel<?>>(model, new TranslatableMessage("event.eventsFor", vo.getName()), false, true, true));
}
}
found = true;
break;
case EventTypeNames.DATA_SOURCE:
// There is no subtype for data sources
if (subtype != null)
throw new BadRequestException();
for (DataSourceVO vo : dataSourceDao.getAll()) {
if (permissionService.hasPermission(user, vo.getReadPermission())) {
AbstractDataSourceModel<?> dsModel = modelMapper.map(vo, AbstractDataSourceModel.class, user);
DataSourceEventTypeModel model = new DataSourceEventTypeModel(new DataSourceEventType(vo.getId(), 0), dsModel);
types.add(new EventTypeVOModel<DataSourceEventType, AbstractDataSourceModel<?>, String>(model, new TranslatableMessage("event.eventsFor", vo.getName()), false, true, true));
}
}
found = true;
break;
case EventTypeNames.PUBLISHER:
// There is no subtype for publishers
if (subtype != null)
throw new BadRequestException();
// There are no permissions for publishers
if (!permissionService.hasAdminRole(user))
break;
for (PublisherVO vo : publisherDao.getAll()) {
AbstractPublisherModel<?, ?> publisherModel = modelMapper.map(vo, AbstractPublisherModel.class, user);
PublisherEventTypeModel model = new PublisherEventTypeModel(new PublisherEventType(vo.getId(), 0), publisherModel);
types.add(new EventTypeVOModel<PublisherEventType, AbstractPublisherModel<?, ?>, String>(model, new TranslatableMessage("event.eventsFor", vo.getName()), false, true, true));
}
found = true;
break;
case EventTypeNames.SYSTEM:
// System
for (SystemEventTypeDefinition def : ModuleRegistry.getDefinitions(SystemEventTypeDefinition.class)) {
if (!StringUtils.equals(def.getTypeName(), subtype))
continue;
found = true;
for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId1(user, subtype)) {
SystemEventType eventType = (SystemEventType) type.getEventType();
SystemEventTypeModel model = modelMapper.map(eventType, SystemEventTypeModel.class, user);
types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), true, def.supportsReferenceId1(), def.supportsReferenceId2()));
}
break;
}
break;
case EventTypeNames.AUDIT:
// Audit does not yet support reference id 1
throw new BadRequestException();
}
if (!found) {
// Module defined
for (EventTypeDefinition def : ModuleRegistry.getDefinitions(EventTypeDefinition.class)) {
if (StringUtils.equals(typeName, def.getTypeName())) {
found = true;
for (EventTypeVO type : def.generatePossibleEventTypesWithReferenceId1(user, subtype, permissionService)) {
EventType eventType = type.getEventType();
AbstractEventTypeModel<?, ?, ?> model = modelMapper.map(eventType, AbstractEventTypeModel.class, user);
types.add(new EventTypeVOModel<>(model, type.getDescription(), type.getAlarmLevel(), def.supportsSubType(), def.supportsReferenceId1(), def.supportsReferenceId2()));
}
break;
}
}
}
if (!found)
throw new NotFoundException();
return types;
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class PointValueRestController method deletePointValues.
@ApiOperation(value = "Delete point values >= from and < to", notes = "The user must have set permission to the data point. If date is not supplied it defaults to now.")
@RequestMapping(method = RequestMethod.DELETE, value = "/{xid}")
public ResponseEntity<Long> deletePointValues(@ApiParam(value = "Point xids", required = true) @PathVariable String xid, @ApiParam(value = "From time") @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time") @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @AuthenticationPrincipal PermissionHolder user) {
DataPointVO vo = dataPointService.get(xid);
dataPointService.ensureSetPermission(user, vo);
ZoneId zoneId;
if (timezone == null) {
if (from != null) {
zoneId = from.getZone();
} else if (to != null)
zoneId = to.getZone();
else
zoneId = TimeZone.getDefault().toZoneId();
} else {
zoneId = ZoneId.of(timezone);
}
// Set the timezone on the from and to dates
long current = Common.timer.currentTimeMillis();
if (from != null)
from = from.withZoneSameInstant(zoneId);
else
from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(current), zoneId);
if (to != null)
to = to.withZoneSameInstant(zoneId);
else
to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(current), zoneId);
Optional<Long> result = Common.runtimeManager.purgeDataPointValuesBetween(vo, from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli());
return ResponseEntity.ok().body(result.orElse(null));
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class PublishersRestController method partialUpdate.
@ApiOperation(value = "Partially update a publisher", notes = "Requires edit permission")
@RequestMapping(method = RequestMethod.PATCH, value = "/{xid}")
public ResponseEntity<AbstractPublisherModel<?, ?>> partialUpdate(@PathVariable String xid, @ApiParam(value = "Updated data source", required = true) @PatchVORequestBody(service = PublisherService.class, modelClass = AbstractPublisherModel.class) AbstractPublisherModel<?, ?> model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
PublisherVO vo = service.update(xid, model.toVO());
insertPoints(vo, model.getPoints(), user);
URI location = builder.path("/publishers/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(map.apply(vo, user), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class PublishersRestController method save.
@ApiOperation(value = "Save publisher")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<AbstractPublisherModel<?, ?>> save(@RequestBody() AbstractPublisherModel<?, ?> model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
PublisherVO vo = this.service.insert(model.toVO());
insertPoints(vo, model.getPoints(), user);
URI location = builder.path("/publishers/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(map.apply(vo, user), headers, HttpStatus.CREATED);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class PublishersRestController method exportQuery.
@ApiOperation(value = "Export formatted for Configuration Import by supplying an RQL query", notes = "User must have read permission")
@RequestMapping(method = RequestMethod.GET, value = "/export", produces = MediaTypes.SEROTONIN_JSON_VALUE)
public Map<String, JsonStreamedArray> exportQuery(HttpServletRequest request, @AuthenticationPrincipal PermissionHolder user) {
ASTNode rql = RQLUtils.parseRQLtoAST(request.getQueryString());
Map<String, JsonStreamedArray> export = new HashMap<>();
if (permissionService.hasAdminRole(user)) {
export.put("publishers", new StreamedSeroJsonVORqlQuery<>(service, rql, null, null, null));
} else {
export.put("publishers", new StreamedSeroJsonVORqlQuery<>(service, rql, null, null, null, vo -> service.hasReadPermission(user, vo)));
}
return export;
}
Aggregations