Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/src/main/java/org/openmrs/api/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,17 @@ public void purgeRole(Role role) throws APIException {
if (role.hasChildRoles()) {
throw new CannotDeleteRoleWithChildrenException();
}

// Manually remove this child from all parents to update the cache
if (role.getInheritedRoles() != null) {

for (Role parent : new HashSet<>(role.getInheritedRoles())) {
if (parent.getChildRoles() != null && parent.getChildRoles().contains(role)) {
parent.getChildRoles().remove(role);
dao.saveRole(parent); // updating the cache
}
}
}

dao.deleteRole(role);
}
Expand Down
34 changes: 34 additions & 0 deletions api/src/test/java/org/openmrs/api/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1750,4 +1750,38 @@ private User createTestUser() {
u.getPerson().setGender("M");
return userService.createUser(u, "Openmr5xy");
}

/**
* @see UserService#purgeRole(Role)
* @verifies update parent role cache when child role is deleted
*/
@Test
public void purgeRole_shouldUpdateParentRoleCacheWhenChildRoleIsDeleted() {

Role parent = new Role("Parent Role", "Parent Description");
userService.saveRole(parent);

Context.flushSession();
Context.clearSession();

Role parentRef = userService.getRole("Parent Role");

Role child = new Role("Child Role", "Child Description");
if (child.getInheritedRoles() == null) {
child.setInheritedRoles(new HashSet<>());
}
child.getInheritedRoles().add(parentRef);
userService.saveRole(child);

Context.flushSession();
Context.clearSession();

Role parentToCache = userService.getRole("Parent Role");
assertEquals(1, parentToCache.getChildRoles().size());

Role childToDelete = userService.getRole("Child Role");
userService.purgeRole(childToDelete);

assertEquals(0, parentToCache.getChildRoles().size());
}
}
Loading