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);
}
}
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());
}
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;
}
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());
}
}
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);
}
}
Aggregations