Search in sources :

Example 1 with NamespaceDto

use of org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto 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 2 with NamespaceDto

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

the class NamespaceControllerIntegrationTest method testFindAllAccessibleNamespacesByPartial.

// newer tests
/**
 * Verifies that the set of namespaces retrieved by searching for all accessible namespaces by a
 * partial substring of the name for a given user returns both all public namespaces with partial
 * matches, and all private namespaces with partial matches (where the user has at least one role).<br/>
 * This is used by the "request access to namespace" form.
 *
 * @throws Exception
 */
@Test
public void testFindAllAccessibleNamespacesByPartial() throws Exception {
    // creates a public namespace as sysadmin and adds userModelCreator as collaborator
    String publicNamespaceName = "org.publicnamespace.abcd";
    createNamespaceSuccessfully(publicNamespaceName, userSysadmin);
    Collaborator collaborator = new Collaborator("userModelCreator", "GITHUB", null, Lists.newArrayList("model_viewer", "model_creator"));
    repositoryServer.perform(put(String.format("/rest/namespaces/%s/users", publicNamespaceName)).content(objectMapper.writeValueAsString(collaborator)).contentType(MediaType.APPLICATION_JSON).with(userSysadmin)).andExpect(status().isOk());
    // creates a private namespace where the searched name would match, but the userModelCreator has
    // no role
    createNamespaceSuccessfully("vorto.private.sysadmin.abcd", userSysadmin);
    // creates a private namespace for userModelCreator
    String privateNamespaceName = "vorto.private.mynamespace.abcd";
    createNamespaceSuccessfully(privateNamespaceName, userModelCreator);
    // now compares expected namespaces to appear in search with REST endpoint outcome
    // note that users and admins in returned DTOs are empty here by design
    // also note that the namespaces are sorted by name
    List<NamespaceDto> expectedNamespaces = Arrays.asList(new NamespaceDto(publicNamespaceName, Collections.emptyList(), Collections.emptyList()), new NamespaceDto(privateNamespaceName, Collections.emptyList(), Collections.emptyList()));
    repositoryServer.perform(get("/rest/namespaces/search/abcd").with(userModelCreator)).andExpect(status().isOk()).andExpect(content().json(objectMapper.writeValueAsString(expectedNamespaces)));
}
Also used : NamespaceDto(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) Test(org.junit.Test)

Example 3 with NamespaceDto

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

the class NamespaceControllerIntegrationTest method testAccessibleNamespacesWithRole.

/**
 * Verifies the list of namespaces where the logged on user has a given role is correct.
 *
 * @throws Exception
 */
