Search in sources :

Example 1 with NamespaceAccessRequestDTO

use of org.eclipse.vorto.repository.web.api.v1.dto.NamespaceAccessRequestDTO in project vorto by eclipse.

the class NamespaceController method requestAccessToNamespace.

@PostMapping("/requestAccess")
@PreAuthorize("isAuthenticated()")
public ResponseEntity<OperationResult> requestAccessToNamespace(@RequestBody @ApiParam(value = "The request body specifying who initiates the request, the namespace, whom the request is intended for, and an optional collection of suggested roles", required = true) NamespaceAccessRequestDTO request) {
    Optional<OperationResult> validationError = NamespaceValidator.validateAccessRequest(request);
    if (validationError.isPresent()) {
        return new ResponseEntity<>(validationError.get(), HttpStatus.BAD_REQUEST);
    }
    // checks namespace exists
    // should only occur if namespace was deleted after user search, but before sending request
    Namespace target;
    try {
        target = namespaceService.getByName(request.getNamespaceName());
    } catch (DoesNotExistException dnee) {
        return new ResponseEntity<>(OperationResult.failure("Namespace not found."), HttpStatus.NOT_FOUND);
    }
    // checks any admin with an e-mail address set
    Set<User> adminsWithEmail = userNamespaceRoleRepository.findAllByNamespace(target).stream().map(UserNamespaceRoles::getUser).filter(u -> !Strings.nullToEmpty(u.getEmailAddress()).trim().isEmpty()).collect(Collectors.toSet());
    if (adminsWithEmail.isEmpty()) {
        return new ResponseEntity<>(OperationResult.failure(String.format("None of the users administrating namespace %s has set their own e-mail. Please contact them directly. ", request.getNamespaceName())), HttpStatus.PRECONDITION_FAILED);
    }
    int successCount = adminsWithEmail.size();
    // attempts to send the e-mails
    // ugly exception handling here, due to the way this was designed in the service
    Collection<IMessage> messages = adminsWithEmail.stream().map(u -> new RequestAccessToNamespaceMessage(request, u, host)).collect(Collectors.toList());
    for (IMessage message : messages) {
        try {
            emailNotificationService.sendNotification(message);
        } catch (NotificationProblem np) {
            successCount--;
        }
    }
    // worked for all recipients
    if (successCount == adminsWithEmail.size()) {
        return new ResponseEntity<>(OperationResult.success(), HttpStatus.OK);
    } else // worked for some recipients
    if (successCount > 0) {
        return new ResponseEntity<>(OperationResult.success("The message could not be sent to all administrators."), HttpStatus.OK);
    } else // did not work for any recipient
    {
        return new ResponseEntity<>(OperationResult.failure("The message could not be sent to any administrator."), HttpStatus.SERVICE_UNAVAILABLE);
    }
}
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) DoesNotExistException(org.eclipse.vorto.repository.services.exceptions.DoesNotExistException) User(org.eclipse.vorto.repository.domain.User) IMessage(org.eclipse.vorto.repository.notification.IMessage) OperationResult(org.eclipse.vorto.repository.web.api.v1.dto.OperationResult) RequestAccessToNamespaceMessage(org.eclipse.vorto.repository.notification.message.RequestAccessToNamespaceMessage) Namespace(org.eclipse.vorto.repository.domain.Namespace) ResponseEntity(org.springframework.http.ResponseEntity) UserNamespaceRoles(org.eclipse.vorto.repository.domain.UserNamespaceRoles) NotificationProblem(org.eclipse.vorto.repository.notification.INotificationService.NotificationProblem) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

Strings (com.google.common.base.Strings)1 ApiParam (io.swagger.annotations.ApiParam)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 IUserContext (org.eclipse.vorto.repository.core.IUserContext)1 UserContext (org.eclipse.vorto.repository.core.impl.UserContext)1 IRole (org.eclipse.vorto.repository.domain.IRole)1 Namespace (org.eclipse.vorto.repository.domain.Namespace)1 User (org.eclipse.vorto.repository.domain.User)1 UserNamespaceRoles (org.eclipse.vorto.repository.domain.UserNamespaceRoles)1 IMessage (org.eclipse.vorto.repository.notification.IMessage)1 INotificationService (org.eclipse.vorto.repository.notification.INotificationService)1 NotificationProblem (org.eclipse.vorto.repository.notification.INotificationService.NotificationProblem)1