From f748b6e2a9bca9a2ab0b8cbf9bad5202434262ef Mon Sep 17 00:00:00 2001 From: Konstantinos Triantafyllou Date: Thu, 4 Mar 2021 14:45:06 +0000 Subject: [PATCH] [User | Trunk]: Add methods to get ids of managers and members for an entity. --- .../usermanagement/api/RegistryService.java | 34 ++++++++++++++ .../usermanagement/utils/RegistryCalls.java | 46 ++++++++++++++++--- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java b/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java index 16d7e00..bbf6019 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java @@ -492,6 +492,24 @@ public class RegistryService { } } + /** + * Get the Identifiers of the members of a type(Community, etc.) with id(ee, egi, etc.) + */ + @Path("/{type}/{id}/members/id") + @GET + @Produces(MediaType.APPLICATION_JSON) + @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," + + "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") + public Response getMembersId(@PathParam("type") String type, @PathParam("id") String id) { + Integer couId = calls.getCouId(type, id, false); + if(couId != null) { + JsonArray members = calls.getUserIdByCouId(couId, false); + return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build(); + } else { + return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); + } + } + /** * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.) */ @@ -538,4 +556,20 @@ public class RegistryService { return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); } } + + /** + * Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.) + */ + @Path("/{type}/{id}/managers/id") + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getManagersId(@PathParam("type") String type, @PathParam("id") String id) { + Integer couId = calls.getCouId(type, id); + if(couId != null) { + JsonArray managers = calls.getUserIdByCouId(couId, true); + return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build(); + } else { + return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); + } + } } diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java index 338be62..5158594 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java @@ -284,7 +284,28 @@ public class RegistryCalls { } /** - * 14. Assign a member role to a User + * 14. Get Users' identifiers of a Cou + */ + public JsonArray getUserIdByCouId(Integer couId, boolean admin) { + Map params = new HashMap<>(); + params.put("couid", couId.toString()); + if (admin) { + params.put("admin", "true"); + } + JsonElement response = httpUtils.get("identifiers.json", params); + JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray(); + JsonArray emails = new JsonArray(); + infos.forEach(info -> { + JsonObject user = new JsonObject(); + user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString()); + user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); + emails.add(user); + }); + return emails; + } + + /** + * 15. Assign a member role to a User */ public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) { if (id != null) { @@ -295,21 +316,21 @@ public class RegistryCalls { } /** - * 15. Remove a member role from a User + * 16. Remove a member role from a User */ public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) { httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted")); } /** - * 16. Create a new role + * 17. Create a new role */ public void createRole(Role role) { httpUtils.post("cous.json", jsonUtils.createNewCou(role)); } /** - * 17. Get User's email + * 18. Get User's email */ public String getUserEmail(Integer coPersonId) { Map params = new HashMap<>(); @@ -320,7 +341,7 @@ public class RegistryCalls { } /** - * 18. Get User's names + * 19. Get User's names */ public String getUserNames(Integer coPersonId) { Map params = new HashMap<>(); @@ -331,7 +352,18 @@ public class RegistryCalls { } /** - * 14. Assign an admin role to a User + * 20. Get User's identifier + */ + public String getUserId(Integer coPersonId) { + Map params = new HashMap<>(); + params.put("copersonid", coPersonId.toString()); + JsonElement response = httpUtils.get("identifiers.json", params); + JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null; + return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null; + } + + /** + * 21. Assign an admin role to a User */ public void assignAdminRole(Integer coPersonId, Integer couId) { JsonObject group = getCouAdminGroup(couId); @@ -341,7 +373,7 @@ public class RegistryCalls { } /** - * 15. Remove an admin role from a User + * 22. Remove an admin role from a User */ public void removeAdminRole(Integer coPersonId, Integer couId) { JsonObject adminGroup = this.getCouAdminGroup(couId);