Search in sources :

Example 1 with RolesChangedInNamespaceMessage

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

the class UserNamespaceRoleService method removeRole.

/**
 * Removes the given {@link IRole} from the given {@link User} on the given {@link Namespace}.<br/>
 * Will return {@literal true} if the user and namespace association exists and the user had that
 * role on the namespace.<br/>
 * Will return {@literal false} for any other reason, such as: no user-namespace association
 * exists, the user did not have that role on the namespace to start with, other failures at
 * repository-level...<br/>
 * Notifies the target user asynchronously if possible.
 *
 * @param actor
 * @param target
 * @param namespace
 * @param role
 * @return {@literal true} if the operation succeeded, {@literal false} otherwise or if not applicable.
 */
public boolean removeRole(User actor, User target, Namespace namespace, IRole role) throws OperationForbiddenException, DoesNotExistException {
    // boilerplate null validation
    ServiceValidationUtil.validate(actor, target, namespace, role);
    ServiceValidationUtil.validateNulls(actor.getId(), target.getId());
    // authorizing actor on namespace
    authorizeActorAsAdminOrOwnerOnNamespace(actor, namespace);
    UserNamespaceRoles roles = cache.withUser(target).getUserNamespaceRoles().stream().filter(unr -> unr.getNamespace().equals(namespace)).findAny().orElseGet(() -> {
        UserNamespaceRoles result = new UserNamespaceRoles();
        result.setID(new UserNamespaceID(target, namespace));
        return result;
    });
    // user does not have that role on that namespace, nothing to do and returning false
    if ((roles.getRoles() & role.getRole()) != role.getRole()) {
        return false;
    } else {
        // removing given role to user roles and returning true if persisting successful
        roles.setRoles(roles.getRoles() - role.getRole());
        boolean result = userNamespaceRoleRepository.save(roles) != null;
        if (result) {
            notificationService.sendNotificationAsync(new RolesChangedInNamespaceMessage(target, namespace.getName(), roleUtil.toNamespaceRoles(roles.getRoles()).stream().map(IRole::getName).collect(Collectors.toList())));
        }
        return result;
    }
}
Also used : RolesChangedInNamespaceMessage(org.eclipse.vorto.repository.notification.message.RolesChangedInNamespaceMessage)

Aggregations

RolesChangedInNamespaceMessage (org.eclipse.vorto.repository.notification.message.RolesChangedInNamespaceMessage)1