2021-07-06 10:20:00 +02:00
|
|
|
package eu.dnetlib.dnetrolemanagement.services;
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import eu.dnetlib.dnetrolemanagement.config.properties.RegistryProperties;
|
|
|
|
import eu.dnetlib.dnetrolemanagement.utils.HttpUtils;
|
|
|
|
import eu.dnetlib.dnetrolemanagement.utils.JsonUtils;
|
2022-09-26 10:50:42 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-07-06 10:20:00 +02:00
|
|
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2021-07-13 15:51:42 +02:00
|
|
|
import java.util.*;
|
2021-07-06 10:20:00 +02:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class RegistryService {
|
|
|
|
|
2022-09-26 10:50:42 +02:00
|
|
|
private static final Logger logger = LogManager.getLogger(RegistryService.class);
|
2021-07-06 10:20:00 +02:00
|
|
|
|
|
|
|
private final String coid;
|
|
|
|
|
|
|
|
public HttpUtils httpUtils;
|
|
|
|
|
|
|
|
public JsonUtils jsonUtils;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public RegistryService(HttpUtils httpUtils, JsonUtils jsonUtils, RegistryProperties registryProperties) {
|
|
|
|
this.coid = registryProperties.getCoid();
|
|
|
|
this.httpUtils = httpUtils;
|
|
|
|
this.jsonUtils = jsonUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-07 16:38:58 +02:00
|
|
|
* 1.1 Get CoPersonId by Email
|
2021-07-06 10:20:00 +02:00
|
|
|
*/
|
|
|
|
public List<Integer> getCoPersonIdsByEmail(String email) {
|
2021-09-09 13:32:59 +02:00
|
|
|
if (email != null) {
|
2021-07-13 15:51:42 +02:00
|
|
|
List<Integer> coPersonIds = new ArrayList<>();
|
|
|
|
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();
|
|
|
|
for (int i = 0; i < coPeople.size(); i++) {
|
|
|
|
coPersonIds.add(coPeople.get(i).getAsJsonObject().get("Id").getAsInt());
|
|
|
|
}
|
2021-07-06 10:20:00 +02:00
|
|
|
}
|
2021-07-13 15:51:42 +02:00
|
|
|
return coPersonIds;
|
|
|
|
} else {
|
|
|
|
Integer coPersonId = getCoPersonIdByIdentifier();
|
2021-09-09 13:32:59 +02:00
|
|
|
return (coPersonId != null) ? Collections.singletonList(coPersonId) : new ArrayList<>();
|
2021-07-06 10:20:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-07 16:38:58 +02:00
|
|
|
* 1.2 Get CoPersonId by AAI identifier
|
2021-07-06 10:20:00 +02:00
|
|
|
*/
|
|
|
|
public Integer getCoPersonIdByIdentifier() {
|
|
|
|
try {
|
|
|
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
String sub = authentication.getUserInfo().getSub();
|
2021-07-07 16:38:58 +02:00
|
|
|
return getCoPersonIdByIdentifier(sub);
|
2021-07-06 10:20:00 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Get User info: An error occurred ", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Integer getCoPersonIdByIdentifier(String sub) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:38:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 2 Get AAI identifier by CoPersonId
|
|
|
|
*/
|
|
|
|
public String getIdentifierByCoPersonId(Integer coPersonId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("copersonid", coPersonId.toString());
|
|
|
|
JsonElement response = httpUtils.get("identifiers.json", params);
|
2021-09-09 13:32:59 +02:00
|
|
|
JsonArray ids = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
|
|
|
|
if (ids.size() > 0) {
|
2021-07-07 16:38:58 +02:00
|
|
|
return ids.get(0).getAsJsonObject().get("Identifier").getAsString();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:20:00 +02:00
|
|
|
/**
|
|
|
|
* 3.1 Get OpenAIRE cous with a specific name(or substring)
|
|
|
|
*/
|
|
|
|
public JsonArray getCous(String name) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("coid", coid);
|
2021-07-07 16:38:58 +02:00
|
|
|
if (name != null) {
|
2021-07-06 10:20:00 +02:00
|
|
|
params.put("name", name.toLowerCase());
|
|
|
|
}
|
|
|
|
JsonElement response = httpUtils.get("cous.json", params);
|
|
|
|
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 3.2 Get all OpenAIRE cous
|
|
|
|
*/
|
|
|
|
public JsonArray getCous() {
|
|
|
|
return getCous(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 4.1 Get a couId by name
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Integer getCouId(String name) {
|
|
|
|
JsonArray cous = getCous(name);
|
|
|
|
for (JsonElement cou : cous) {
|
2021-10-04 15:35:27 +02:00
|
|
|
if (cou.getAsJsonObject().get("Name").getAsString().equalsIgnoreCase(name)) {
|
2021-07-06 10:20:00 +02:00
|
|
|
return cou.getAsJsonObject().get("Id").getAsInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 5. Get User non admin roles
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 7. Get User Groups
|
|
|
|
*/
|
|
|
|
public JsonArray getUserGroups(Integer coPersonId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("copersonid", coPersonId.toString());
|
|
|
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
|
|
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 8. Get User Admin Group of a Cou
|
|
|
|
*/
|
|
|
|
public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("copersonid", coPersonId.toString());
|
|
|
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
|
|
|
JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
|
|
|
for (JsonElement role : roles) {
|
|
|
|
JsonObject object = role.getAsJsonObject();
|
|
|
|
if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
|
|
|
|
if (object.get("Name").getAsString().contains("admins")) {
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 11. Get users of a group
|
|
|
|
*/
|
|
|
|
public JsonArray getGroupMembers(Integer coGroupId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("cogroupid", coGroupId.toString());
|
|
|
|
JsonElement response = httpUtils.get("co_group_members.json", params);
|
|
|
|
return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 12. Get Users' email of a Cou
|
|
|
|
*/
|
|
|
|
public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("couid", couId.toString());
|
|
|
|
if (admin) {
|
|
|
|
params.put("admin", "true");
|
|
|
|
}
|
|
|
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
|
|
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
|
|
|
|
JsonArray emails = new JsonArray();
|
|
|
|
infos.forEach(info -> {
|
|
|
|
JsonObject user = new JsonObject();
|
2021-09-09 13:32:59 +02:00
|
|
|
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
|
|
|
|
user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
|
|
|
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
|
|
|
emails.add(user);
|
2021-07-06 10:20:00 +02:00
|
|
|
});
|
|
|
|
return emails;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 13. Get Users' names of a Cou
|
|
|
|
*/
|
|
|
|
public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("couid", couId.toString());
|
|
|
|
if (admin) {
|
|
|
|
params.put("admin", "true");
|
|
|
|
}
|
|
|
|
JsonElement response = httpUtils.get("names.json", params);
|
|
|
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
|
|
|
|
JsonArray names = new JsonArray();
|
|
|
|
infos.forEach(info -> {
|
|
|
|
JsonObject user = new JsonObject();
|
2021-09-09 13:32:59 +02:00
|
|
|
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
|
2021-07-06 10:20:00 +02:00
|
|
|
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
|
|
|
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
|
|
|
names.add(user);
|
|
|
|
});
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 14. Get Users' identifiers of a Cou
|
|
|
|
*/
|
|
|
|
public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
|
|
|
|
Map<String, String> 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();
|
2021-07-06 16:47:39 +02:00
|
|
|
JsonArray ids = new JsonArray();
|
2021-07-06 10:20:00 +02:00
|
|
|
infos.forEach(info -> {
|
|
|
|
JsonObject user = new JsonObject();
|
2021-09-09 13:32:59 +02:00
|
|
|
user.addProperty("coPersonId", info.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsString());
|
2021-07-06 10:20:00 +02:00
|
|
|
user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
|
|
|
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
2021-07-06 16:47:39 +02:00
|
|
|
ids.add(user);
|
2021-07-06 10:20:00 +02:00
|
|
|
});
|
2021-07-06 16:47:39 +02:00
|
|
|
return ids;
|
2021-07-06 10:20:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 15. Assign a member role to a User
|
|
|
|
*/
|
|
|
|
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
|
|
|
if (id != null) {
|
2021-10-18 15:46:38 +02:00
|
|
|
httpUtils.put("co_person_roles/" + id + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
2021-07-06 10:20:00 +02:00
|
|
|
} else {
|
|
|
|
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 16. Remove a member role from a User
|
|
|
|
*/
|
|
|
|
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
2021-07-07 16:38:58 +02:00
|
|
|
if (id != null) {
|
2021-10-18 15:46:38 +02:00
|
|
|
httpUtils.put("co_person_roles/" + id + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
|
2021-07-06 10:20:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 17. Create a new role
|
|
|
|
*/
|
|
|
|
public Integer createRole(String name, String description) {
|
|
|
|
JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(name, description));
|
|
|
|
return element.getAsJsonObject().get("Id").getAsInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 18. Get User's email
|
|
|
|
*/
|
|
|
|
public String getUserEmail(Integer coPersonId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("copersonid", coPersonId.toString());
|
|
|
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
|
|
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
|
|
|
|
return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 19. Get User's names
|
|
|
|
*/
|
|
|
|
public String getUserNames(Integer coPersonId) {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("copersonid", coPersonId.toString());
|
|
|
|
JsonElement response = httpUtils.get("names.json", params);
|
|
|
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
|
|
|
|
return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 20. Get User's identifier
|
|
|
|
*/
|
|
|
|
public String getUserId(Integer coPersonId) {
|
|
|
|
Map<String, String> 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);
|
|
|
|
if (group != null) {
|
|
|
|
httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 22. Remove an admin role from a User
|
|
|
|
*/
|
|
|
|
public void removeAdminRole(Integer coPersonId, Integer couId) {
|
|
|
|
JsonObject adminGroup = this.getCouAdminGroup(couId);
|
|
|
|
JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
|
|
|
|
Integer id = null;
|
|
|
|
for (JsonElement admin : admins) {
|
|
|
|
if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
|
|
|
|
id = admin.getAsJsonObject().get("Id").getAsInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (id != null) {
|
2021-10-18 15:46:38 +02:00
|
|
|
httpUtils.delete("co_group_members/" + id + ".json");
|
2021-07-06 10:20:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|