use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.
the class NamespaceController method getCollaboratorsByNamespace.
/**
* @param namespace
* @return all users of a given namespace, if the user acting the call has either administrative rights on the namespace, or on the repository.
*/
@RequestMapping(method = RequestMethod.GET, value = "/{namespace:.+}/users")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<Collection<Collaborator>> getCollaboratorsByNamespace(@ApiParam(value = "namespace", required = true) @PathVariable String namespace) {
Collection<Collaborator> collaborators = new HashSet<>();
try {
IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
collaborators = EntityDTOConverter.createCollaborators(userNamespaceRoleService.getRolesByUser(userContext.getUsername(), namespace));
return new ResponseEntity<>(collaborators, HttpStatus.OK);
} catch (OperationForbiddenException ofe) {
return new ResponseEntity<>(collaborators, HttpStatus.FORBIDDEN);
} catch (DoesNotExistException d) {
return new ResponseEntity<>(collaborators, HttpStatus.NOT_FOUND);
}
}
use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.
the class NamespaceController method getAllNamespacesForLoggedUser.
/**
* @return all namespaces the logged on user has access to.
*/
@RequestMapping(method = RequestMethod.GET, value = "/all")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<Collection<NamespaceDto>> getAllNamespacesForLoggedUser() {
IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
Collection<NamespaceDto> namespaces = new TreeSet<>(Comparator.comparing(NamespaceDto::getName));
try {
for (Map.Entry<Namespace, Map<User, Collection<IRole>>> entry : userNamespaceRoleService.getNamespacesCollaboratorsAndRoles(userContext.getUsername(), userContext.getUsername(), "namespace_admin").entrySet()) {
namespaces.add(EntityDTOConverter.createNamespaceDTO(entry.getKey(), entry.getValue()));
}
} catch (OperationForbiddenException ofe) {
return new ResponseEntity<>(namespaces, HttpStatus.FORBIDDEN);
} catch (DoesNotExistException d) {
return new ResponseEntity<>(namespaces, HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(namespaces, HttpStatus.OK);
}
use of org.eclipse.vorto.repository.core.IUserContext 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.core.IUserContext in project vorto by eclipse.
the class AbstractRepositoryOperation method doInElevatedSession.
public <T> T doInElevatedSession(SessionFunction<T> fn, IUserContext userContext, PrivilegeService privilegeService) {
RequestRepositorySessionHelper helper = new RequestRepositorySessionHelper(false, privilegeService);
IUserContext elevatedUserContext = getUserContextForCreatingAttachment(userContext);
try {
helper.setUser(elevatedUserContext.getAuthentication());
helper.setRepository(repositorySessionHelperSupplier.get().getRepository());
helper.setUserRoles(Stream.of(RepositoryRole.SYS_ADMIN).collect(Collectors.toSet()));
helper.setWorkspaceId(repositorySessionHelperSupplier.get().getWorkspaceId());
return fn.apply(helper.getSession());
} catch (Exception e) {
throw new FatalModelRepositoryException("Unexpected exception", e);
} finally {
helper.getSession().logout();
}
}
use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.
the class AbstractModelImporter method sortAndSaveToRepository.
private List<ModelInfo> sortAndSaveToRepository(List<ModelResource> resources, FileUpload extractedFile, Context context) {
final IUserContext user = context.getUser();
List<ModelInfo> savedModels = new ArrayList<>();
DependencyManager dm = new DependencyManager();
for (ModelResource resource : resources) {
dm.addResource(resource);
}
dm.getSorted().forEach(resource -> {
try {
IModelRepository modelRepository = modelRepoFactory.getRepositoryByModel(resource.getId());
ModelInfo importedModel = modelRepository.save((ModelResource) resource, user);
savedModels.add(importedModel);
postProcessImportedModel(importedModel, new FileContent(extractedFile.getFileName(), extractedFile.getContent()), user);
} catch (Exception e) {
logger.error("Problem importing model", e);
throw new ModelImporterException("Problem importing model", e);
}
});
return savedModels;
}
Aggregations