Search in sources :

Example 26 with IUserContext

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);
    }
}
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) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with IUserContext

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);
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) NamespaceDto(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) ResponseEntity(org.springframework.http.ResponseEntity) IRole(org.eclipse.vorto.repository.domain.IRole) TreeSet(java.util.TreeSet) Map(java.util.Map) Namespace(org.eclipse.vorto.repository.domain.Namespace) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with IUserContext

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);
    }
}
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 29 with IUserContext

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();
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ModelReferentialIntegrityException(org.eclipse.vorto.repository.core.ModelReferentialIntegrityException) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException)

Example 30 with IUserContext

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;
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) FileContent(org.eclipse.vorto.repository.core.FileContent) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ArrayList(java.util.ArrayList) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IOException(java.io.IOException) BulkUploadException(org.eclipse.vorto.repository.web.core.exceptions.BulkUploadException)

Aggregations

IUserContext (org.eclipse.vorto.repository.core.IUserContext)54 Test (org.junit.Test)32 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)28 ClassPathResource (org.springframework.core.io.ClassPathResource)18 ResponseEntity (org.springframework.http.ResponseEntity)14 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)10 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)8 ModelId (org.eclipse.vorto.model.ModelId)7 IModelRepository (org.eclipse.vorto.repository.core.IModelRepository)6 User (org.eclipse.vorto.repository.domain.User)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 PutMapping (org.springframework.web.bind.annotation.PutMapping)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 Namespace (org.eclipse.vorto.repository.domain.Namespace)4 ApiOperation (io.swagger.annotations.ApiOperation)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ModelResource (org.eclipse.vorto.repository.core.ModelResource)3 InvalidUserException (org.eclipse.vorto.repository.services.exceptions.InvalidUserException)3