Search in sources :

Example 51 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class NamespaceController method findAllAccessibleNamespacesByPartial.

/**
 * Finds all namespaces accessible to the authenticated user, by a partial name. <br/>
 * This is used in the UI to search for namespaces the user can view, aka all the public ones and
 * the private ones the user has at least one role in.
 *
 * @param partial
 * @return
 */
@RequestMapping(method = RequestMethod.GET, value = "/search/{partial:.+}")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<Collection<NamespaceDto>> findAllAccessibleNamespacesByPartial(@ApiParam(value = "The partial name of the namespaces to be searched with", required = true) @PathVariable String partial) {
    if (Strings.nullToEmpty(partial).trim().isEmpty()) {
        return new ResponseEntity<>(Collections.emptyList(), HttpStatus.OK);
    }
    IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
    Collection<NamespaceDto> result = namespaceRepository.findNamespaceByPartial(partial.toLowerCase()).stream().filter(n -> {
        try {
            return // all public namespaces
            !n.getName().startsWith(NamespaceValidator.PRIVATE_NAMESPACE_PREFIX) || // or namespaces where user has a role
            userNamespaceRoleService.hasAnyRole(userContext.getUsername(), n.getName());
        // should never occur here
        } catch (DoesNotExistException dnee) {
            return false;
        }
    }).map(EntityDTOConverter::createNamespaceDTO).sorted(Comparator.comparing(NamespaceDto::getName)).collect(Collectors.toList());
    return new ResponseEntity<>(result, HttpStatus.OK);
}
Also used : OperationResult(org.eclipse.vorto.repository.web.api.v1.dto.OperationResult) PathVariable(org.springframework.web.bind.annotation.PathVariable) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) NamespaceValidator(org.eclipse.vorto.repository.web.api.v1.util.NamespaceValidator) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) UserNamespaceRoles(org.eclipse.vorto.repository.domain.UserNamespaceRoles) EntityDTOConverter(org.eclipse.vorto.repository.web.api.v1.util.EntityDTOConverter) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) PrivateNamespaceQuotaExceededException(org.eclipse.vorto.repository.services.exceptions.PrivateNamespaceQuotaExceededException) PutMapping(org.springframework.web.bind.annotation.PutMapping) Map(java.util.Map) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) RequestAccessToNamespaceMessage(org.eclipse.vorto.repository.notification.message.RequestAccessToNamespaceMessage) PostMapping(org.springframework.web.bind.annotation.PostMapping) Collection(java.util.Collection) UserService(org.eclipse.vorto.repository.services.UserService) Set(java.util.Set) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IRole(org.eclipse.vorto.repository.domain.IRole) User(org.eclipse.vorto.repository.domain.User) Collectors(java.util.stream.Collectors) Namespace(org.eclipse.vorto.repository.domain.Namespace) RestController(org.springframework.web.bind.annotation.RestController) IMessage(org.eclipse.vorto.repository.notification.IMessage) Optional(java.util.Optional) NameSyntaxException(org.eclipse.vorto.repository.services.exceptions.NameSyntaxException) IUserContext(org.eclipse.vorto.repository.core.IUserContext) NotificationProblem(org.eclipse.vorto.repository.notification.INotificationService.NotificationProblem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Value(org.springframework.beans.factory.annotation.Value) RequestBody(org.springframework.web.bind.annotation.RequestBody) Strings(com.google.common.base.Strings) UserNamespaceRoleRepository(org.eclipse.vorto.repository.repositories.UserNamespaceRoleRepository) INotificationService(org.eclipse.vorto.repository.notification.INotificationService) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) GetMapping(org.springframework.web.bind.annotation.GetMapping) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) CollisionException(org.eclipse.vorto.repository.services.exceptions.CollisionException) NamespaceDto(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto) UserNamespaceRoleService(org.eclipse.vorto.repository.services.UserNamespaceRoleService) NamespaceService(org.eclipse.vorto.repository.services.NamespaceService) UserUtil(org.eclipse.vorto.repository.services.UserUtil) HttpStatus(org.springframework.http.HttpStatus) NamespaceRepository(org.eclipse.vorto.repository.repositories.NamespaceRepository) NamespaceAccessRequestDTO(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceAccessRequestDTO) ResponseEntity(org.springframework.http.ResponseEntity) Comparator(java.util.Comparator) Collections(java.util.Collections) UserContext(org.eclipse.vorto.repository.core.impl.UserContext) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) NamespaceDto(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) EntityDTOConverter(org.eclipse.vorto.repository.web.api.v1.util.EntityDTOConverter) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 52 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class NamespaceController method createNamespace.

