Error handling on user role assignment service
This commit is contained in:
parent
f47f9721fe
commit
24bfd69e4d
|
@ -209,7 +209,12 @@ public class UserServiceImpl implements UserService {
|
|||
if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
|
||||
|
||||
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).userIds(data.getId()).collect();
|
||||
boolean credentialsExist = !userCredentials.isEmpty();
|
||||
if (userCredentials.isEmpty())
|
||||
throw new MyApplicationException("Currently cannot update roles for this user");
|
||||
if (userCredentials.getFirst().getExternalId() == null)
|
||||
throw new MyApplicationException("Currently cannot update roles for this user");
|
||||
UUID subjectId = UUID.fromString(userCredentials.getFirst().getExternalId());
|
||||
|
||||
|
||||
List<UserRoleEntity> existingItems = this.queryFactory.query(UserRoleQuery.class).userIds(data.getId()).collect();
|
||||
List<UUID> foundIds = new ArrayList<>();
|
||||
|
@ -222,9 +227,7 @@ public class UserServiceImpl implements UserService {
|
|||
item.setRole(roleName);
|
||||
item.setCreatedAt(Instant.now());
|
||||
this.entityManager.persist(item);
|
||||
if (credentialsExist) {
|
||||
this.keycloakService.addUserToGroup(UUID.fromString(userCredentials.getFirst().getExternalId()), KeycloakRole.valueOf(roleName));
|
||||
}
|
||||
this.keycloakService.addUserToGroup(subjectId, KeycloakRole.valueOf(roleName));
|
||||
}
|
||||
foundIds.add(item.getId());
|
||||
}
|
||||
|
@ -232,9 +235,7 @@ public class UserServiceImpl implements UserService {
|
|||
this.entityManager.flush();
|
||||
|
||||
List<UserRoleEntity> toDelete = existingItems.stream().filter(x-> foundIds.stream().noneMatch(y-> y.equals(x.getId()))).collect(Collectors.toList());
|
||||
if (credentialsExist) {
|
||||
toDelete.forEach(x -> this.keycloakService.removeUserFromGroup(UUID.fromString(userCredentials.getFirst().getExternalId()), KeycloakRole.valueOf(x.getRole())));
|
||||
}
|
||||
toDelete.forEach(x -> this.keycloakService.removeUserFromGroup(subjectId, KeycloakRole.valueOf(x.getRole())));
|
||||
this.deleterFactory.deleter(UserRoleDeleter.class).deleteAndSave(toDelete);
|
||||
|
||||
this.entityManager.flush();
|
||||
|
|
Loading…
Reference in New Issue