Search in sources :

Example 86 with PermissionHolder

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;
}
Also used : AbstractPointEventDetectorModel(com.infiniteautomation.mango.rest.latest.model.event.detectors.AbstractPointEventDetectorModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) AbstractDataSourceModel(com.infiniteautomation.mango.rest.latest.model.datasource.AbstractDataSourceModel) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) HashMap(java.util.HashMap) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) PublisherEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.PublisherEventTypeModel) ArrayList(java.util.ArrayList) AbstractPublisherModel(com.infiniteautomation.mango.rest.latest.model.publisher.AbstractPublisherModel) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) SystemEventTypeDefinition(com.serotonin.m2m2.module.SystemEventTypeDefinition) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) PublisherVO(com.serotonin.m2m2.vo.publish.PublisherVO) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) DataPointModel(com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel) DataSourceEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataSourceEventTypeModel) DataPointEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.DataPointEventTypeModel) SystemEventTypeModel(com.infiniteautomation.mango.rest.latest.model.event.SystemEventTypeModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) EventTypeVOModel(com.infiniteautomation.mango.rest.latest.model.event.EventTypeVOModel)

Example 87 with PermissionHolder

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 88 with PermissionHolder

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) URI(java.net.URI) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 89 with PermissionHolder

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;
}
Also used : CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ArrayList(java.util.ArrayList) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) Module(com.serotonin.m2m2.module.Module) CoreModule(com.serotonin.m2m2.module.ModuleRegistry.CoreModule) InvalidModule(com.infiniteautomation.mango.rest.latest.model.modules.UpgradeUploadResult.InvalidModule) ModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.ModuleModel) CoreModuleModel(com.infiniteautomation.mango.rest.latest.model.modules.CoreModuleModel) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 90 with PermissionHolder

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) JsonString(com.serotonin.json.type.JsonString) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)205 ApiOperation (io.swagger.annotations.ApiOperation)98 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)98 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)86 ResponseEntity (org.springframework.http.ResponseEntity)53 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)50 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)50 ArrayList (java.util.ArrayList)50 HttpHeaders (org.springframework.http.HttpHeaders)50 URI (java.net.URI)48 List (java.util.List)43 User (com.serotonin.m2m2.vo.User)37 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)35 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)32 Common (com.serotonin.m2m2.Common)31 Collectors (java.util.stream.Collectors)30 Role (com.serotonin.m2m2.vo.role.Role)29 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)28 Autowired (org.springframework.beans.factory.annotation.Autowired)25 HashMap (java.util.HashMap)24