Search in sources :

Example 11 with Permissions

use of com.serotonin.m2m2.vo.permission.Permissions in project ma-core-public by infiniteautomation.

the class DataPointDetailsController method handleRequest.

@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws Exception {
    User user = Common.getHttpUser();
    int id = -1;
    if (user.getEditPoint() != null)
        id = user.getEditPoint().getId();
    DataPointDao dataPointDao = DataPointDao.instance;
    String idStr = request.getParameter("dpid");
    DataPointVO point = null;
    if (StringUtils.equals(idStr, "exception"))
        throw new IOException("testing");
    else if (StringUtils.equals(idStr, "permission-exception"))
        throw new PermissionException(new TranslatableMessage("common.default", "Testing"), user);
    if (StringUtils.isBlank(idStr)) {
        // Check for pedid (point event detector id)
        String pedStr = request.getParameter("pedid");
        if (StringUtils.isBlank(pedStr)) {
            // Check if an XID was provided.
            String xid = request.getParameter("dpxid");
            if (!StringUtils.isBlank(xid)) {
                model.put("currentXid", xid);
                point = dataPointDao.getDataPoint(xid);
                id = point == null ? -2 : point.getId();
            }
        } else {
            int pedid = Integer.parseInt(pedStr);
            id = EventDetectorDao.instance.getSourceId(pedid, EventType.EventTypeNames.DATA_POINT);
        }
    } else
        id = Integer.parseInt(idStr);
    // Find accessible points for the goto list
    List<DataPointSummary> userPoints = ControllerUtils.addPointListDataToModel(user, id, model);
    // Get the point.
    if (point == null && id != -1)
        point = dataPointDao.getDataPoint(id);
    if (point == null && id != -2 && /* -2 means an explicit XID was provided but not found */
    !userPoints.isEmpty()) {
        // Load at least 1 point, there may be many points but some might not actually load if thier data source DNE anymore
        for (DataPointSummary userPoint : userPoints) {
            point = dataPointDao.getDataPoint(userPoint.getId());
            if (point != null)
                break;
        }
    }
    if (point != null) {
        // Check permissions
        Permissions.ensureDataPointReadPermission(user, point);
        // Put the point in the model.
        model.put("point", point);
        // Get the users that have access to this point.
        List<User> allUsers = UserDao.instance.getUsers();
        List<Map<String, Object>> users = new LinkedList<>();
        Map<String, Object> userData;
        int accessType;
        for (User mangoUser : allUsers) {
            accessType = Permissions.getDataPointAccessType(mangoUser, point);
            if (accessType != Permissions.DataPointAccessTypes.NONE) {
                userData = new HashMap<>();
                userData.put("user", mangoUser);
                userData.put("accessType", accessType);
                users.add(userData);
            }
        }
        model.put("users", users);
        // Determine whether the link to edit the point should be displayed
        model.put("pointEditor", Permissions.hasDataSourcePermission(user, point.getDataSourceId()));
        // Put the events in the model.
        model.put("events", EventDao.instance.getEventsForDataPoint(id, user.getId()));
        // Put the default history table count into the model. Default to 10.
        int historyLimit = 10;
        if (point.getChartRenderer() instanceof TableChartRenderer)
            historyLimit = ((TableChartRenderer) point.getChartRenderer()).getLimit();
        else if (point.getChartRenderer() instanceof ImageFlipbookRenderer)
            historyLimit = ((ImageFlipbookRenderer) point.getChartRenderer()).getLimit();
        model.put("historyLimit", historyLimit);
        // Determine our image chart rendering capabilities.
        if (ImageChartRenderer.getDefinition().supports(point.getPointLocator().getDataTypeId())) {
            // This point can render an image chart. Carry on...
            int periodType = Common.TimePeriods.DAYS;
            int periodCount = 1;
            if (point.getChartRenderer() instanceof ImageChartRenderer) {
                ImageChartRenderer r = (ImageChartRenderer) point.getChartRenderer();
                periodType = r.getTimePeriod();
                periodCount = r.getNumberOfPeriods();
            }
            model.put("periodType", periodType);
            model.put("periodCount", periodCount);
        }
        // Determine out flipbook rendering capabilities
        if (ImageFlipbookRenderer.getDefinition().supports(point.getPointLocator().getDataTypeId()))
            model.put("flipbookLimit", 10);
        // Set the point in the session for the dwr.
        user.setEditPoint(point);
        model.put("currentXid", point.getXid());
        model.put("hierPath", CollectionUtils.implode(dataPointDao.getPointHierarchy(true).getPath(id), " &raquo; "));
    }
    return null;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) TableChartRenderer(com.serotonin.m2m2.view.chart.TableChartRenderer) DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary) User(com.serotonin.m2m2.vo.User) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IOException(java.io.IOException) ImageChartRenderer(com.serotonin.m2m2.view.chart.ImageChartRenderer) LinkedList(java.util.LinkedList) ImageFlipbookRenderer(com.serotonin.m2m2.view.chart.ImageFlipbookRenderer) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Permissions

use of com.serotonin.m2m2.vo.permission.Permissions in project ma-core-public by infiniteautomation.

the class DataSourceQuery method query.

