Search in sources :

Example 51 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MangoPermissionModelDeserializer method nodeToModel.

@SuppressWarnings("unchecked")
public MangoPermissionModel nodeToModel(JsonNode tree, ObjectMapper mapper) throws JsonProcessingException {
    Set<Set<Role>> roles = new HashSet<>();
    if (tree instanceof ArrayNode) {
        Set<Object> outerSet = mapper.treeToValue(tree, Set.class);
        for (Object o : outerSet) {
            Set<Role> innerRoles = new HashSet<>();
            roles.add(innerRoles);
            if (o instanceof Iterable) {
                for (String xid : (Iterable<String>) o) {
                    Role role = permissionService.getRole(xid);
                    if (role != null) {
                        innerRoles.add(role);
                    } else {
                        // Let validation pick this up
                        innerRoles.add(new Role(Common.NEW_ID, xid));
                    }
                }
            } else {
                String xid = (String) o;
                Role role = permissionService.getRole(xid);
                if (role != null) {
                    innerRoles.add(role);
                } else {
                    // Let validation pick this up
                    innerRoles.add(new Role(Common.NEW_ID, xid));
                }
            }
        }
    } else if (tree instanceof TextNode) {
        Set<String> xids = PermissionService.explodeLegacyPermissionGroups(tree.asText());
        for (String xid : xids) {
            Role role = permissionService.getRole(xid);
            if (role != null) {
                roles.add(Collections.singleton(role));
            } else {
                // Let validation pick this up
                roles.add(Collections.singleton(new Role(Common.NEW_ID, xid)));
            }
        }
    }
    return new MangoPermissionModel(new MangoPermission(roles));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TextNode(com.fasterxml.jackson.databind.node.TextNode) Role(com.serotonin.m2m2.vo.role.Role) MangoPermissionModel(com.infiniteautomation.mango.rest.latest.model.permissions.MangoPermissionModel) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Example 52 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class SystemMetricsRestController method get.

@ApiOperation(value = "Get the current value for one System Metric by its ID", notes = "")
@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public ValueMonitorModel get(@ApiParam(value = "Valid Monitor id", required = true, allowMultiple = false) @PathVariable String id, @AuthenticationPrincipal PermissionHolder user) {
    MangoPermission permission = definition.getPermission();
    service.ensurePermission(user, permission);
    List<ValueMonitor<?>> values = Common.MONITORED_VALUES.getMonitors();
    for (ValueMonitor<?> v : values) {
        if (v.getId().equals(id)) {
            return new ValueMonitorModel(v);
        }
    }
    throw new NotFoundException();
}
Also used : ValueMonitorModel(com.infiniteautomation.mango.rest.latest.model.ValueMonitorModel) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ValueMonitor(com.infiniteautomation.mango.monitor.ValueMonitor) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 53 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MaintenanceEventEmportDefinition method doImport.

@Override
public void doImport(JsonValue jsonValue, ImportContext importContext, PermissionHolder importer) throws JsonException {
    MaintenanceEventsService service = Common.getBean(MaintenanceEventsService.class);
    JsonObject maintenanceEvent = jsonValue.toJsonObject();
    String xid = maintenanceEvent.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = service.generateUniqueXid();
    MaintenanceEventVO vo = null;
    if (StringUtils.isBlank(xid)) {
        xid = service.generateUniqueXid();
    } else {
        try {
            vo = service.get(xid);
        } catch (NotFoundException e) {
        }
    }
    if (vo == null) {
        vo = new MaintenanceEventVO();
        vo.setXid(xid);
    }
    try {
        importContext.getReader().readInto(vo, maintenanceEvent);
        // Ensure we have a default permission since null is valid in Mango 3.x
        if (vo.getTogglePermission() == null) {
            vo.setTogglePermission(new MangoPermission());
        }
        boolean isnew = vo.getId() == Common.NEW_ID;
        if (isnew) {
            service.insert(vo);
        } else {
            service.update(vo.getId(), vo);
        }
        importContext.addSuccessMessage(isnew, "emport.maintenanceEvent.prefix", xid);
    } catch (ValidationException e) {
        importContext.copyValidationMessages(e.getValidationResult(), "emport.maintenanceEvent.prefix", xid);
    } catch (TranslatableJsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        importContext.getResult().addGenericMessage("emport.maintenanceEvent.prefix", xid, importContext.getJsonExceptionMessage(e));
    }
}
Also used : MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) JsonObject(com.serotonin.json.type.JsonObject) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission)

Example 54 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MaintenanceEventType method getEventPermission.

