use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class UsersService method handleRoleEvent.
@EventListener
protected void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
if (event.getType() == DaoEventType.DELETE || event.getType() == DaoEventType.UPDATE) {
Role originalRole = event.getType() == DaoEventType.UPDATE ? event.getOriginalVo().getRole() : event.getVo().getRole();
// in the user here
if (userByUsername != null) {
userByUsername.asMap().forEach((username, user) -> {
if (user.getRoles().contains(originalRole)) {
Set<Role> updatedRoles = new HashSet<>(user.getRoles());
if (event.getType() == DaoEventType.DELETE) {
// Remove this role
updatedRoles.remove(originalRole);
} else if (event.getType() == DaoEventType.UPDATE) {
// Replace this role
updatedRoles.remove(originalRole);
updatedRoles.add(event.getVo().getRole());
}
user.setRoles(Collections.unmodifiableSet(updatedRoles));
// publish the event using the same user for originalVo, we aren't changing the XID
// so it shouldn't matter
DaoEvent<User> userUpdatedEvent = new DaoEvent<>(this.dao, UPDATE, user, user);
this.eventPublisher.publishEvent(userUpdatedEvent);
}
});
}
}
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class LazyFieldJsonTest method testLazyPermissionFromJsonObject.
@Test
public void testLazyPermissionFromJsonObject() {
RoleService roleService = Common.getBean(RoleService.class);
PermissionService permissionService = Common.getBean(PermissionService.class);
Role role1 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-1", "Role 1")).getRole();
Role role2 = roleService.insert(new RoleVO(Common.NEW_ID, "XID-2", "Role 2")).getRole();
LazyField<MangoPermission> permission = new LazyField<>(() -> MangoPermission.builder().minterm(role1, role2).build());
try (StringWriter stringWriter = new StringWriter()) {
JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
JsonTypeWriter typeWriter = new JsonTypeWriter(Common.JSON_CONTEXT);
JsonValue value = typeWriter.writeObject(permission);
writer.setPrettyIndent(0);
writer.setPrettyOutput(true);
writer.writeObject(value);
String json = stringWriter.toString();
JsonTypeReader typeReader = new JsonTypeReader(json);
JsonValue read = typeReader.read();
JsonArray root = read.toJsonArray();
JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
ImportContext context = new ImportContext(reader, new ProcessResult(), Common.getTranslations());
LazyField<MangoPermission> readPermission = new LazyField<>();
TypeDefinition lazyType = new TypeDefinition(LazyField.class, MangoPermission.class);
context.getReader().readInto(lazyType, readPermission, root);
assertEquals(permission.get(), readPermission.get());
} catch (IOException | JsonException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class UserEventMulticasterTest method setupRoles.
@Before
public void setupRoles() {
roleService = Common.getBean(RoleService.class);
systemSuperadmin = PermissionHolder.SYSTEM_SUPERADMIN;
// Add some roles
mockRole = new RoleVO(Common.NEW_ID, "MOCK", "Mock test role.");
mockRole = roleService.insert(mockRole);
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
the class DataPointService method handleRoleEvent.
@EventListener
protected void handleRoleEvent(DaoEvent<? extends RoleVO> event) {
if (event.getType() == DaoEventType.DELETE) {
Role deletedRole = event.getVo().getRole();
for (DataPointRT rt : getRuntimeManager().getRunningDataPoints()) {
DataPointVO point = rt.getVO();
point.setReadPermission(point.getReadPermission().withoutRole(deletedRole));
point.setEditPermission(point.getEditPermission().withoutRole(deletedRole));
point.setSetPermission(point.getSetPermission().withoutRole(deletedRole));
}
}
}
use of com.serotonin.m2m2.vo.role.RoleVO in project ma-core-public by MangoAutomation.
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;
}
}
}
Aggregations