public List<DataSourceWrapper> query(String query) {
    ASTNode root = parser.parse(query);
    BaseSqlQuery<DataSourceVO<?>> sqlQuery = DataSourceDao.instance.createQuery(root, true);
    List<DataSourceVO<?>> dataSources = sqlQuery.immediateQuery();
    List<DataSourceWrapper> results = new ArrayList<DataSourceWrapper>();
    // Filter on permissions
    for (DataSourceVO<?> ds : dataSources) {
        if (Permissions.hasDataSourcePermission(permissions.getDataSourcePermissions(), ds)) {
            List<DataPointWrapper> points = getPointsForSource(ds);
            results.add(new DataSourceWrapper(ds, points));
        }
    }
    return results;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList)

Example 13 with Permissions

use of com.serotonin.m2m2.vo.permission.Permissions in project ma-core-public by infiniteautomation.

the class CompiledScriptExecutor method execute.

/**
 * Execute the script on the common engine
 * @param script
 * @param context
 * @param additionalContext
 * @param runtime
 * @param dataTypeId
 * @param timestamp
 * @param permissions
 * @param scriptWriter
 * @return
 * @throws ScriptException
 * @throws ResultTypeException
 */
public static PointValueTime execute(CompiledScript script, Map<String, IDataPointValueSource> context, Map<String, Object> additionalContext, long runtime, int dataTypeId, long timestamp, ScriptPermissions permissions, PrintWriter scriptWriter, ScriptLog log, ScriptPointValueSetter setter, List<JsonImportExclusion> importExclusions, boolean testRun) throws ScriptException, ResultTypeException {
    // StopWatch stopWatch = new Log4JStopWatch();
    // stopWatch.start();
    ensureInit();
    // Create the wrapper object context.
    ScriptEngine engine = script.getEngine();
    // Prepare the Engine
    Bindings engineScope = prepareEngine(engine, context, additionalContext, runtime, timestamp, permissions, scriptWriter, log, setter, importExclusions, testRun);
    // Execute.
    Object result;
    try {
        result = script.eval(engineScope);
    } catch (ScriptException e) {
        throw prettyScriptMessage(e);
    }
    PointValueTime value = getResult(engine, result, dataTypeId, timestamp);
    // stopWatch.stop("execute()");
    return value;
}
Also used : ScriptException(javax.script.ScriptException) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 14 with Permissions

use of com.serotonin.m2m2.vo.permission.Permissions in project ma-core-public by infiniteautomation.

the class ScriptPointValueSetter method set.

// Ensure points are settable and the setter has permissions
public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
    DataPointRT dprt = (DataPointRT) point;
    if (!dprt.getVO().getPointLocator().isSettable())
        return;
    if (permissions != null && !Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions()))
        throw new ScriptPermissionsException(new TranslatableMessage("script.set.permissionDenied", dprt.getVO().getXid()));
    setImpl(point, value, timestamp, annotation);
}
Also used : DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 15 with Permissions

use of com.serotonin.m2m2.vo.permission.Permissions in project ma-core-public by infiniteautomation.

the class DataPointDao method bulkUpdatePermissions.

/**
 * @param node
 * @param permissions
 * @param updateSetPermissions - true will update set, false will update read
 * @return
 */
public long bulkUpdatePermissions(ASTNode root, String uncleanPermissions, boolean updateSetPermissions) {
    // Anything to do?
    if (StringUtils.isEmpty(uncleanPermissions))
        return 0;
    // Trim off any ending commas
    if (uncleanPermissions.endsWith(",")) {
        uncleanPermissions = uncleanPermissions.substring(0, uncleanPermissions.length() - 1);
    }
    final String newPermissions = uncleanPermissions;
    long count;
    if (root == null) {
        // All points
        count = ejt.execute(new DataPointPermissionChangePreparedStatementCreator("SELECT dp.id,dp.readPermission,dp.setPermission FROM dataPoints AS dp FOR UPDATE; ", new ArrayList<Object>()), new DataPointPermissionChangeCallback(updateSetPermissions, newPermissions));
    } else {
        switch(Common.databaseProxy.getType()) {
            case H2:
            case DERBY:
            case MSSQL:
            case MYSQL:
            case POSTGRES:
            default:
                final SQLStatement select = new SQLStatement("SELECT dp.id,dp.readPermission,dp.setPermission FROM ", COUNT_BASE, this.joins, this.tableName, this.TABLE_PREFIX, true, false, super.getIndexes(), this.databaseType);
                root.accept(new RQLToSQLSelect<DataPointVO>(this), select);
                select.build();
                count = ejt.execute(new DataPointPermissionChangePreparedStatementCreator(select.getSelectSql() + " FOR UPDATE; ", select.getSelectArgs()), new DataPointPermissionChangeCallback(updateSetPermissions, newPermissions));
                break;
        }
    }
    return count;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) SQLStatement(com.infiniteautomation.mango.db.query.SQLStatement)

Aggregations

User (com.serotonin.m2m2.vo.User)61 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)43 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)43 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)40 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)36 ArrayList (java.util.ArrayList)27 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)20 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)17 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)16 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)15 HashMap (java.util.HashMap)15 List (java.util.List)14 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)10 ASTNode (net.jazdw.rql.parser.ASTNode)10 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)9 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)8 DataPointModel (com.serotonin.m2m2.web.mvc.rest.v1.model.DataPointModel)8 URI (java.net.URI)8 Map (java.util.Map)8 ResponseEntity (org.springframework.http.ResponseEntity)7