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