Search in sources :

Example 1 with AddedToNamespaceMessage

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

the class UserNamespaceRoleService method setRoles.

/**
 * Sets the roles of the given {@link User} on the given {@link Namespace} with a value as a power
 * of {@literal 2}.<br/>
 * This method is private as the numeric value is not checked.<br/>
 * This can fail for a number of reasons:
 * <ul>
 *   <li>Either the actor user, target user or namespace do not exist</li>
 *   <li>
 *     Or, if not parametrized for a new namespace, if the acting user does not have the
 *     {@literal namespace_admin} role on that namespace.
 *   </li>
 * </ul>
 * <br/>
 * Notifies the target user asynchronously if possible.
 *
 * @param actor
 * @param target
 * @param namespace
 * @param rolesValue
 * @param newNamespace
 * @return {@literal true} if operation succeeded, {@literal false} if operation not required or failed to persist.
 */
private boolean setRoles(User actor, User target, Namespace namespace, long rolesValue, boolean newNamespace) throws OperationForbiddenException, DoesNotExistException {
    // boilerplate null validation
    ServiceValidationUtil.validate(actor, target, namespace);
    ServiceValidationUtil.validateNulls(actor.getId(), target.getId());
    // authorizing actor on namespace, only if the namespace is not being created for the first time
    if (!newNamespace) {
        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 already has those roles on that namespace, nothing to do and returning false
    if (roles.getRoles() == rolesValue) {
        return false;
    } else {
        // assigning given roles to user and returning true if persisting successful
        roles.setRoles(rolesValue);
        boolean result = userNamespaceRoleRepository.save(roles) != null;
        // namespace - only triggers when namespace has not just been created
        if (result && !newNamespace) {
            notificationService.sendNotificationAsync(new AddedToNamespaceMessage(target, namespace.getName(), roleUtil.toNamespaceRoles(roles.getRoles()).stream().map(IRole::getName).collect(Collectors.toList())));
        }
        return result;
    }
}
Also used : AddedToNamespaceMessage(org.eclipse.vorto.repository.notification.message.AddedToNamespaceMessage)

Example 2 with AddedToNamespaceMessage

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

the class UserNamespaceRoleService method addRole.

/**
 * Adds the given {@link IRole} to the given {@link User} on the given {@link Namespace}, as
 * acted by the {@literal actor} {@link User} if so authorized.<br/>
 * The pre-condition for authorizing this operation is that the actor is either sysadmin, or has
 * administrative privileges on the given {@link Namespace}.<br/>
 * Notifies the target user asynchronously if possible.
 *
 * @param actor
 * @param target
 * @param namespace
 * @param role
 * @return {@literal true} if the user did not have the role on the namespace prior to adding it, {@literal false} if they already had the role.
 * @throws OperationForbiddenException
 * @throws DoesNotExistException
 */
public boolean addRole(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
    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 already has that role on that namespace, nothing to do and returning false
    if ((roles.getRoles() & role.getRole()) == role.getRole()) {
        return false;
    }
    // adding 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 AddedToNamespaceMessage(target, namespace.getName(), roleUtil.toNamespaceRoles(roles.getRoles()).stream().map(IRole::getName).collect(Collectors.toList())));
    }
    return result;
}
Also used : AddedToNamespaceMessage(org.eclipse.vorto.repository.notification.message.AddedToNamespaceMessage)

Aggregations

AddedToNamespaceMessage (org.eclipse.vorto.repository.notification.message.AddedToNamespaceMessage)2