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 f57cb38..16d7e00 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java @@ -120,8 +120,12 @@ public class RegistryService { @Consumes(MediaType.APPLICATION_JSON) @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") public Response createRole(@RequestBody Role role) { - calls.createRole(role); - return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); + if(calls.getCouId(role.getName()) == null) { + calls.createRole(role); + return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); + } else { + return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build(); + } } /** @@ -162,7 +166,7 @@ public class RegistryService { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " + "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id, false); if (couId != null) { JsonObject details = new JsonParser().parse(body).getAsJsonObject(); JsonObject email = details.get("email").getAsJsonObject(); @@ -224,7 +228,7 @@ public class RegistryService { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " + "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id, false); if (couId != null) { verificationUtils.deleteMemberVerifications(email, type, id); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build(); @@ -360,7 +364,7 @@ public class RegistryService { if (coPersonId != null) { if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) { if (verification.getVerificationCode().equals(code)) { - Integer couId = calls.getCouId(verification.getType(), verification.getEntity()); + Integer couId = calls.getCouId(verification.getType(), verification.getEntity(), false); if (couId != null) { Integer role = calls.getRoleId(coPersonId, couId); calls.assignMemberRole(coPersonId, couId, role); @@ -429,7 +433,7 @@ public class RegistryService { id, @PathParam("email") String email) { Integer coPersonId = calls.getCoPersonIdByEmail(email); if (coPersonId != null) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id, false); Integer role = null; if(couId != null) { role = calls.getRoleId(coPersonId, couId); @@ -461,7 +465,7 @@ public class RegistryService { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," + "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id,false); if(couId != null) { JsonArray members = calls.getUserNamesByCouId(couId, false); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build(); @@ -479,7 +483,7 @@ public class RegistryService { @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," + "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id, false); if(couId != null) { JsonArray members = calls.getUserEmailByCouId(couId, false); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build(); @@ -495,7 +499,7 @@ public class RegistryService { @GET @Produces(MediaType.APPLICATION_JSON) public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) { - Integer couId = calls.getCouId(type, id); + Integer couId = calls.getCouId(type, id, false); int count = 0; if(couId != null) { count = calls.getUserNamesByCouId(couId, false).size(); diff --git a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/AuthorizationService.java b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/AuthorizationService.java index eb0327c..2456767 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/AuthorizationService.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/AuthorizationService.java @@ -6,12 +6,13 @@ import org.springframework.stereotype.Component; public class AuthorizationService { public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR"; + public final String ANONYMOUS_USER = "ROLE_ANONYMOUS"; + public final String REGISTERED_USER = "REGISTERED_USER"; - private String mapType(String type) { + private String mapType(String type, boolean communityMap) { if(type.equals("organization")) { type = "institution"; - } - if(type.equals("ri")) { + } else if(type.equals("ri") && communityMap) { type = "community"; } return type; @@ -22,7 +23,7 @@ public class AuthorizationService { * * */ public String curator(String type) { - return "CURATOR_" + mapType(type).toUpperCase(); + return "CURATOR_" + mapType(type, true).toUpperCase(); } /** @@ -31,19 +32,19 @@ public class AuthorizationService { * Id = EE, EGI, etc * */ public String manager(String type, String id) { - return mapType(type).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; + return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; } /** - * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT + * Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT * * Id = EE, EGI, etc * */ public String member(String type, String id) { - return mapType(type).toUpperCase() + "_" + id.toUpperCase(); + return mapType(type, false).toUpperCase() + "_" + id.toUpperCase(); } public boolean isCommunity(String type) { - return mapType(type).equals("community"); + return mapType(type, false).equals("community"); } } 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 20857ce..338be62 100644 --- a/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java +++ b/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java @@ -29,11 +29,10 @@ public class RegistryCalls { public JsonUtils jsonUtils; - private String mapType(String type) { + private String mapType(String type, boolean communityMap) { if(type.equals("organization")) { type = "institution"; - } - if(type.equals("ri")) { + } else if(type.equals("ri") && communityMap) { type = "community"; } return type; @@ -107,6 +106,23 @@ public class RegistryCalls { return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); } + /** + * 4. Get a couId by name + * + * @param name + * @return + */ + public Integer getCouId(String name) { + JsonArray cous = getCous(); + Integer couId = null; + for (JsonElement cou : cous) { + if (cou.getAsJsonObject().get("Name").getAsString().equals(name)) { + couId = cou.getAsJsonObject().get("Id").getAsInt(); + } + } + return couId; + } + /** * 4. Get a couId by type.id * @@ -115,10 +131,22 @@ public class RegistryCalls { * @return */ public Integer getCouId(String type, String id) { + return getCouId(type, id, true); + } + + + /** + * 4. Get a couId by type.id without mapping type + * + * @param type + * @param id + * @return + */ + public Integer getCouId(String type, String id, boolean communityMap) { JsonArray cous = getCous(); Integer couId = null; for (JsonElement cou : cous) { - if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type) + "." + id)) { + if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type, communityMap) + "." + id)) { couId = cou.getAsJsonObject().get("Id").getAsInt(); } } diff --git a/src/main/webapp/registerService.jsp b/src/main/webapp/registerService.jsp index 95c0cd0..4533c42 100644 --- a/src/main/webapp/registerService.jsp +++ b/src/main/webapp/registerService.jsp @@ -88,7 +88,7 @@
-
Security level hint
+