Search in sources :

Example 1 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.

the class MangoCustomMethodSecurityExpressionRoot method hasDataSourceXidPermission.

/**
 * Does this User have edit access for this data source
 * @param xid
 * @return
 */
public boolean hasDataSourceXidPermission(String xid) {
    User user = (User) this.getPrincipal();
    if (user.isAdmin())
        return true;
    DataSourceVO<?> dsvo = DataSourceDao.instance.getByXid(xid);
    if ((dsvo == null) || (!Permissions.hasDataSourcePermission(user, dsvo)))
        return false;
    return true;
}
Also used : User(com.serotonin.m2m2.vo.User)

Example 2 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorVO method validate.

public static void validate(String condition, DwrResponseI18n response) {
    try {
        User user = Common.getUser();
        final boolean admin = Permissions.hasAdmin(user);
        if (!admin)
            Permissions.ensureDataSourcePermission(user);
        LogicalOperator l = CompoundEventDetectorRT.parseConditionStatement(condition);
        List<String> keys = l.getDetectorKeys();
        // Get all of the point event detectors.
        List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(null, true);
        // Create a lookup of data sources.
        Map<Integer, DataSourceVO<?>> dss = new HashMap<>();
        for (DataSourceVO<?> ds : DataSourceDao.instance.getAll()) dss.put(ds.getId(), ds);
        for (String key : keys) {
            if (!key.startsWith(SimpleEventDetectorVO.POINT_EVENT_DETECTOR_PREFIX))
                continue;
            boolean found = false;
            for (DataPointVO dp : dataPoints) {
                if (!admin && !Permissions.hasDataSourcePermission(user, dss.get(dp.getDataSourceId())))
                    continue;
                if (found)
                    break;
            }
            if (!found)
                throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.invalidKey"));
        }
    } catch (ConditionParseException e) {
        response.addMessage("condition", e.getLocalizableMessage());
        if (e.isRange()) {
            response.addData("range", true);
            response.addData("from", e.getFrom());
            response.addData("to", e.getTo());
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) LogicalOperator(com.serotonin.m2m2.rt.event.compound.LogicalOperator) ConditionParseException(com.serotonin.m2m2.rt.event.compound.ConditionParseException) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 3 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.

the class DataSourceDwr method get.

@DwrPermission(user = true)
@Override
public ProcessResult get(int id) {
    ProcessResult response;
    try {
        if (id > 0) {
            response = super.get(id);
            // Kludge for modules to be able to use a default edit point for some of their tools (Bacnet for example needs this for adding lots of points)
            // This is an issue for opening AllDataPoints Point because it opens the Datasource too.
            // TODO to fix this we need to fix DataSourceEditDwr to not save the editing DataPoint state in the User, this will propogate into existing modules...
            DataSourceVO<?> vo = (DataSourceVO<?>) response.getData().get("vo");
            // Quick fix to ensure we don't keep the edit point around if we have switched data sources
            if ((Common.getUser().getEditPoint() == null) || (Common.getUser().getEditPoint().getDataSourceId() != vo.getId()) || (Common.getUser().getEditPoint().getDataSourceTypeName() != vo.getDefinition().getDataSourceTypeName())) {
                DataPointVO dp = new DataPointVO();
                dp.setXid(DataPointDao.instance.generateUniqueXid());
                dp.setDataSourceId(vo.getId());
                dp.setDataSourceTypeName(vo.getDefinition().getDataSourceTypeName());
                dp.setDeviceName(vo.getName());
                dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
                dp.defaultTextRenderer();
                dp.setXid(DataPointDao.instance.generateUniqueXid());
                dp.setPointLocator(vo.createPointLocator());
                Common.getUser().setEditPoint(dp);
            }
        } else {
            throw new ShouldNeverHappenException("Unable to get a new DataSource.");
        }
        // Setup the page info
        response.addData("editPagePath", ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getModule().getWebPath() + "/" + ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getEditPagePath());
        response.addData("statusPagePath", ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getModule().getWebPath() + "/" + ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getStatusPagePath());
    } catch (Exception e) {
        LOG.error(e.getMessage());
        response = new ProcessResult();
        response.addMessage(new TranslatableMessage("table.error.dwr", e.getMessage()));
    }
    return response;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.

the class DataSourceEditDwr method validatePoint.

protected ProcessResult validatePoint(int id, String xid, String name, PointLocatorVO<?> locator, DataPointDefaulter defaulter, boolean includePointList) {
    ProcessResult response = new ProcessResult();
    // This saving of the point into the User is a bad idea, need to rework to
    // pass the point back and forth to page.
    DataPointVO dp = getPoint(id, defaulter);
    dp.setXid(xid);
    dp.setName(name);
    dp.setPointLocator(locator);
    // Confirm that we are assinging a point to the correct data source
    DataSourceVO<?> ds = DataSourceDao.instance.get(dp.getDataSourceId());
    PointLocatorVO<?> plvo = ds.createPointLocator();
    if (plvo.getClass() != locator.getClass()) {
        response.addGenericMessage("validate.invalidType");
        return response;
    }
    // If we are a new point then only validate the basics
    if (id == Common.NEW_ID) {
        if (StringUtils.isBlank(xid))
            response.addContextualMessage("xid", "validate.required");
        else if (StringValidation.isLengthGreaterThan(xid, 50))
            response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
        else if (!DataPointDao.instance.isXidUnique(xid, id))
            response.addContextualMessage("xid", "validate.xidUsed");
        if (StringUtils.isBlank(name))
            response.addContextualMessage("name", "validate.required");
        // Load in the default Template
        DataPointPropertiesTemplateVO template = TemplateDao.instance.getDefaultDataPointTemplate(locator.getDataTypeId());
        if (template != null) {
            template.updateDataPointVO(dp);
        }
        // Should really be done elsewhere
        dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    } else if (id == DataPointDwr.COPY_ID) {
        dp.setId(Common.NEW_ID);
        if (StringUtils.isBlank(xid))
            response.addContextualMessage("xid", "validate.required");
        else if (StringValidation.isLengthGreaterThan(xid, 50))
            response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
        else if (!DataPointDao.instance.isXidUnique(xid, id))
            response.addContextualMessage("xid", "validate.xidUsed");
        if (StringUtils.isBlank(name))
            response.addContextualMessage("name", "validate.required");
    } else {
        // New validation on save for all settings on existing points
        dp.validate(response);
        if (dp.getChartRenderer() != null)
            dp.getChartRenderer().validate(response);
        if (dp.getTextRenderer() != null)
            dp.getTextRenderer().validate(response);
    }
    // Validate Locator
    locator.validate(response, dp);
    if (!response.getHasMessages()) {
        try {
            Common.runtimeManager.saveDataPoint(dp);
        } catch (DuplicateKeyException e) {
            response.addGenericMessage("pointEdit.detectors.duplicateXid");
            return response;
        } catch (LicenseViolatedException e) {
            response.addMessage(e.getErrorMessage());
            return response;
        }
        if (defaulter != null)
            defaulter.postSave(dp);
        response.addData("id", dp.getId());
        response.addData("vo", dp);
        if (includePointList)
            response.addData("points", getPoints());
        // Set the User Point
        Common.getUser().setEditPoint(dp);
    }
    return response;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 5 with DataSourceVO

use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.

the class DataSourceEditDwr method tryDataSourceSave.

protected ProcessResult tryDataSourceSave(DataSourceVO<?> ds) {
    ProcessResult response = new ProcessResult();
    User user = Common.getUser();
    if (!Permissions.hasPermission(user, ds.getEditPermission())) {
        response.addContextualMessage("dataSource.editPermission", "validate.mustHaveEditPermission");
        return response;
    }
    ds.validate(response);
    if (!response.getHasMessages()) {
        Common.runtimeManager.saveDataSource(ds);
        response.addData("id", ds.getId());
        // For new table method
        response.addData("vo", ds);
        user.setEditDataSource(ds);
    }
    return response;
}
Also used : User(com.serotonin.m2m2.vo.User) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Aggregations

User (com.serotonin.m2m2.vo.User)31 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)28 ArrayList (java.util.ArrayList)21 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)19 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)18 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)18 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)15 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)15 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 List (java.util.List)10 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)9 AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)8 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)6 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)6 DataPointPropertiesTemplateVO (com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO)6 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)5 URI (java.net.URI)5 HashMap (java.util.HashMap)5