use of org.eclipse.vorto.repository.notification.message.RemovedFromNamespaceMessage in project vorto by eclipse.
the class UserNamespaceRoleService method deleteAllRoles.
/**
* Deletes the {@link User} + {@link Namespace} role association for the given {@link User} and
* {@link Namespace} entirely. <br/>
* The operation is permitted in the following cases:
* <ol>
* <li>
* The acting user is sysadmin.
* </li>
* <li>
* The acting user is trying to remove themselves and is not the only administrator of the
* namespace.
* </li>
* <li>
* The acting user is trying to remove themselves and is the only administrator of the
* namespace, but they are deleting the namespace - this will fail early
* in {@link NamespaceService#deleteNamespace(User, String)} if the namespace has public
* models.
* </li>
* </ol>
* In any other case, the operation will fail and throw {@link OperationForbiddenException}.
*
* @param actor
* @param target
* @param namespace
* @param deleteNamespace
* @return
* @throws DoesNotExistException
*/
@Transactional(rollbackOn = { DoesNotExistException.class, OperationForbiddenException.class })
public boolean deleteAllRoles(User actor, User target, Namespace namespace, boolean deleteNamespace) throws DoesNotExistException, OperationForbiddenException {
// boilerplate null validation
ServiceValidationUtil.validate(actor, target, namespace);
ServiceValidationUtil.validateNulls(actor.getId(), target.getId(), namespace.getId());
// namespace does not exist
if (!namespaceRequestCache.namespace(namespace::equals).isPresent()) {
throw new DoesNotExistException("Namespace [%s] does not exist - aborting deletion of user roles.");
}
// deleting the whole namespace: forbidden
if (hasRole(actor, namespace, namespaceAdminRole()) && actor.equals(target) && !deleteNamespace) {
throw new OperationForbiddenException(String.format("Acting user with namespace administrator role cannot remove themselves from namespace [%s].", namespace.getName()));
} else // sysadmin
if (!hasRole(actor, namespace, namespaceAdminRole()) && !actor.equals(target) && isNotSysadmin(actor)) {
throw new OperationForbiddenException(String.format("Acting user cannot delete user roles for namespace [%s].", namespace.getName()));
}
Optional<UserNamespaceRoles> rolesToDelete = cache.withUser(target).getUserNamespaceRoles().stream().filter(unr -> unr.getNamespace().equals(namespace)).findAny();
// user-namespace role association does not exist
if (!rolesToDelete.isPresent()) {
LOGGER.warn("Attempting to delete non existing user namespace roles. Aborting.");
return false;
}
userNamespaceRoleRepository.delete(rolesToDelete.get().getID());
LOGGER.info("Deleted user-namespace role association.");
notificationService.sendNotificationAsync(new RemovedFromNamespaceMessage(target, namespace.getName()));
return true;
}
Aggregations