Search in sources :

Example 1 with ScriptPermissions

use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-modules-public by infiniteautomation.

the class SetPointEventHandlerModel method readInto.

@Override
public void readInto(SetPointEventHandlerVO vo) {
    super.readInto(vo);
    Integer targetId = DataPointDao.getInstance().getIdByXid(targetPointXid);
    if (targetId != null)
        vo.setTargetPointId(targetId);
    vo.setActiveAction(SetPointEventHandlerVO.SET_ACTION_CODES.getId(activeAction));
    if (activeValueToSet != null)
        vo.setActiveValueToSet(activeValueToSet.toString());
    if (activePointXid != null) {
        Integer activePointId = DataPointDao.getInstance().getIdByXid(activePointXid);
        if (activePointId != null)
            vo.setActivePointId(activePointId);
    }
    vo.setActiveScript(activeScript);
    vo.setInactiveAction(SetPointEventHandlerVO.SET_ACTION_CODES.getId(inactiveAction));
    if (inactiveValueToSet != null)
        vo.setInactiveValueToSet(inactiveValueToSet.toString());
    if (inactivePointXid != null) {
        Integer inactivePointId = DataPointDao.getInstance().getIdByXid(inactivePointXid);
        if (inactivePointId != null)
            vo.setInactivePointId(inactivePointId);
    }
    vo.setInactiveScript(inactiveScript);
    PermissionService service = Common.getBean(PermissionService.class);
    vo.setScriptRoles(new ScriptPermissions(service.explodeLegacyPermissionGroupsToRoles(scriptPermissions)));
    if (scriptContext != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (ScriptContextVariableModel var : scriptContext) {
            Integer id = DataPointDao.getInstance().getIdByXid(var.getXid());
            if (id != null) {
                additionalContext.add(new IntStringPair(id, var.getVariableName()));
            } else {
                additionalContext.add(new IntStringPair(Common.NEW_ID, var.getVariableName()));
            }
        }
        vo.setAdditionalContext(additionalContext);
    }
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) IntStringPair(com.serotonin.db.pair.IntStringPair) ScriptContextVariableModel(com.infiniteautomation.mango.rest.latest.model.javascript.MangoJavaScriptModel.ScriptContextVariableModel) ArrayList(java.util.ArrayList) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 2 with ScriptPermissions

use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by infiniteautomation.

the class ScriptPermissionConverter method jsonWrite.

@Override
public void jsonWrite(JsonWriter writer, Object value) throws IOException, JsonException {
    ScriptPermissions permission = (ScriptPermissions) value;
    JsonArray roles = new JsonArray();
    for (Role role : permission.getRoles()) {
        roles.add(role.getXid());
    }
    writer.writeObject(roles);
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) Role(com.serotonin.m2m2.vo.role.Role) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 3 with ScriptPermissions

use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by infiniteautomation.

the class ScriptPermissionConverter method jsonWrite.

@Override
public JsonValue jsonWrite(JsonTypeWriter writer, Object value) throws JsonException {
    ScriptPermissions permission = (ScriptPermissions) value;
    JsonArray roles = new JsonArray();
    for (Role role : permission.getRoles()) {
        roles.add(role.getXid());
    }
    return roles;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) Role(com.serotonin.m2m2.vo.role.Role) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Example 4 with ScriptPermissions

use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerDefinition method handleRoleEvent.

@Override
public void handleRoleEvent(SetPointEventHandlerVO vo, DaoEvent<? extends RoleVO> event) {
    // Remove and re-serialize our handler's script roles if it has a role that was deleted
    if (vo.getScriptRoles().getRoles().contains(event.getVo().getRole())) {
        switch(event.getType()) {
            case UPDATE:
                break;
            case DELETE:
                Set<Role> updated = new HashSet<>(vo.getScriptRoles().getRoles());
                updated.remove(event.getVo().getRole());
                Set<Role> allRoles = new HashSet<>();
                for (Role role : updated) {
                    allRoles.addAll(service.getAllInheritedRoles(role));
                }
                ScriptPermissions permission = new ScriptPermissions(allRoles, vo.getScriptRoles().getPermissionHolderName());
                vo.setScriptRoles(permission);
                eventHandlerService.update(vo.getId(), vo);
                break;
            default:
                break;
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) HashSet(java.util.HashSet)

Example 5 with ScriptPermissions

use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerVO method readObject.

@SuppressWarnings({ "unchecked", "deprecation" })
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    int ver = in.readInt();
    // Switch on the version of the class so that version changes can be elegantly handled.
    if (ver == 1) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = inactiveScript = null;
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
    } else if (ver == 2) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = SerializationHelper.readSafeUTF(in);
        inactiveScript = SerializationHelper.readSafeUTF(in);
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
    } else if (ver == 3) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = SerializationHelper.readSafeUTF(in);
        inactiveScript = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        com.serotonin.m2m2.rt.script.ScriptPermissions oldPermissions = (com.serotonin.m2m2.rt.script.ScriptPermissions) in.readObject();
        if (oldPermissions != null) {
            // We will be using this in the upgrade so this is temporary and will be used to set
            // the scriptRoles
            legacyScriptRoles = oldPermissions.getAllLegacyPermissions();
            legacyPermissionHolderName = oldPermissions.getPermissionHolderName();
        } else {
            scriptRoles = new ScriptPermissions();
        }
    } else if (ver == 4) {
        targetPointId = in.readInt();
        activeAction = in.readInt();
        activeValueToSet = SerializationHelper.readSafeUTF(in);
        activePointId = in.readInt();
        inactiveAction = in.readInt();
        inactiveValueToSet = SerializationHelper.readSafeUTF(in);
        inactivePointId = in.readInt();
        activeScript = SerializationHelper.readSafeUTF(in);
        inactiveScript = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
    }
}
Also used : IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions)

Aggregations

ScriptPermissions (com.infiniteautomation.mango.util.script.ScriptPermissions)24 Test (org.junit.Test)10 EmailEventHandlerVO (com.serotonin.m2m2.vo.event.EmailEventHandlerVO)8 SetPointEventHandlerVO (com.serotonin.m2m2.vo.event.SetPointEventHandlerVO)7 Role (com.serotonin.m2m2.vo.role.Role)6 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)4 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)4 IntStringPair (com.serotonin.db.pair.IntStringPair)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 JsonArray (com.serotonin.json.type.JsonArray)3 ScriptContextVariableModel (com.infiniteautomation.mango.rest.latest.model.javascript.MangoJavaScriptModel.ScriptContextVariableModel)2 EventTypeMatcher (com.serotonin.m2m2.rt.event.type.EventTypeMatcher)2 MockEventType (com.serotonin.m2m2.rt.event.type.MockEventType)2 List (java.util.List)2 Set (java.util.Set)2 EmailRecipientModel (com.infiniteautomation.mango.rest.latest.model.mailingList.EmailRecipientModel)1 MangoJavaScript (com.infiniteautomation.mango.util.script.MangoJavaScript)1 JsonObject (com.serotonin.json.type.JsonObject)1 JsonValue (com.serotonin.json.type.JsonValue)1