Search in sources :

Example 86 with Permissions

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

the class SetPointEventHandlerVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    DataPointDao dataPointDao = DataPointDao.instance;
    String xid = jsonObject.getString("targetPointId");
    if (xid != null) {
        Integer id = dataPointDao.getIdByXid(xid);
        if (id == null)
            throw new TranslatableJsonException("emport.error.missingPoint", xid);
        targetPointId = id;
    }
    // Active
    String text = jsonObject.getString("activeAction");
    if (text != null) {
        activeAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(activeAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "activeAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (activeAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("activePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            activePointId = id;
        }
    } else if (activeAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("activeValueToSet");
        if (text != null)
            activeValueToSet = text;
    } else if (activeAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("activeScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        activeValueToSet = text;
    }
    // Inactive
    text = jsonObject.getString("inactiveAction");
    if (text != null) {
        inactiveAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(inactiveAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("inactivePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            inactivePointId = id;
        }
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("inactiveValueToSet");
        if (text != null)
            inactiveValueToSet = text;
    } else if (inactiveAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("inactiveScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        inactiveValueToSet = text;
    }
    JsonArray context = jsonObject.getJsonArray("additionalContext");
    if (context != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (JsonValue jv : context) {
            JsonObject jo = jv.toJsonObject();
            String dataPointXid = jo.getString("dataPointXid");
            if (dataPointXid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            Integer id = DataPointDao.instance.getIdByXid(dataPointXid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", dataPointXid);
            String contextKey = jo.getString("contextKey");
            if (contextKey == null)
                throw new TranslatableJsonException("emport.error.context.missing", "contextKey");
            additionalContext.add(new IntStringPair(id, contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    JsonObject permissions = jsonObject.getJsonObject("scriptPermissions");
    ScriptPermissions scriptPermissions = new ScriptPermissions();
    if (permissions != null) {
        String perm = permissions.getString(ScriptPermissions.DATA_SOURCE);
        if (perm != null)
            scriptPermissions.setDataSourcePermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_READ);
        if (perm != null)
            scriptPermissions.setDataPointReadPermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_SET);
        if (perm != null)
            scriptPermissions.setDataPointSetPermissions(perm);
    }
    this.scriptPermissions = scriptPermissions;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions)

Example 87 with Permissions

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

the class SetPointEventHandlerVO method jsonWrite.

@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    String dpXid = DataPointDao.instance.getXidById(targetPointId);
    writer.writeEntry("targetPointId", dpXid);
    // Active
    writer.writeEntry("activeAction", SET_ACTION_CODES.getCode(activeAction));
    if (activeAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(activePointId);
        writer.writeEntry("activePointId", dpXid);
    } else if (activeAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("activeValueToSet", activeValueToSet);
    else if (activeAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("activeScript", activeScript);
    // Inactive
    writer.writeEntry("inactiveAction", SET_ACTION_CODES.getCode(inactiveAction));
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(inactivePointId);
        writer.writeEntry("inactivePointId", dpXid);
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("inactiveValueToSet", inactiveValueToSet);
    else if (inactiveAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("inactiveScript", inactiveScript);
    JsonArray context = new JsonArray();
    for (IntStringPair pnt : additionalContext) {
        DataPointVO dpvo = DataPointDao.instance.getDataPoint(pnt.getKey(), false);
        if (dpvo != null) {
            JsonObject point = new JsonObject();
            point.put("dataPointXid", dpvo.getXid());
            point.put("contextKey", pnt.getValue());
            context.add(point);
        }
    }
    writer.writeEntry("additionalContext", context);
    if (scriptPermissions != null) {
        JsonObject permissions = new JsonObject();
        permissions.put(ScriptPermissions.DATA_SOURCE, scriptPermissions.getDataSourcePermissions());
        permissions.put(ScriptPermissions.DATA_POINT_READ, scriptPermissions.getDataPointReadPermissions());
        permissions.put(ScriptPermissions.DATA_POINT_SET, scriptPermissions.getDataPointSetPermissions());
        writer.writeEntry("scriptPermissions", permissions);
    } else {
        writer.writeEntry("scriptPermissions", null);
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonObject(com.serotonin.json.type.JsonObject)

Example 88 with Permissions

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

the class RealTimeDataPointValueCache method getUserView.

/**
 * Get all the points a user can see based on permissions
 * @param user
 * @return
 */
public List<RealTimeDataPointValue> getUserView(User user) {
    if (cleared) {
        // Reload
        PointHierarchy ph = createPointHierarchy(Common.getTranslations());
        PointFolder root = ph.getRoot();
        // Fill the cache now
        fillCache(root, realTimeData);
        cleared = false;
    }
    List<RealTimeDataPointValue> results = new ArrayList<RealTimeDataPointValue>();
    final boolean admin = Permissions.hasAdmin(user);
    final String permissions = user.getPermissions();
    for (RealTimeDataPointValue rtdpv : this.realTimeData) {
        // Do we have set or read permissions for this point?
        if (admin || Permissions.hasPermission(rtdpv.getSetPermission(), permissions) || Permissions.hasPermission(rtdpv.getReadPermission(), permissions))
            results.add(rtdpv);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) PointHierarchy(com.serotonin.m2m2.vo.hierarchy.PointHierarchy) PointFolder(com.serotonin.m2m2.vo.hierarchy.PointFolder)

Example 89 with Permissions

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

the class SystemSettingsDao method getAllSystemSettingsAsCodes.

/**
 * Return all settings (if no setting is saved return default value) whilst converting to Export Codes where necessary
 *
 * @return
 */
public Map<String, Object> getAllSystemSettingsAsCodes() {
    Map<String, Object> settings = new HashMap<String, Object>(DEFAULT_VALUES.size());
    // Add The Permissions with empty values since they don't necessarily have defaults
    for (PermissionDefinition def : ModuleRegistry.getDefinitions(PermissionDefinition.class)) {
        settings.put(def.getPermissionTypeName(), "");
    }
    // Start with all the defaults
    Iterator<String> it = DEFAULT_VALUES.keySet().iterator();
    String key;
    while (it.hasNext()) {
        key = it.next();
        if (!key.toLowerCase().contains("password") && !key.startsWith(DATABASE_SCHEMA_VERSION))
            settings.put(key, DEFAULT_VALUES.get(key));
    }
    // Then replace anything with what is stored in the database
    ejt.query("select settingName,settingValue from systemSettings", new RowCallbackHandler() {

        public void processRow(ResultSet rs) throws SQLException {
            String settingName = rs.getString(1);
            // Don't export any passwords or schema numbers
            if ((!settingName.toLowerCase().contains("password") && !settingName.startsWith(DATABASE_SCHEMA_VERSION))) {
                String settingValue = rs.getString(2);
                if (settingValue != null) {
                    // Convert Numbers to Integers
                    try {
                        settings.put(settingName, Integer.parseInt(settingValue));
                    } catch (NumberFormatException e) {
                        // Are we a boolean
                        if (settingValue.equalsIgnoreCase("y")) {
                            settings.put(settingName, new Boolean(true));
                        } else if (settingValue.equalsIgnoreCase("n")) {
                            settings.put(settingName, new Boolean(false));
                        } else {
                            // Must be a string
                            settings.put(settingName, settingValue);
                        }
                    }
                } else {
                    // and also so that there is an indication that a null setting exists
                    if (settings.get(settingName) == null)
                        settings.put(settingName, null);
                }
            }
        }
    });
    // Convert the Integers to Codes
    it = settings.keySet().iterator();
    while (it.hasNext()) {
        key = it.next();
        Object value = settings.get(key);
        if (value instanceof Integer) {
            String code = convertToCodeFromValue(key, (Integer) value);
            if (code != null)
                settings.put(key, code);
        }
    }
    return settings;
}
Also used : SuperadminPermissionDefinition(com.serotonin.m2m2.module.definitions.permissions.SuperadminPermissionDefinition) PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Example 90 with Permissions

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

the class MangoCustomMethodSecurityExpressionRoot method hasPermissionType.

/**
 * Does the user have any of the given permissions assigned to the type
 * @param permissionType
 * @return
 */
public boolean hasPermissionType(String permissionType) {
    User user = (User) this.getPrincipal();
    if (user.isAdmin())
        return true;
    Set<String> userPermissions = Permissions.explodePermissionGroups(user.getPermissions());
    for (PermissionDefinition def : ModuleRegistry.getDefinitions(PermissionDefinition.class)) {
        String groups = SystemSettingsDao.getValue(def.getPermissionTypeName());
        Set<String> permissions = Permissions.explodePermissionGroups(groups);
        // TODO Use Collections.disjoint?
        for (String permission : permissions) {
            if (userPermissions.contains(permission))
                return true;
        }
    }
    return false;
}
Also used : PermissionDefinition(com.serotonin.m2m2.module.PermissionDefinition) User(com.serotonin.m2m2.vo.User)

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