/**
 * Creates a new namespace with the given name for the authenticated user. <br/>
 * Automatically adds the user as owner and gives them all applicable roles on the namespace.<br/>
 * Subject to restrictions in terms of number of private namespaces owned, and whether the user
 * has the sufficient repository privileges to own a non-private namespace.
 *
 * @param namespace
 * @return
 */
@PutMapping(value = "/{namespace:.+}", produces = "application/json")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<OperationResult> createNamespace(@ApiParam(value = "The name of the namespace to be created", required = true) @PathVariable final String namespace) {
    try {
        IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
        namespaceService.create(userContext.getUsername(), userContext.getUsername(), namespace);
        return new ResponseEntity<>(OperationResult.success(), HttpStatus.CREATED);
    } catch (DoesNotExistException | NameSyntaxException e) {
        return new ResponseEntity<>(OperationResult.failure(e.getMessage()), HttpStatus.BAD_REQUEST);
    } catch (PrivateNamespaceQuotaExceededException pnqee) {
        return new ResponseEntity<>(OperationResult.failure(pnqee.getMessage()), HttpStatus.FORBIDDEN);
    }// omitting explicit collision message and just going with status here
     catch (CollisionException ce) {
        return new ResponseEntity<>(OperationResult.failure(""), HttpStatus.CONFLICT);
    } catch (OperationForbiddenException ofe) {
        return new ResponseEntity<>(OperationResult.failure(ofe.getMessage()), HttpStatus.FORBIDDEN);
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) OperationForbiddenException(org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException) PrivateNamespaceQuotaExceededException(org.eclipse.vorto.repository.services.exceptions.PrivateNamespaceQuotaExceededException) CollisionException(org.eclipse.vorto.repository.services.exceptions.CollisionException) NameSyntaxException(org.eclipse.vorto.repository.services.exceptions.NameSyntaxException) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 53 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class NamespaceController method addOrUpdateCollaboratorForNamespace.

/**
 * Sets the roles of the given user on the given namespace.
 *
 * @param namespace
 * @param collaborator
 * @return
 */
@PreAuthorize("isAuthenticated()")
@RequestMapping(method = RequestMethod.PUT, value = "/{namespace:.+}/users")
public ResponseEntity<Boolean> addOrUpdateCollaboratorForNamespace(@ApiParam(value = "namespace", required = true) @PathVariable String namespace, @RequestBody @ApiParam(value = "The user to be associated with the namespace", required = true) final Collaborator collaborator) {
    try {
        // no validation here save for essentials: we are pointing to an existing user
        User user = EntityDTOConverter.createUser(null, collaborator);
        IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
        return new ResponseEntity<>(userNamespaceRoleService.setRoles(userContext.getUsername(), user.getUsername(), namespace, collaborator.getRoles(), false), HttpStatus.OK);
    } catch (InvalidUserException iue) {
        return new ResponseEntity<>(false, HttpStatus.BAD_REQUEST);
    } catch (OperationForbiddenException ofe) {
        return new ResponseEntity<>(false, HttpStatus.FORBIDDEN);
    } catch (DoesNotExistException d) {
        return new ResponseEntity<>(false, 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) User(org.eclipse.vorto.repository.domain.User) InvalidUserException(org.eclipse.vorto.repository.services.exceptions.InvalidUserException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 54 with IUserContext

use of org.eclipse.vorto.repository.core.IUserContext in project vorto by eclipse.

the class AccountController method getUser.

@GetMapping("/rest/accounts/{username:.+}")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<UserDto> getUser(@ApiParam(value = "Username", required = true) @PathVariable String username) {
    IUserContext userContext = UserContext.user(SecurityContextHolder.getContext().getAuthentication());
    User user = accountService.getUser(ControllerUtils.sanitize(username));
    if (user != null) {
        // logged-on user's name
        return new ResponseEntity<>(UserDto.fromUser(user, !userContext.getUsername().equals(username)), HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}
Also used : IUserContext(org.eclipse.vorto.repository.core.IUserContext) ResponseEntity(org.springframework.http.ResponseEntity) User(org.eclipse.vorto.repository.domain.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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