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