UserProfileService extends Service { ProfileDescriptor toDescriptor(Profile profile, SecurityContext context) { final UriBuilder uriBuilder = getServiceContext().getServiceUriBuilder(); final List<Link> links = new LinkedList<>(); if (context.isUserInRole("user")) { links.add(LinksHelper.createLink(HttpMethod.GET, uriBuilder.clone() .path(getClass(), "getCurrent") .build() .toString(), null, APPLICATION_JSON, LINK_REL_GET_CURRENT_USER_PROFILE)); links.add(LinksHelper.createLink(HttpMethod.GET, uriBuilder.clone() .path(getClass(), "getById") .build(profile.getId()) .toString(), null, APPLICATION_JSON, LINK_REL_GET_USER_PROFILE_BY_ID)); links.add(LinksHelper.createLink(HttpMethod.POST, uriBuilder.clone() .path(getClass(), "updateCurrent") .build() .toString(), APPLICATION_JSON, APPLICATION_JSON, LINK_REL_UPDATE_CURRENT_USER_PROFILE)); links.add(LinksHelper.createLink(HttpMethod.POST, uriBuilder.clone() .path(getClass(), "updatePreferences") .build() .toString(), APPLICATION_JSON, APPLICATION_JSON, LINK_REL_UPDATE_PREFERENCES)); } if (context.isUserInRole("system/admin") || context.isUserInRole("system/manager")) { links.add(LinksHelper.createLink(HttpMethod.GET, uriBuilder.clone() .path(getClass(), "getById") .build(profile.getId()) .toString(), null, APPLICATION_JSON, LINK_REL_GET_USER_PROFILE_BY_ID)); } if (context.isUserInRole("system/admin")) { links.add(LinksHelper.createLink(HttpMethod.POST, uriBuilder.clone() .path(getClass(), "update") .build(profile.getId()) .toString(), APPLICATION_JSON, APPLICATION_JSON, LINK_REL_UPDATE_USER_PROFILE_BY_ID)); } return DtoFactory.getInstance().createDto(ProfileDescriptor.class) .withId(profile.getId()) .withUserId(profile.getUserId()) .withAttributes(profile.getAttributes()) .withLinks(links); } @Inject  UserProfileService(UserProfileDao profileDao, PreferenceDao preferenceDao, UserDao userDao); @ApiOperation(value = "Get user profile", notes = "Get user profile details. Roles allowed: user, temp_user", response = ProfileDescriptor.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @RolesAllowed({"user", "temp_user"}) @GenerateLink(rel = LINK_REL_GET_CURRENT_USER_PROFILE) @Produces(APPLICATION_JSON) ProfileDescriptor getCurrent(@Context SecurityContext context); @ApiOperation(value = "Get user preferences", notes = "Get user preferences, like SSH keys, recently opened project and files. It is possible " + "to use a filter, e.g. CodenvyAppState or ssh.key.public.github.com to get the last opened project " + "or a part of GitHub SSH key (if any)", response = ProfileDescriptor.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/prefs") @Produces(APPLICATION_JSON) @RolesAllowed({"user", "temp_user"}) Map<String, String> getPreferences(@ApiParam(value = "Filer")
                                                  @QueryParam("filter") String filter); @POST @RolesAllowed("user") @GenerateLink(rel = LINK_REL_UPDATE_CURRENT_USER_PROFILE) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) ProfileDescriptor updateCurrent(@Description("attributes to update") Map<String, String> updates,
                                           @Context SecurityContext context); @POST @Path("/{id}") @RolesAllowed({"system/admin"}) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) ProfileDescriptor update(@PathParam("id") String profileId,
                                    Map<String, String> updates,
                                    @Context SecurityContext context); @ApiOperation(value = "Get profile of a specific user", notes = "Get profile of a specific user. Roles allowed: system/admin, system/manager", response = ProfileDescriptor.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GET @Path("/{id}") @RolesAllowed({"user", "system/admin", "system/manager"}) @Produces(APPLICATION_JSON) ProfileDescriptor getById(@ApiParam(value = "  ID")
                                     @PathParam("id")
                                     String profileId,
                                     @Context SecurityContext context); @POST @Path("/prefs") @RolesAllowed({"user", "temp_user"}) @GenerateLink(rel = LINK_REL_UPDATE_PREFERENCES) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) Map<String, String> updatePreferences(@Required Map<String, String> update); @ApiOperation(value = "Remove attributes of a current user", notes = "Remove attributes of a current user", position = 6) @ApiResponses(value = { @ApiResponse(code = 204, message = "OK"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 409, message = "Attributes names required"), @ApiResponse(code = 500, message = "Internal Server Error")}) @DELETE @Path("/attributes") @GenerateLink(rel = LINK_REL_REMOVE_ATTRIBUTES) @RolesAllowed({"user", "temp_user"}) @Consumes(APPLICATION_JSON) void removeAttributes(@ApiParam(value = "Attributes", required = true)
                                 @Required
                                 @Description("Attributes names to remove")
                                 List<String> attrNames,
                                 @Context SecurityContext context); @ApiOperation(value = "Remove profile references of a current user", notes = "Remove profile references of a current user", position = 7) @ApiResponses(value = { @ApiResponse(code = 204, message = "OK"), @ApiResponse(code = 404, message = "Not Found"), @ApiResponse(code = 409, message = "Preferences names required"), @ApiResponse(code = 500, message = "Internal Server Error")}) @DELETE @Path("/prefs") @GenerateLink(rel = LINK_REL_REMOVE_PREFERENCES) @RolesAllowed({"user", "temp_user"}) @Consumes(APPLICATION_JSON) void removePreferences(@ApiParam(value = "Preferences to remove", required = true)
                                  @Required
                                  List<String> names);  }
@Test public void testLinksForUser() { final Profile profile = new Profile().withId(testUser.getId()); when(securityContext.isUserInRole("user")).thenReturn(true); final Set<String> expectedRels = new HashSet<>(asList(LINK_REL_GET_CURRENT_USER_PROFILE, LINK_REL_UPDATE_CURRENT_USER_PROFILE, LINK_REL_GET_USER_PROFILE_BY_ID, LINK_REL_UPDATE_PREFERENCES)); assertEquals(asRels(service.toDescriptor(profile, securityContext).getLinks()), expectedRels); }
@Test public void testLinksForSystemAdmin() { final Profile profile = new Profile().withId(testUser.getId()); when(securityContext.isUserInRole("system/admin")).thenReturn(true); final Set<String> expectedRels = new HashSet<>(asList(LINK_REL_UPDATE_USER_PROFILE_BY_ID, LINK_REL_GET_USER_PROFILE_BY_ID)); assertEquals(asRels(service.toDescriptor(profile, securityContext).getLinks()), expectedRels); }
@Test public void testLinksForSystemManager() { final Profile profile = new Profile().withId(testUser.getId()); when(securityContext.isUserInRole("system/manager")).thenReturn(true); assertEquals(asRels(service.toDescriptor(profile, securityContext).getLinks()), singleton(LINK_REL_GET_USER_PROFILE_BY_ID)); }