Search in sources :

Example 31 with ScriptPermissions

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

the class EmailEventHandlerServiceTest method testCannotUpdateUnauthorizedScriptRole.

@Test
@ExpectValidationException("scriptRoles")
public void testCannotUpdateUnauthorizedScriptRole() {
    addRoleToCreatePermission(editRole);
    EmailEventHandlerVO vo = newVO(editUser);
    vo.setReadPermission(MangoPermission.requireAnyRole(editRole));
    vo.setEditPermission(MangoPermission.requireAnyRole(editRole));
    ScriptPermissions permissions = new ScriptPermissions(Sets.newHashSet(editRole));
    vo.setScriptRoles(permissions);
    runAs.runAs(editUser, () -> {
        EmailEventHandlerVO fromDb = (EmailEventHandlerVO) service.insert(vo);
        ScriptPermissions newPermissions = new ScriptPermissions(Sets.newHashSet(readRole, editRole));
        fromDb.setScriptRoles(newPermissions);
        service.update(fromDb.getId(), fromDb);
    });
}
Also used : EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 32 with ScriptPermissions

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

the class EmailEventHandlerServiceTest method testDeleteRoleUpdateVO.

@Test
public void testDeleteRoleUpdateVO() {
    EmailEventHandlerVO vo = newVO(readUser);
    ScriptPermissions permissions = new ScriptPermissions(Sets.newHashSet(readRole, editRole));
    vo.setScriptRoles(permissions);
    service.insert(vo);
    EmailEventHandlerVO fromDb = (EmailEventHandlerVO) service.get(vo.getId());
    assertVoEqual(vo, fromDb);
    roleService.delete(editRole.getId());
    roleService.delete(readRole.getId());
    EmailEventHandlerVO updated = (EmailEventHandlerVO) service.get(fromDb.getId());
    fromDb.setScriptRoles(new ScriptPermissions(Collections.emptySet()));
    assertVoEqual(fromDb, updated);
}
Also used : EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) Test(org.junit.Test)

Example 33 with ScriptPermissions

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

the class EmailEventHandlerServiceTest method testCreatePrivilegeSuccess.

@Test
public void testCreatePrivilegeSuccess() {
    EmailEventHandlerVO vo = newVO(editUser);
    vo.setReadPermission(MangoPermission.requireAnyRole(editRole));
    vo.setEditPermission(MangoPermission.requireAnyRole(editRole));
    ScriptPermissions permissions = new ScriptPermissions(Sets.newHashSet(editRole));
    vo.setScriptRoles(permissions);
    addRoleToCreatePermission(editRole);
    runAs.runAs(editUser, () -> {
        service.insert(vo);
    });
}
Also used : EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) Test(org.junit.Test)

Example 34 with ScriptPermissions

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

the class ScriptPermissionConverter method jsonRead.

// TODO Mango 4.2 improve performance with lazy field as PermissionService is not available at construct time
@Override
public Object jsonRead(JsonReader reader, JsonValue jsonValue, Type type) throws JsonException {
    Set<Role> roles = new HashSet<>();
    PermissionService service = Common.getBean(PermissionService.class);
    if (jsonValue instanceof JsonArray) {
        for (JsonValue val : (JsonArray) jsonValue) {
            // Just a single string
            Role r = service.getRole(val.toString());
            if (r != null) {
                roles.add(r);
            } else {
                // Let the validation pick this up as a missing role, the response to the user is cleaner
                roles.add(new Role(Common.NEW_ID, val.toString()));
            }
        }
    } else if (jsonValue instanceof JsonObject) {
        // Could be the super-legacy version with 3 separate sets of roles
        JsonObject o = (JsonObject) jsonValue;
        Set<String> permissions = new HashSet<>();
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_SOURCE)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_SET)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(DATA_POINT_READ)));
        permissions.addAll(PermissionService.explodeLegacyPermissionGroups(o.getString(CUSTOM)));
        for (String role : permissions) {
            Role r = service.getRole(role);
            if (r != null) {
                roles.add(r);
            } else {
                // Let the validation pick this up as a missing role, the response to the user is cleaner
                roles.add(new Role(Common.NEW_ID, role));
            }
        }
    }
    return new ScriptPermissions(roles);
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) JsonArray(com.serotonin.json.type.JsonArray) Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) HashSet(java.util.HashSet)

Example 35 with ScriptPermissions

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

the class EmailEventHandlerVO method readObject.

@SuppressWarnings({ "unchecked", "deprecation" })
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    // NOTE: after deserialization the recipient lists need to be cleaned of deleted references,
    // See MailingListDao.cleanRecipientsList.  This happens in the DAO
    int ver = in.readInt();
    subject = SUBJECT_INCLUDE_EVENT_MESSAGE;
    if (ver == 1) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = null;
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 2) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 3) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 4) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = 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();
        }
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 5) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = 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();
        }
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 6) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 7) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
        subject = in.readInt();
    } else if (ver == 8) {
        activeRecipients = (List<MailingListRecipient>) in.readObject();
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        escalationRecipients = (List<MailingListRecipient>) in.readObject();
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        inactiveRecipients = (List<MailingListRecipient>) in.readObject();
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
        subject = in.readInt();
    }
}
Also used : IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) ArrayList(java.util.ArrayList) List(java.util.List) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Aggregations

ScriptPermissions (com.infiniteautomation.mango.util.script.ScriptPermissions)45 Test (org.junit.Test)20 EmailEventHandlerVO (com.serotonin.m2m2.vo.event.EmailEventHandlerVO)16 SetPointEventHandlerVO (com.serotonin.m2m2.vo.event.SetPointEventHandlerVO)14 Role (com.serotonin.m2m2.vo.role.Role)12 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)8 HashSet (java.util.HashSet)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6 JsonArray (com.serotonin.json.type.JsonArray)6 ArrayList (java.util.ArrayList)6 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)5 EventTypeMatcher (com.serotonin.m2m2.rt.event.type.EventTypeMatcher)4 MockEventType (com.serotonin.m2m2.rt.event.type.MockEventType)4 List (java.util.List)4 Set (java.util.Set)4 ScriptContextVariableModel (com.infiniteautomation.mango.rest.latest.model.javascript.MangoJavaScriptModel.ScriptContextVariableModel)2 JsonObject (com.serotonin.json.type.JsonObject)2 JsonValue (com.serotonin.json.type.JsonValue)2 RecipientListEntryBean (com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)2 ResultSet (java.sql.ResultSet)2