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 MailingListRestController method partialUpdate.
@ApiOperation(value = "Partially update a Mailing List", notes = "Requires edit permission", response = MailingListWithRecipientsModel.class)
@RequestMapping(method = RequestMethod.PATCH, value = "/{xid}")
public ResponseEntity<MailingListModel> partialUpdate(@PathVariable String xid, @ApiParam(value = "Updated mailing list", required = true) @PatchVORequestBody(service = MailingListService.class, modelClass = MailingListWithRecipientsModel.class) MailingListWithRecipientsModel model, @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
MailingList vo = service.update(xid, mapping.unmap(model, user, mapper));
URI location = builder.path("/mailing-lists/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class MailingListRestController method create.
@ApiOperation(value = "Create a Mailing List", notes = "Requires global Create Mailing List privileges", response = MailingListWithRecipientsModel.class)
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<MailingListModel> create(@RequestBody MailingListWithRecipientsModel model, @ApiParam(value = "User", required = true) @AuthenticationPrincipal PermissionHolder user, UriComponentsBuilder builder) {
MailingList vo = service.insert(mapping.unmap(model, user, mapper));
URI location = builder.path("/mailing-lists/{xid}").buildAndExpand(vo.getXid()).toUri();
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity<>(mapping.map(vo, user, mapper), headers, HttpStatus.OK);
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class ModulesRestController method listModules.
@ApiOperation(value = "List Current Installed Modules", notes = "List all installed")
@RequestMapping(method = RequestMethod.GET, value = "/list")
public List<ModuleModel> listModules(@AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
List<ModuleModel> models = new ArrayList<ModuleModel>();
List<Module> modules = ModuleRegistry.getModules();
for (Module module : modules) {
if (module instanceof CoreModule) {
CoreModuleModel coreModel = new CoreModuleModel(ModuleRegistry.getModule(ModuleRegistry.CORE_MODULE_NAME));
coreModel.setGuid(Providers.get(ICoreLicense.class).getGuid());
coreModel.setInstanceDescription(SystemSettingsDao.getInstance().getValue(SystemSettingsDao.INSTANCE_DESCRIPTION));
coreModel.setDistributor(Common.envProps.getString("distributor"));
coreModel.setUpgradeVersionState(SystemSettingsDao.getInstance().getIntValue(SystemSettingsDao.UPGRADE_VERSION_STATE));
coreModel.setStoreUrl(Common.envProps.getString("store.url"));
models.add(coreModel);
} else {
models.add(new ModuleModel(module));
}
}
// Add the unloaded modules at the end?
List<Module> unloaded = ModuleRegistry.getUnloadedModules();
for (Module module : unloaded) {
ModuleModel model = new ModuleModel(module);
model.setUnloaded(true);
models.add(model);
}
return models;
}
use of com.serotonin.m2m2.vo.permission.PermissionHolder in project ma-modules-public by infiniteautomation.
the class ModulesRestController method upgrade.
@ApiOperation(value = "Download Upgrades and optionally backup and restart", notes = "Use Modules web socket to track progress")
@RequestMapping(method = RequestMethod.POST, value = "/upgrade")
public ResponseEntity<TranslatableMessage> upgrade(@ApiParam(value = "Perform Backup first", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean backup, @ApiParam(value = "Restart when completed", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean restart, @ApiParam(value = "Desired Upgrades", required = true) @RequestBody(required = true) ModuleUpgradesModel model, @AuthenticationPrincipal PermissionHolder user) {
permissionService.ensureAdminRole(user);
// Start Downloads
String status = service.startDownloads(model.fullModulesList(), backup, restart);
if (status == null) {
return new ResponseEntity<>(new TranslatableMessage("rest.httpStatus.200"), HttpStatus.OK);
} else {
// headers are for compatibility with v1 for the UI
HttpHeaders headers = new HttpHeaders();
headers.add("messages", stripAndTrimHeader(status, -1));
return new ResponseEntity<>(new TranslatableMessage("common.default", status), HttpStatus.NOT_MODIFIED);
}
}
Aggregations