Search in sources :

Example 1 with RemovedFromNamespaceMessage

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;
}
Also used : ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) org.eclipse.vorto.repository.domain(org.eclipse.vorto.repository.domain) java.util(java.util) Logger(org.slf4j.Logger) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) Transactional(javax.transaction.Transactional) LoggerFactory(org.slf4j.LoggerFactory) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) Autowired(org.springframework.beans.factory.annotation.Autowired) NamespaceRoleRepository(org.eclipse.vorto.repository.repositories.NamespaceRoleRepository) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RemovedFromNamespaceMessage(org.eclipse.vorto.repository.notification.message.RemovedFromNamespaceMessage) UserNamespaceRoleRepository(org.eclipse.vorto.repository.repositories.UserNamespaceRoleRepository) UserRolesRequestCache(org.eclipse.vorto.repository.core.impl.cache.UserRolesRequestCache) Service(org.springframework.stereotype.Service) RolesChangedInNamespaceMessage(org.eclipse.vorto.repository.notification.message.RolesChangedInNamespaceMessage) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) INotificationService(org.eclipse.vorto.repository.notification.INotificationService) AddedToNamespaceMessage(org.eclipse.vorto.repository.notification.message.AddedToNamespaceMessage) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) NamespaceRequestCache(org.eclipse.vorto.repository.core.impl.cache.NamespaceRequestCache) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) RemovedFromNamespaceMessage(org.eclipse.vorto.repository.notification.message.RemovedFromNamespaceMessage) Transactional(javax.transaction.Transactional)

Aggregations

Sets (com.google.common.collect.Sets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Transactional (javax.transaction.Transactional)1 NamespaceRequestCache (org.eclipse.vorto.repository.core.impl.cache.NamespaceRequestCache)1 UserRolesRequestCache (org.eclipse.vorto.repository.core.impl.cache.UserRolesRequestCache)1 org.eclipse.vorto.repository.domain (org.eclipse.vorto.repository.domain)1 INotificationService (org.eclipse.vorto.repository.notification.INotificationService)1 AddedToNamespaceMessage (org.eclipse.vorto.repository.notification.message.AddedToNamespaceMessage)1 RemovedFromNamespaceMessage (org.eclipse.vorto.repository.notification.message.RemovedFromNamespaceMessage)1 RolesChangedInNamespaceMessage (org.eclipse.vorto.repository.notification.message.RolesChangedInNamespaceMessage)1 NamespaceRoleRepository (org.eclipse.vorto.repository.repositories.NamespaceRoleRepository)1 UserNamespaceRoleRepository (org.eclipse.vorto.repository.repositories.UserNamespaceRoleRepository)1 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)1 InvalidUserException (org.eclipse.vorto.repository.services.exceptions.InvalidUserException)1 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1