use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by MangoAutomation.
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);
}
use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by MangoAutomation.
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);
});
}
use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by MangoAutomation.
the class Upgrade29 method upgradeEventHandlers.
private void upgradeEventHandlers(OutputStream out) {
// Add permission id columns
Map<String, String[]> scripts = new HashMap<>();
scripts.put(DatabaseType.MYSQL.name(), eventHandlersPermissionMySQL);
scripts.put(DatabaseType.H2.name(), eventHandlersPermissionH2);
scripts.put(DatabaseType.MSSQL.name(), eventHandlersPermissionMSSQL);
scripts.put(DatabaseType.POSTGRES.name(), eventHandlersPermissionMySQL);
runScript(scripts, out);
// set permission to superadmin
Integer readId = getOrCreatePermission(MangoPermission.superadminOnly()).getId();
Integer editId = getOrCreatePermission(MangoPermission.superadminOnly()).getId();
ejt.update("UPDATE eventHandlers SET readPermissionId=?, editPermissionId=?", readId, editId);
// Restrict to NOT NULL
scripts = new HashMap<>();
scripts.put(DatabaseType.MYSQL.name(), eventHandlersPermissionNotNullMySQL);
scripts.put(DatabaseType.H2.name(), eventHandlersPermissionNotNull);
scripts.put(DatabaseType.MSSQL.name(), eventHandlersPermissionNotNull);
scripts.put(DatabaseType.POSTGRES.name(), eventHandlersPermissionNotNull);
runScript(scripts, out);
// Upgrade Email Event Handlers to fix the script permission serialization, we don't need to clean the
// recipient list here as we aren't going to use it.
this.ejt.query("SELECT eh.id, eh.data FROM eventHandlers eh WHERE eh.eventHandlerType=?", new Object[] { EmailEventHandlerDefinition.TYPE_NAME }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
int id = rs.getInt(1);
EmailEventHandlerVO vo = (EmailEventHandlerVO) SerializationHelper.readObjectInContext(rs.getBinaryStream(2));
Set<String> legacyScriptRoles = vo.getLegacyScriptRoles();
if (legacyScriptRoles != null) {
Set<Role> roles = new HashSet<>();
for (String r : legacyScriptRoles) {
roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
}
vo.setScriptRoles(new ScriptPermissions(roles, vo.getLegacyPermissionHolderName()));
} else {
// Must be a ScriptPermission that might need to be upgraded
ScriptPermissions permission = vo.getScriptRoles();
if (permission.getLegacyScriptRoles() != null) {
Set<Role> roles = new HashSet<>();
for (String r : permission.getLegacyScriptRoles()) {
roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
}
ScriptPermissions upgraded = new ScriptPermissions(roles, permission.getPermissionHolderName());
vo.setScriptRoles(upgraded);
}
}
ejt.update("UPDATE eventHandlers SET data=? where id=?", SerializationHelper.writeObjectToArray(vo), id);
}
});
// Upgrade Set Point Event Handlers
this.ejt.query("SELECT eh.id, eh.data FROM eventHandlers eh WHERE eh.eventHandlerType=?", new Object[] { SetPointEventHandlerDefinition.TYPE_NAME }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
int id = rs.getInt(1);
SetPointEventHandlerVO vo = (SetPointEventHandlerVO) SerializationHelper.readObjectInContext(rs.getBinaryStream(2));
Set<String> legacyScriptRoles = vo.getLegacyScriptRoles();
if (legacyScriptRoles != null) {
Set<Role> roles = new HashSet<>();
for (String r : legacyScriptRoles) {
roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
}
vo.setScriptRoles(new ScriptPermissions(roles, vo.getLegacyPermissionHolderName()));
} else {
// Must be a ScriptPermission that might need to be upgraded
ScriptPermissions permission = vo.getScriptRoles();
if (permission.getLegacyScriptRoles() != null) {
Set<Role> roles = new HashSet<>();
for (String r : permission.getLegacyScriptRoles()) {
roles.add(getOrCreateRole(new Role(Common.NEW_ID, r)));
}
ScriptPermissions upgraded = new ScriptPermissions(roles, permission.getPermissionHolderName());
vo.setScriptRoles(upgraded);
}
}
ejt.update("UPDATE eventHandlers SET data=? where id=?", SerializationHelper.writeObjectToArray(vo), id);
}
});
}
use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by MangoAutomation.
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);
}
use of com.infiniteautomation.mango.util.script.ScriptPermissions in project ma-core-public by MangoAutomation.
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;
}
Aggregations