New Add Repository Functionality
This commit is contained in:
parent
4f7cc920da
commit
f9b12e56f5
|
@ -0,0 +1,179 @@
|
||||||
|
package eu.dnetlib.repo.manager.controllers;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
||||||
|
import eu.dnetlib.repo.manager.service.AaiUserRoleService;
|
||||||
|
import eu.dnetlib.repo.manager.utils.AuthorizationService;
|
||||||
|
import eu.dnetlib.repo.manager.utils.JsonUtils;
|
||||||
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.ws.rs.*;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/aai-user-management")
|
||||||
|
public class AaiUserRoleController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AaiUserRoleService calls;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JsonUtils jsonUtils;
|
||||||
|
|
||||||
|
// TODO: Antonis K. This should be uncommented
|
||||||
|
// @Autowired
|
||||||
|
// private AuthoritiesUpdater authoritiesUpdater;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthorizationService authorizationService;
|
||||||
|
|
||||||
|
private String sendEmail() {
|
||||||
|
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
return authenticationToken.getUserInfo().getEmail();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new role with the given name and description.
|
||||||
|
**/
|
||||||
|
@Path("/createRole")
|
||||||
|
@POST
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
||||||
|
*/
|
||||||
|
@Path("/subscribe/{type}/{id}")
|
||||||
|
@POST
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
||||||
|
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
|
||||||
|
Integer coPersonId = calls.getCoPersonIdByIdentifier();
|
||||||
|
Integer couId = calls.getCouId(type, id);
|
||||||
|
if (couId != null) {
|
||||||
|
Integer role = calls.getRoleId(coPersonId, couId);
|
||||||
|
calls.assignMemberRole(coPersonId, couId, role);
|
||||||
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
||||||
|
// authoritiesUpdater.update(sendEmail(), old -> {
|
||||||
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
||||||
|
// authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
||||||
|
// return authorities;
|
||||||
|
// });
|
||||||
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
||||||
|
*/
|
||||||
|
@Path("/{type}/{id}/member/{email}")
|
||||||
|
@DELETE
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
||||||
|
public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
|
||||||
|
id, @PathParam("email") String email) {
|
||||||
|
Integer coPersonId = calls.getCoPersonIdByEmail(email);
|
||||||
|
if (coPersonId != null) {
|
||||||
|
Integer couId = calls.getCouId(type, id);
|
||||||
|
Integer role = null;
|
||||||
|
if (couId != null) {
|
||||||
|
role = calls.getRoleId(coPersonId, couId);
|
||||||
|
}
|
||||||
|
if (couId != null && role != null) {
|
||||||
|
calls.removeMemberRole(coPersonId, couId, role);
|
||||||
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
||||||
|
// authoritiesUpdater.update(email, old -> {
|
||||||
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
||||||
|
// authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
||||||
|
// return authorities;
|
||||||
|
// });
|
||||||
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").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();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the users that have the role that is associated with repoId
|
||||||
|
*/
|
||||||
|
@Path("/repo/{id}/all-users")
|
||||||
|
@GET
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
||||||
|
public Response getAllUserOfARepo(@PathParam("id") String id) {
|
||||||
|
|
||||||
|
// find roleId by repoId
|
||||||
|
String roleId = calls.getRoleIdByRepoId(id, "datasource");
|
||||||
|
|
||||||
|
// find couId by role name
|
||||||
|
if (roleId != null) {
|
||||||
|
Integer couId = calls.getCouId("datasource", roleId);
|
||||||
|
if (couId != null) {
|
||||||
|
JsonArray users = calls.getUsersByCouId(couId);
|
||||||
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(users).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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to role-repo by his email
|
||||||
|
*/
|
||||||
|
@Path("/subscribe/repo-role/{id}/email/{email}")
|
||||||
|
@POST
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
||||||
|
public Response subscribeRoleByEmail(@PathParam("id") String id, @PathParam("email") String email) {
|
||||||
|
Integer coPersonId = calls.getCoPersonIdByEmail(email);
|
||||||
|
if (coPersonId != null) {
|
||||||
|
String roleId = calls.getRoleIdByRepoId(id, "datasource");
|
||||||
|
if (roleId != null) {
|
||||||
|
Integer couId = calls.getCouId("datasource", roleId);
|
||||||
|
if (couId != null) {
|
||||||
|
Integer role = calls.getRoleId(coPersonId, couId);
|
||||||
|
calls.assignMemberRole(coPersonId, couId, role);
|
||||||
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
||||||
|
// authoritiesUpdater.update(sendEmail(), old -> {
|
||||||
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
||||||
|
// authorities.add(new SimpleGrantedAuthority(authorizationService.member("datasource", id)));
|
||||||
|
// return authorities;
|
||||||
|
// });
|
||||||
|
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").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();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User with this email has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package eu.dnetlib.repo.manager.domain.dto;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement
|
||||||
|
public class Role {
|
||||||
|
String name;
|
||||||
|
String description;
|
||||||
|
|
||||||
|
public Role() {}
|
||||||
|
|
||||||
|
public Role(String name, String description) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,79 @@
|
||||||
|
package eu.dnetlib.repo.manager.service;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
||||||
|
|
||||||
|
public interface AaiUserRoleService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1. Get CoPersonId by Email
|
||||||
|
*/
|
||||||
|
Integer getCoPersonIdByEmail(String email);
|
||||||
|
|
||||||
|
Integer getCoPersonIdByIdentifier();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3. Get all OpenAIRE cous
|
||||||
|
*/
|
||||||
|
JsonArray getCous();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4. Get a couId by type.id
|
||||||
|
*
|
||||||
|
* @param type
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Integer getCouId(String type, String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 5. Get User non admin roles
|
||||||
|
*/
|
||||||
|
JsonArray getRoles(Integer coPersonId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 6. Get Role id of User base on couId.
|
||||||
|
*/
|
||||||
|
Integer getRoleId(Integer coPersonId, Integer couId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 16. Create a new role
|
||||||
|
*/
|
||||||
|
void createRole(Role role);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 14. Assign a member role to a User
|
||||||
|
*/
|
||||||
|
void assignMemberRole(Integer coPersonId, Integer couId, Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 15. Remove a member role from a User
|
||||||
|
*/
|
||||||
|
void removeMemberRole(Integer coPersonId, Integer couId, Integer id);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util function to remove the datasource prefix in role Id
|
||||||
|
* @param fullName
|
||||||
|
* @param prefix
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getRepoNameWithoutType(String fullName, String prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util function to transform repoId name to roleId name
|
||||||
|
* @param repoId
|
||||||
|
* @param prefix
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String getRoleIdByRepoId(String repoId, String prefix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 12. Get All Users that have a specific role
|
||||||
|
* @param couId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
JsonArray getUsersByCouId(Integer couId);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,225 @@
|
||||||
|
package eu.dnetlib.repo.manager.service;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
||||||
|
import eu.dnetlib.repo.manager.utils.HttpUtils;
|
||||||
|
import eu.dnetlib.repo.manager.utils.JsonUtils;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AaiUserRoleServiceImpl implements AaiUserRoleService {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(AaiUserRoleServiceImpl.class);
|
||||||
|
|
||||||
|
@Value("2")
|
||||||
|
private String coid;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public HttpUtils httpUtils;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JsonUtils jsonUtils;
|
||||||
|
|
||||||
|
private String mapType(String type) {
|
||||||
|
if(type.equals("datasource")) {
|
||||||
|
type = "datasource";
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRepoNameWithoutType(String fullName, String prefix) {
|
||||||
|
if ( fullName != null && prefix != null && fullName.startsWith(prefix) ) {
|
||||||
|
return fullName.substring(prefix.length());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleIdByRepoId(String repoId, String prefix) {
|
||||||
|
String roleId = "";
|
||||||
|
if ( repoId != null && prefix != null ) {
|
||||||
|
roleId = prefix + "." + repoId.replaceAll(":", "$");
|
||||||
|
return roleId;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1. Get CoPersonId by Email
|
||||||
|
* @param email
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Integer getCoPersonIdByEmail(String email) {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("coid", coid);
|
||||||
|
params.put("mail", email);
|
||||||
|
JsonElement response = httpUtils.get("co_people.json", params);
|
||||||
|
if(response != null) {
|
||||||
|
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
|
||||||
|
if(coPeople.size() > 0) {
|
||||||
|
return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2. Get CoPersonId by AAI identifier
|
||||||
|
*/
|
||||||
|
public Integer getCoPersonIdByIdentifier() {
|
||||||
|
try {
|
||||||
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String sub = authentication.getUserInfo().getSub();
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("coid", coid);
|
||||||
|
params.put("search.identifier", sub);
|
||||||
|
JsonElement response = httpUtils.get("co_people.json", params);
|
||||||
|
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Get User info: An error occurred ", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3. Get all OpenAIRE cous
|
||||||
|
*/
|
||||||
|
public JsonArray getCous() {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("coid", coid);
|
||||||
|
JsonElement response = httpUtils.get("cous.json", params);
|
||||||
|
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4. Get a couId by type.id
|
||||||
|
*
|
||||||
|
* @param type
|
||||||
|
* @param id It is the datasourceId (e.g openaire____$$45e3c7b69bcee6cc5fa945c9e183deb9)
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Integer getCouId(String type, String id) {
|
||||||
|
JsonArray cous = getCous();
|
||||||
|
Integer couId = null;
|
||||||
|
for (JsonElement cou : cous) {
|
||||||
|
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type) + "." + id)) {
|
||||||
|
couId = cou.getAsJsonObject().get("Id").getAsInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return couId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 5. Get User non admin roles
|
||||||
|
* @param coPersonId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public JsonArray getRoles(Integer coPersonId) {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("copersonid", coPersonId.toString());
|
||||||
|
JsonElement response = httpUtils.get("co_person_roles.json", params);
|
||||||
|
return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 6. Get Role id of User base on couId.
|
||||||
|
* @param coPersonId
|
||||||
|
* @param couId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Integer getRoleId(Integer coPersonId, Integer couId) {
|
||||||
|
JsonArray roles = getRoles(coPersonId);
|
||||||
|
for (JsonElement role : roles) {
|
||||||
|
JsonObject object = role.getAsJsonObject();
|
||||||
|
if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
|
||||||
|
return object.get("Id").getAsInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 16. Create a new role
|
||||||
|
* @param role
|
||||||
|
*/
|
||||||
|
public void createRole(Role role) {
|
||||||
|
httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 14. Assign a member role to a User
|
||||||
|
* @param coPersonId The id of a person in OpenAIRE
|
||||||
|
* @param couId The id of a role in OpenAIRE (result of getCouId)
|
||||||
|
* @param id The id that is returned from getRoleId (role's id)
|
||||||
|
*/
|
||||||
|
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
||||||
|
if (id != null) {
|
||||||
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
||||||
|
} else {
|
||||||
|
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 15. Remove a member role from a User
|
||||||
|
* @param coPersonId The id of a person in OpenAIRE
|
||||||
|
* @param couId The id of a role in OpenAIRE (result of getCouId)
|
||||||
|
* @param id The id that is returned from getRoleId (role's id)
|
||||||
|
*/
|
||||||
|
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
||||||
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 12. Get All Users that have a specific role
|
||||||
|
*/
|
||||||
|
public JsonArray getUsersByCouId(Integer couId) {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("couid", couId.toString());
|
||||||
|
JsonElement response = httpUtils.get("co_person_roles.json", params);
|
||||||
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
|
||||||
|
// JsonArray users = new JsonArray();
|
||||||
|
// infos.forEach(info -> {
|
||||||
|
// JsonObject user = new JsonObject();
|
||||||
|
// user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
|
||||||
|
// user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
||||||
|
// emails.add(user);
|
||||||
|
// });
|
||||||
|
return infos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 9. Get Groups of a Cou
|
||||||
|
*/
|
||||||
|
public JsonArray getCouGroups(Integer couId) {
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
params.put("coid", coid);
|
||||||
|
params.put("couid", couId.toString());
|
||||||
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
||||||
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 10. Get Admin Group of a Cou
|
||||||
|
*/
|
||||||
|
public JsonObject getCouAdminGroup(Integer couId) {
|
||||||
|
JsonArray groups = getCouGroups(couId);
|
||||||
|
for (JsonElement group : groups) {
|
||||||
|
if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
|
||||||
|
return group.getAsJsonObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package eu.dnetlib.repo.manager.utils;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("AuthorizationService")
|
||||||
|
public class AuthorizationService {
|
||||||
|
|
||||||
|
public final String ROLE_ADMIN = "ROLE_ADMIN";
|
||||||
|
public final String ROLE_PROVIDE_ADMIN = "ROLE_PROVIDE_ADMIN";
|
||||||
|
public final String ROLE_USER = "ROLE_USER";
|
||||||
|
|
||||||
|
private String mapType(String type) {
|
||||||
|
if(type.equals("datasource")) {
|
||||||
|
type = "datasource";
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type = DATASOURCE
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
public String member(String type, String id) {
|
||||||
|
return mapType(type).toUpperCase() + "_" + id.toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package eu.dnetlib.repo.manager.utils;
|
||||||
|
|
||||||
|
public class DatasourceManagerClient {
|
||||||
|
//
|
||||||
|
}
|
|
@ -0,0 +1,105 @@
|
||||||
|
package eu.dnetlib.repo.manager.utils;
|
||||||
|
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class HttpUtils {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(HttpUtils.class);
|
||||||
|
|
||||||
|
// TODO: To be changed the values
|
||||||
|
// @Value("https://aai.openaire.eu/registry/")
|
||||||
|
@Value("https://openaire-dev.aai-dev.grnet.gr/registry/")
|
||||||
|
private String issuer;
|
||||||
|
|
||||||
|
@Value("kostis30fylloy")
|
||||||
|
private String user;
|
||||||
|
|
||||||
|
@Value("fynhWc7F*2y4me4U")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public JsonElement post(String path, JsonObject body) {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
HttpHeaders headers = createHeaders(user, password);
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class);
|
||||||
|
if(responseEntity.getBody() != null) {
|
||||||
|
return new JsonParser().parse(responseEntity.getBody());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonElement put(String path, JsonObject body) {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
HttpHeaders headers = createHeaders(user, password);
|
||||||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class);
|
||||||
|
if(responseEntity.getBody() != null) {
|
||||||
|
return new JsonParser().parse(responseEntity.getBody());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonElement get(String path, Map<String, String> params) {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
String url = issuer + path + ((params != null) ? createParams(params) : null);
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
||||||
|
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
|
||||||
|
if(responseEntity.getBody() != null) {
|
||||||
|
return new JsonParser().parse(responseEntity.getBody());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonElement delete(String path) {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
String url = issuer + path;
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
||||||
|
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
||||||
|
if(responseEntity.getBody() != null) {
|
||||||
|
return new JsonParser().parse(responseEntity.getBody());
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String createParams(Map<String, String> params) {
|
||||||
|
StringBuilder ret = new StringBuilder("?");
|
||||||
|
int count = 0;
|
||||||
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
||||||
|
ret.append(param.getKey()).append("=").append(param.getValue());
|
||||||
|
count++;
|
||||||
|
if (count != params.entrySet().size()) {
|
||||||
|
ret.append("&");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpHeaders createHeaders(String username, String password) {
|
||||||
|
return new HttpHeaders() {{
|
||||||
|
String auth = username + ":" + password;
|
||||||
|
byte[] encodedAuth = Base64.encodeBase64(
|
||||||
|
auth.getBytes(Charset.forName("US-ASCII")));
|
||||||
|
String authHeader = "Basic " + new String(encodedAuth);
|
||||||
|
set("Authorization", authHeader);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package eu.dnetlib.repo.manager.utils;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JsonUtils {
|
||||||
|
|
||||||
|
@Value("1.0")
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
@Value("2")
|
||||||
|
private String coid;
|
||||||
|
|
||||||
|
public JsonObject coPersonRoles(Integer coPersonId, Integer couId, String status) {
|
||||||
|
JsonObject role = new JsonObject();
|
||||||
|
JsonArray coPersonRoles = new JsonArray();
|
||||||
|
JsonObject coPersonRole = new JsonObject();
|
||||||
|
JsonObject person = new JsonObject();
|
||||||
|
person.addProperty("Type", "CO");
|
||||||
|
person.addProperty("Id", coPersonId.toString());
|
||||||
|
coPersonRole.addProperty("Version", version);
|
||||||
|
coPersonRole.add("Person", person);
|
||||||
|
coPersonRole.addProperty("CouId", couId.toString());
|
||||||
|
coPersonRole.addProperty("Affiliation", "member");
|
||||||
|
coPersonRole.addProperty("Title", "");
|
||||||
|
coPersonRole.addProperty("O", "Openaire");
|
||||||
|
coPersonRole.addProperty("Status", status);
|
||||||
|
coPersonRole.addProperty("ValidFrom", "");
|
||||||
|
coPersonRole.addProperty("ValidThrough", "");
|
||||||
|
coPersonRoles.add(coPersonRole);
|
||||||
|
role.addProperty("RequestType", "CoPersonRoles");
|
||||||
|
role.addProperty("Version", version);
|
||||||
|
role.add("CoPersonRoles", coPersonRoles);
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createNewCou(Role role) {
|
||||||
|
JsonObject cou = new JsonObject();
|
||||||
|
JsonArray cous = new JsonArray();
|
||||||
|
JsonObject newCou = new JsonObject();
|
||||||
|
newCou.addProperty("Version", version);
|
||||||
|
newCou.addProperty("CoId", coid);
|
||||||
|
newCou.addProperty("Name", role.getName());
|
||||||
|
newCou.addProperty("Description", role.getDescription());
|
||||||
|
cous.add(newCou);
|
||||||
|
cou.addProperty("RequestType", "Cous");
|
||||||
|
cou.addProperty("Version", version);
|
||||||
|
cou.add("Cous", cous);
|
||||||
|
return cou;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createResponse(JsonElement response) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
json.add("response", response);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createResponse(String response) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
json.addProperty("response", response);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createResponse(Number response) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
json.addProperty("response", response);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createResponse(Boolean response) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
json.addProperty("response", response);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonObject createResponse(Character response) {
|
||||||
|
JsonObject json = new JsonObject();
|
||||||
|
json.addProperty("response", response);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue