Search in sources :

Example 1 with DaoEvent

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);
                }
            });
        }
    }
}
Also used : Role(com.serotonin.m2m2.vo.role.Role) DaoEvent(com.infiniteautomation.mango.spring.events.DaoEvent) User(com.serotonin.m2m2.vo.User) HashSet(java.util.HashSet) EventListener(org.springframework.context.event.EventListener)

Aggregations

DaoEvent (com.infiniteautomation.mango.spring.events.DaoEvent)1 User (com.serotonin.m2m2.vo.User)1 Role (com.serotonin.m2m2.vo.role.Role)1 HashSet (java.util.HashSet)1 EventListener (org.springframework.context.event.EventListener)1