Search in sources :

Example 1 with DeleteAccountMessage

use of org.eclipse.vorto.repository.notification.message.DeleteAccountMessage in project vorto by eclipse.

the class UserService method delete.

/**
 * Deletes the given {@link User} and their namespace-role associations, as acted by the given
 * acting {@link User}.<br/>
 * This can fail for a number of reasons:
 * <ul>
 *   <li>
 *     The acting {@link User} does not have the {@literal sysadmin} repository role, or is not
 *     the same {@link User} as the target.
 *   </li>
 *   <li>
 *     The target {@link User} owns a {@link org.eclipse.vorto.repository.domain.Namespace} - in
 *     which case, ownership should be given to another {@link User} before deleting.
 *   </li>
 *   <li>
 *     The target {@link User} is the only one listed with namespace role {@literal namespace_admin}
 *     on one or more {@link org.eclipse.vorto.repository.domain.Namespace}s - in which case, the
 *     role should be given to at least one other {@link User} before deleting.
 *   </li>
 * </ul>
 * Failures above will throw checked exceptions. <br/>
 * It is also possible that this method will fail by returning {@code false}, should the target
 * {@link User} simply not exist.
 *
 * @param actor
 * @param target
 * @return
 */
@Transactional(rollbackFor = { OperationForbiddenException.class, DoesNotExistException.class })
public boolean delete(User actor, User target) throws OperationForbiddenException, DoesNotExistException {
    // boilerplate null validation
    ServiceValidationUtil.validateNulls(actor, target);
    if (cache.withUser(target).getUser() == null) {
        LOGGER.info("Attempting to delete a user that does not exist. ");
        return false;
    }
    // authorizing actor
    userUtil.authorizeActorAsTargetOrSysadmin(actor, target);
    // checking if only admin in any namespace
    if (userNamespaceRoleService.isOnlyAdminInAnyNamespace(actor, target)) {
        throw new OperationForbiddenException("User is the only administrator of at least one namespace - aborting delete operation.");
    }
    // retrieving namespaces target manages
    Collection<Namespace> namespacesManagedByTarget = userNamespaceRoleService.getNamespacesAndRolesByUser(actor, target).entrySet().stream().filter(e -> e.getValue().contains(userNamespaceRoleService.namespaceAdminRole())).map(Entry::getKey).collect(Collectors.toSet());
    // target owns at least one namespace - failing
    if (!namespacesManagedByTarget.isEmpty()) {
        throw new OperationForbiddenException("User is administrator in at least one namespace. Ownership must change before user can be deleted. Aborting operation.");
    }
    // collecting target user's e-mail address if any
    DeleteAccountMessage message = null;
    if (target.hasEmailAddress()) {
        message = new DeleteAccountMessage(target);
    }
    // firstly, publish the user deleted event - this way, the models are all anonymized while the
    // user and their namespace associations are still there
    eventPublisher.publishEvent(new AppEvent(this, target.getUsername(), EventType.USER_DELETED));
    // then, retrie namespaces where target has any role
    Collection<Namespace> namespacesWhereTargetHasAnyRole = userNamespaceRoleService.getNamespaces(actor, target);
    // and remove association for all namespaces
    for (Namespace namespace : namespacesWhereTargetHasAnyRole) {
        userNamespaceRoleService.deleteAllRoles(actor, target, namespace, false);
    }
    // finally, delete target user
    userRepository.delete(target);
    // and send them a message if possible
    if (message != null) {
        notificationService.sendNotification(message);
    }
    return true;
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) Namespace(org.eclipse.vorto.repository.domain.Namespace) DeleteAccountMessage(org.eclipse.vorto.repository.notification.message.DeleteAccountMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with DeleteAccountMessage

use of org.eclipse.vorto.repository.notification.message.DeleteAccountMessage in project vorto by eclipse.

the class DefaultUserAccountService method delete.

@Override
public void delete(final String userId) {
    User userToDelete = userRepository.findByUsername(userId);
    if (userToDelete != null) {
        makeModelsAnonymous(userToDelete.getUsername());
        userRepository.delete(userToDelete);
        notificationService.sendNotification(new DeleteAccountMessage(userToDelete));
    }
}
Also used : DeleteAccountMessage(org.eclipse.vorto.repository.notification.message.DeleteAccountMessage)

Example 3 with DeleteAccountMessage

use of org.eclipse.vorto.repository.notification.message.DeleteAccountMessage in project vorto by eclipse.

the class UserService method delete.

@Transactional
public void delete(final String userId) {
    User userToDelete = cache.withUser(userId).getUser();
    if (userToDelete != null) {
        eventPublisher.publishEvent(new AppEvent(this, userId, EventType.USER_DELETED));
        userRepository.delete(userToDelete);
        if (userToDelete.hasEmailAddress()) {
            notificationService.sendNotification(new DeleteAccountMessage(userToDelete));
        }
    }
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) User(org.eclipse.vorto.repository.domain.User) DeleteAccountMessage(org.eclipse.vorto.repository.notification.message.DeleteAccountMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DeleteAccountMessage (org.eclipse.vorto.repository.notification.message.DeleteAccountMessage)3 AppEvent (org.eclipse.vorto.repository.core.events.AppEvent)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Namespace (org.eclipse.vorto.repository.domain.Namespace)1 User (org.eclipse.vorto.repository.domain.User)1 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)1