Search in sources :

Example 6 with DoesNotExistException

use of org.eclipse.vorto.repository.services.exceptions.DoesNotExistException in project vorto by eclipse.

the class AccountController method deleteUserAccount.

@DeleteMapping("/rest/accounts/{username:.+}")
@PreAuthorize("hasAuthority('sysadmin') or hasPermission(#username,'user:delete')")
public ResponseEntity<Void> deleteUserAccount(@PathVariable("username") final String username) {
    try {
        IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
        userService.delete(userContext.getUsername(), username);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    } catch (OperationForbiddenException ofe) {
        return new ResponseEntity<>(HttpStatus.FORBIDDEN);
    } catch (DoesNotExistException dnee) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with DoesNotExistException

use of org.eclipse.vorto.repository.services.exceptions.DoesNotExistException in project vorto by eclipse.

the class NamespaceService method findWorkspaceIdsOfPossibleReferences.

public Set<String> findWorkspaceIdsOfPossibleReferences() {
    Set<Namespace> visibleNamespaces = new HashSet<>(cache.namespaces(NamespaceRequestCache.PUBLIC));
    IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
    if (!userContext.isAnonymous()) {
        User user = userRolesRequestCache.withUser(userContext.getUsername()).getUser();
        try {
            visibleNamespaces.addAll(userNamespaceRoleService.getNamespaces(user, user, (Long) null));
        } catch (OperationForbiddenException | DoesNotExistException e) {
            throw new IllegalStateException(e);
        }
    }
    return visibleNamespaces.stream().map(Namespace::getWorkspaceId).collect(Collectors.toSet());
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) User(org.eclipse.vorto.repository.domain.User) Namespace(org.eclipse.vorto.repository.domain.Namespace) HashSet(java.util.HashSet)

Example 8 with DoesNotExistException

use of org.eclipse.vorto.repository.services.exceptions.DoesNotExistException 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)

Example 9 with DoesNotExistException

use of org.eclipse.vorto.repository.services.exceptions.DoesNotExistException in project vorto by eclipse.

the class DefaultCommentService method createComment.

public void createComment(Comment comment) throws DoesNotExistException {
    final ModelId id = ModelId.fromPrettyFormat(comment.getModelId());
    Optional<String> workspaceId = namespaceService.resolveWorkspaceIdForNamespace(id.getNamespace());
    if (!workspaceId.isPresent()) {
        throw new DoesNotExistException(String.format("Namespace [%s] does not exist.", id.getNamespace()));
    }
    IModelRepository modelRepo = modelRepositoryFactory.getRepository(workspaceId.get());
    if (modelRepo.exists(id)) {
        comment.setAuthor(comment.getAuthor());
        comment.setModelId(id.getPrettyFormat());
        comment.setDate(new Date());
        commentRepository.save(comment);
        notifyAllCommentAuthors(comment, modelRepo.getById(id));
    } else {
        throw new ModelNotFoundException("Model not found", new PathNotFoundException());
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) PathNotFoundException(javax.jcr.PathNotFoundException) ModelId(org.eclipse.vorto.model.ModelId) Date(java.util.Date)

Example 10 with DoesNotExistException

use of org.eclipse.vorto.repository.services.exceptions.DoesNotExistException in project vorto by eclipse.

the class ModelRepositoryController method newRefactoring.

@GetMapping(value = "/refactorings/{modelId:.+}", produces = "application/json")
@PreAuthorize("hasAuthority('sysadmin') or " + "hasPermission(T(org.eclipse.vorto.model.ModelId).fromPrettyFormat(#modelId)," + "T(org.eclipse.vorto.repository.core.PolicyEntry.Permission).MODIFY)")
public ResponseEntity<Map<String, String>> newRefactoring(@PathVariable String modelId) {
    try {
        Namespace namespace = namespaceService.getByName(ModelId.fromPrettyFormat(modelId).getNamespace());
        Map<String, String> response = new HashMap<>();
        response.put("namespace", namespace.getName());
        return new ResponseEntity<>(response, HttpStatus.OK);
    } catch (DoesNotExistException doesNotExistException) {
        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Namespace(org.eclipse.vorto.repository.domain.Namespace) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)15 IUserContext (org.eclipse.vorto.repository.core.IUserContext)12 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)12 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)11 ResponseEntity (org.springframework.http.ResponseEntity)11 Namespace (org.eclipse.vorto.repository.domain.Namespace)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 User (org.eclipse.vorto.repository.domain.User)5 InvalidUserException (org.eclipse.vorto.repository.services.exceptions.InvalidUserException)5 PutMapping (org.springframework.web.bind.annotation.PutMapping)5 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 ApiParam (io.swagger.annotations.ApiParam)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 TreeSet (java.util.TreeSet)3 INotificationService (org.eclipse.vorto.repository.notification.INotificationService)3 UserNamespaceRoleRepository (org.eclipse.vorto.repository.repositories.UserNamespaceRoleRepository)3 CollisionException (org.eclipse.vorto.repository.services.exceptions.CollisionException)3