@Test
public void testAccessibleNamespacesWithRole() throws Exception {
    // first, creates a namespace for the userModelCreator user
    createNamespaceSuccessfully("vorto.private.myNamespace", userModelCreator2);
    // now, creates a namespace for the userModelCreator2 user
    createNamespaceSuccessfully("vorto.private.myNamespace2", userModelCreator3);
    // Now adds userModelCreator to userModelCreator2's namespace as model creator
    Collaborator userModelCreatorCollaborator = new Collaborator();
    userModelCreatorCollaborator.setUserId(USER_MODEL_CREATOR_NAME_2);
    userModelCreatorCollaborator.setAuthenticationProviderId(GITHUB);
    userModelCreatorCollaborator.setSubject("none");
    Set<String> roles = new HashSet<>();
    roles.add("model_viewer");
    roles.add("model_creator");
    userModelCreatorCollaborator.setRoles(roles);
    repositoryServer.perform(put("/rest/namespaces/vorto.private.myNamespace2/users").contentType("application/json").content(objectMapper.writeValueAsString(userModelCreatorCollaborator)).with(userSysadmin)).andExpect(status().isOk()).andExpect(content().string("true"));
    // finally, lists all namespaces where userModelCreator has the model_creator role, that is, their own
    // and userModelCreator2's namespace
    // expected namespaces
    Collection<NamespaceDto> expectedNamespaces = new ArrayList<>();
    // admins and users of the userModelCreator's namespace
    Collection<Collaborator> userModelCreatorNSAdmins = new ArrayList<>();
    Collection<Collaborator> userModelCreatorNSUsers = new ArrayList<>();
    /*
    Creating set of namespace owner roles
    */
    Set<String> ownerRoles = new HashSet<>();
    ownerRoles.add("model_viewer");
    ownerRoles.add("model_creator");
    ownerRoles.add("namespace_admin");
    ownerRoles.add("model_promoter");
    ownerRoles.add("model_reviewer");
    ownerRoles.add("model_publisher");
    // creating Collaborator for userModelCreator as admin in their own namespace
    Collaborator userModelCreatorCollaboratorAsAdmin = new Collaborator();
    userModelCreatorCollaboratorAsAdmin.setUserId(USER_MODEL_CREATOR_NAME_2);
    userModelCreatorCollaboratorAsAdmin.setAuthenticationProviderId(GITHUB);
    userModelCreatorCollaboratorAsAdmin.setSubject("none");
    userModelCreatorCollaboratorAsAdmin.setRoles(ownerRoles);
    userModelCreatorNSAdmins.add(userModelCreatorCollaboratorAsAdmin);
    // creating Collaborator for userModelCreator as user in their own namespace
    Collaborator userModelCreatorCollaboratorAsUserSysadmin = new Collaborator();
    userModelCreatorCollaboratorAsUserSysadmin.setUserId(USER_MODEL_CREATOR_NAME_2);
    userModelCreatorCollaboratorAsUserSysadmin.setAuthenticationProviderId(GITHUB);
    userModelCreatorCollaboratorAsUserSysadmin.setSubject("none");
    userModelCreatorCollaboratorAsUserSysadmin.setRoles(ownerRoles);
    userModelCreatorNSUsers.add(userModelCreatorCollaboratorAsUserSysadmin);
    // creating namespace for userModelCreator
    NamespaceDto userModelCreatorNS = new NamespaceDto("vorto.private.myNamespace", userModelCreatorNSUsers, userModelCreatorNSAdmins);
    // creating userModelCreator2 as a Collaborator object
    Collaborator userModelCreator2CollaboratorAsAdmin = new Collaborator();
    userModelCreator2CollaboratorAsAdmin.setUserId(USER_MODEL_CREATOR_NAME_3);
    userModelCreator2CollaboratorAsAdmin.setAuthenticationProviderId(GITHUB);
    userModelCreator2CollaboratorAsAdmin.setSubject("none");
    userModelCreator2CollaboratorAsAdmin.setRoles(ownerRoles);
    Collaborator userModelCreator2CollaboratorAsUserSysadmin = new Collaborator();
    userModelCreator2CollaboratorAsUserSysadmin.setUserId(USER_MODEL_CREATOR_NAME_3);
    userModelCreator2CollaboratorAsUserSysadmin.setAuthenticationProviderId(GITHUB);
    userModelCreator2CollaboratorAsUserSysadmin.setSubject("none");
    userModelCreator2CollaboratorAsUserSysadmin.setRoles(ownerRoles);
    // adding to userModelCreator2 namespace admins
    Collection<Collaborator> userModelCreator2NSAdmins = new ArrayList<>();
    userModelCreator2NSAdmins.add(userModelCreator2CollaboratorAsAdmin);
    // adding both userModelCreator2 collaborator and userModelCreator (the non-admin collaborator from up above)
    // to the userModelCreator2 namespace users
    Collection<Collaborator> userModelCreator2NSUsers = new ArrayList<>();
    userModelCreator2NSUsers.add(userModelCreator2CollaboratorAsUserSysadmin);
    userModelCreator2NSUsers.add(userModelCreatorCollaborator);
    // creating ns for userModelCreator2
    NamespaceDto userModelCreator2NS = new NamespaceDto("vorto.private.myNamespace2", userModelCreator2NSUsers, userModelCreator2NSAdmins);
    // adding both ns to expected collection
    expectedNamespaces.add(userModelCreatorNS);
    expectedNamespaces.add(userModelCreator2NS);
    repositoryServer.perform(get("/rest/namespaces/role/model_creator").with(userModelCreator2)).andExpect(status().isOk()).andExpect(content().json(objectMapper.writeValueAsString(expectedNamespaces)));
}
Also used : NamespaceDto(org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with NamespaceDto

use of org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto 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)

Aggregations

NamespaceDto (org.eclipse.vorto.repository.web.api.v1.dto.NamespaceDto)4 Collaborator (org.eclipse.vorto.repository.web.api.v1.dto.Collaborator)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 IUserContext (org.eclipse.vorto.repository.core.IUserContext)2 IRole (org.eclipse.vorto.repository.domain.IRole)2 Namespace (org.eclipse.vorto.repository.domain.Namespace)2 DoesNotExistException (org.eclipse.vorto.repository.services.exceptions.DoesNotExistException)2 OperationForbiddenException (org.eclipse.vorto.repository.services.exceptions.OperationForbiddenException)2 Strings (com.google.common.base.Strings)1 ApiParam (io.swagger.annotations.ApiParam)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 UserContext (org.eclipse.vorto.repository.core.impl.UserContext)1