[Users | Trunk]: Merge user info method of members and managers (Keep old paths). Add create member role for a type.id.

This commit is contained in:
Konstantinos Triantafyllou 2021-09-08 16:33:33 +00:00
parent 92da59a386
commit 51b3acf5dc
5 changed files with 79 additions and 102 deletions

View File

@ -14,7 +14,6 @@ import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
@ -106,6 +105,25 @@ public class RegistryService {
}
}
/**
* Create a new role with the given type(Community, etc.) with id(ee, egi, etc.).
**/
@Path("/create/{type}/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
public Response createMemberRole(@PathParam("type") String type, @PathParam("id") String id) {
if (calls.getCouId(type, id) != null) {
if(calls.createRole(new Role(type + "." + id,calls.mapType(type, false) + " " + id)) != null) {
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("An error has occurred. Please try again later").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();
}
}
/**
* Create a new role with the given name and description.
**/
@ -470,60 +488,6 @@ public class RegistryService {
}
}
/**
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/members")
@GET
@Produces(MediaType.APPLICATION_JSON)
@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, 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();
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
}
/**
* Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/members/email")
@GET
@Produces(MediaType.APPLICATION_JSON)
@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, 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();
} else {
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 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.)
*/
@ -534,53 +498,47 @@ public class RegistryService {
Integer couId = calls.getCouId(type, id, false);
int count = 0;
if (couId != null) {
count = calls.getUserNamesByCouId(couId, false).size();
count = calls.getUserIdByCouId(couId, false).size();
}
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
}
/**
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/managers")
@Path("/{type}/{id}/members{var:.*}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@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, false);
if (couId != null) {
JsonArray members = calls.getUserIdByCouId(couId, false);
JsonArray emails = calls.getUserEmailByCouId(couId, false);
JsonArray names = calls.getUserNamesByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(JsonUtils.mergeUserInfo(members, emails, names)).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 infos of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/managers{var:.*}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
JsonArray managers = calls.getUserNamesByCouId(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();
}
}
/**
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/managers/email")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
JsonArray managers = calls.getUserEmailByCouId(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();
}
}
/**
* 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);
if(authorizationService.isManager(type, id) || authorizationService.isPortalAdmin() || authorizationService.isCurator(type)) {
JsonArray emails = calls.getUserEmailByCouId(couId, true);
JsonArray names = calls.getUserNamesByCouId(couId, true);
JsonUtils.mergeUserInfo(managers, emails, names);
}
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();

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Component("AuthorizationService")
public class AuthorizationService {
@ -55,20 +56,28 @@ public class AuthorizationService {
return mapType(type, false).equals("community");
}
public boolean isPortalAdmin() {
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(PORTAL_ADMIN));
}
public boolean isCurator(String type) {
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(curator(type)));
}
public boolean isManager(String type, String id) {
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(manager(type, id)));
}
public boolean isMember(String type, String id) {
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(member(type, id)));
}
public List<String> getRoles() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
if (authorities != null) {
List<String> roles = new ArrayList<>();
authorities.forEach((authority) -> {
roles.add(authority.getAuthority());
});
return roles;
}
if (authentication instanceof OIDCAuthenticationToken) {
return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
}
return null;
return new ArrayList<>();
}
public String getAaiId() {

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.usermanagement.utils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -87,6 +88,14 @@ public class JsonUtils {
return verification;
}
public static JsonArray mergeUserInfo(JsonArray users, JsonArray emails, JsonArray names) {
for (int i = 0; i < users.size(); i++) {
users.get(i).getAsJsonObject().addProperty("email", emails.get(i).getAsJsonObject().get("email").getAsString());
users.get(i).getAsJsonObject().addProperty("name", names.get(i).getAsJsonObject().get("name").getAsString());
}
return users;
}
public JsonObject createResponse(JsonElement response) {
JsonObject json = new JsonObject();
json.add("response", response);

View File

@ -32,7 +32,7 @@ public class RegistryCalls {
public JsonUtils jsonUtils;
private String mapType(String type, boolean communityMap) {
public String mapType(String type, boolean communityMap) {
if(type.equals("organization")) {
type = "institution";
} else if(type.equals("ri") && communityMap) {

View File

@ -48,7 +48,8 @@ public class VerificationUtils {
RoleVerification roleVerification = actions.getMemberVerification(email, type, entity);
if(roleVerification == null) {
roleVerification = actions.addMemberVerification(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime()));
} JsonObject invitation = new JsonObject();
}
JsonObject invitation = new JsonObject();
invitation.addProperty("link", roleVerification.getId());
invitation.addProperty("code", roleVerification.getVerificationCode());
return invitation;