use of com.infiniteautomation.mango.spring.events.DaoEvent in project ma-core-public by infiniteautomation.
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);
}
});
}
}
}
Aggregations