Improve control when owner try to be changed.

This commit is contained in:
Paulo Gustavo Veiga 2022-04-15 00:21:38 -03:00
parent 51cfa961e6
commit d831c9d81f

View File

@ -324,42 +324,42 @@ public class MindmapController extends BaseController {
} }
// Has any role changed ?. Just removed it. // Has any role changed ?. Just removed it.
final Map<String, Collaboration> mapsByEmail = mindMap final Map<String, Collaboration> collabByEmail = mindMap
.getCollaborations() .getCollaborations()
.stream() .stream()
.collect(Collectors.toMap(collaboration -> collaboration.getCollaborator().getEmail(), collaboration -> collaboration)); .collect(Collectors.toMap(collaboration -> collaboration.getCollaborator().getEmail(), collaboration -> collaboration));
restCollabs
.getCollaborations()
.forEach(collab -> {
final String email = collab.getEmail();
if (mapsByEmail.containsKey(email)) {
try {
mindmapService.removeCollaboration(mindMap, mapsByEmail.get(email));
} catch (CollaborationException e) {
logger.error(e);
}
}
});
// Great, let's add all the collabs again ... // Great, let's add all the collabs again ...
for (RestCollaboration restCollab : restCollabs.getCollaborations()) { for (RestCollaboration restCollab : restCollabs.getCollaborations()) {
// Validate role format ... // Validate newRole format ...
String roleStr = restCollab.getRole(); final String roleStr = restCollab.getRole();
if (roleStr == null) { if (roleStr == null) {
throw new IllegalArgumentException(roleStr + " is not a valid role"); throw new IllegalArgumentException(roleStr + " is not a valid newRole");
} }
// Is owner ? // Had the newRole changed ?. Otherwise, don't touch it.
final String email = restCollab.getEmail(); final CollaborationRole newRole = CollaborationRole.valueOf(roleStr.toUpperCase());
final Collaboration collaboration = mindMap.findCollaboration(email); final String collabEmail = restCollab.getEmail();
if (collaboration != null && collaboration.getRole() == CollaborationRole.OWNER) { final Collaboration currentCollab = collabByEmail.get(collabEmail);
throw new CollabChangeException(email); if (currentCollab == null || currentCollab.getRole() != newRole) {
// Are we trying to change the owner ...
if (currentCollab != null && currentCollab.getRole() == CollaborationRole.OWNER) {
throw new CollabChangeException(collabEmail);
} }
final CollaborationRole role = CollaborationRole.valueOf(roleStr.toUpperCase()); // Role can not be changed ...
mindmapService.addCollaboration(mindMap, email, role, restCollabs.getMessage()); if (newRole == CollaborationRole.OWNER) {
throw new CollabChangeException(collabEmail);
}
// This is collaboration that with different newRole, try to change it ...
if (currentCollab != null) {
mindmapService.removeCollaboration(mindMap, currentCollab);
}
mindmapService.addCollaboration(mindMap, collabEmail, newRole, restCollabs.getMessage());
}
} }
} }