@Override
public MangoPermission getEventPermission(Map<String, Object> context, PermissionService service) {
    DataSourceService dataSourceService = Common.getBean(DataSourceService.class);
    DataPointService dataPointService = Common.getBean(DataPointService.class);
    MaintenanceEventsService maintenanceEventService = Common.getBean(MaintenanceEventsService.class);
    Set<Role> allRequired = new HashSet<>();
    try {
        MaintenanceEventVO vo = maintenanceEventService.get(maintenanceId);
        try {
            for (int dsId : vo.getDataSources()) {
                MangoPermission read = dataSourceService.getReadPermission(dsId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
        try {
            for (int dpId : vo.getDataPoints()) {
                MangoPermission read = dataPointService.getReadPermission(dpId);
                read.getRoles().forEach(allRequired::addAll);
            }
        } catch (NotFoundException e) {
        // Ignore this item
        }
    } catch (NotFoundException e) {
    // Ignore all of it
    }
    if (allRequired.size() == 0) {
        return MangoPermission.superadminOnly();
    } else {
        return MangoPermission.requireAllRoles(allRequired);
    }
}
Also used : DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) MaintenanceEventsService(com.infiniteautomation.mango.spring.service.maintenanceEvents.MaintenanceEventsService) Role(com.serotonin.m2m2.vo.role.Role) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) HashSet(java.util.HashSet)

Example 55 with MangoPermission

use of com.infiniteautomation.mango.permission.MangoPermission in project ma-modules-public by infiniteautomation.

the class MaintenanceEventModel method toVO.

@Override
public MaintenanceEventVO toVO() {
    MaintenanceEventVO vo = super.toVO();
    ProcessResult result = new ProcessResult();
    if (dataSources != null) {
        Set<Integer> ids = new HashSet<>();
        for (String xid : dataSources) {
            Integer id = DataSourceDao.getInstance().getIdByXid(xid);
            if (id != null)
                ids.add(id);
            else
                result.addContextualMessage("dataSources", "maintenanceEvents.validate.missingDataSource", xid);
        }
        vo.setDataSources(new ArrayList<>(ids));
    }
    if (dataPoints != null) {
        Set<Integer> ids = new HashSet<>();
        for (String xid : dataPoints) {
            Integer id = DataPointDao.getInstance().getIdByXid(xid);
            if (id != null)
                ids.add(id);
            else
                result.addContextualMessage("dataPoints", "maintenanceEvents.validate.missingDataPoint", xid);
        }
        vo.setDataPoints(new ArrayList<>(ids));
    }
    if (result.getHasMessages())
        throw new ValidationException(result);
    vo.setAlarmLevel(alarmLevel);
    vo.setScheduleType(MaintenanceEventVO.TYPE_CODES.getId(scheduleType));
    vo.setDisabled(disabled == null ? false : disabled);
    vo.setActiveYear(activeYear == null ? 0 : activeYear);
    vo.setActiveMonth(activeMonth == null ? 0 : activeMonth);
    vo.setActiveDay(activeDay == null ? 0 : activeDay);
    vo.setActiveHour(activeHour == null ? 0 : activeHour);
    vo.setActiveMinute(activeMinute == null ? 0 : activeMinute);
    vo.setActiveSecond(activeSecond == null ? 0 : activeSecond);
    vo.setActiveCron(activeCron);
    vo.setInactiveYear(inactiveYear == null ? 0 : inactiveYear);
    vo.setInactiveMonth(inactiveMonth == null ? 0 : inactiveMonth);
    vo.setInactiveDay(inactiveDay == null ? 0 : inactiveDay);
    vo.setInactiveHour(inactiveHour == null ? 0 : inactiveHour);
    vo.setInactiveMinute(inactiveMinute == null ? 0 : inactiveMinute);
    vo.setInactiveSecond(inactiveSecond == null ? 0 : inactiveSecond);
    vo.setInactiveCron(inactiveCron);
    vo.setTimeoutPeriods(timeoutPeriods == null ? 0 : timeoutPeriods);
    vo.setTimeoutPeriodType(Common.TIME_PERIOD_CODES.getId(timeoutPeriodType));
    vo.setTogglePermission(togglePermission != null ? togglePermission.getPermission() : new MangoPermission());
    return vo;
}
Also used : MaintenanceEventVO(com.serotonin.m2m2.maintenanceEvents.MaintenanceEventVO) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) HashSet(java.util.HashSet)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)93 Role (com.serotonin.m2m2.vo.role.Role)22 HashSet (java.util.HashSet)21 Set (java.util.Set)18 HashMap (java.util.HashMap)13 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)12 PermissionDefinition (com.serotonin.m2m2.module.PermissionDefinition)11 JsonException (com.serotonin.json.JsonException)10 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)9 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)8 JsonValue (com.serotonin.json.type.JsonValue)7 ArrayList (java.util.ArrayList)7 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 User (com.serotonin.m2m2.vo.User)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4