2020-08-04 18:18:08 +02:00
|
|
|
package eu.dnetlib.openaire.usermanagement.utils;
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import eu.dnetlib.openaire.usermanagement.dto.Role;
|
|
|
|
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 RegistryCalls {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(RegistryCalls.class);
|
|
|
|
|
|
|
|
@Value("${registry.coid}")
|
|
|
|
private String coid;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public HttpUtils httpUtils;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public JsonUtils jsonUtils;
|
|
|
|
|
2020-11-04 11:59:49 +01:00
|
|
|
|
2021-01-28 15:40:31 +01:00
|
|
|
private String mapType(String type, boolean communityMap) {
|
2020-11-04 11:59:49 +01:00
|
|
|
if(type.equals("organization")) {
|
|
|
|
type = "institution";
|
2021-01-28 15:40:31 +01:00
|
|
|
} else if(type.equals("ri") && communityMap) {
|
2020-11-04 11:59:49 +01:00
|
|
|
type = "community";
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:18:08 +02:00
|
|
|
/**
|
|
|
|
* 1. Get CoPersonId by Email
|
|
|
|
*/
|
|
|
|
public Integer getCoPersonIdByEmail() {
|
|
|
|
try {
|
|
|
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
String email = authentication.getUserInfo().getEmail();
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
params.put("coid", coid);
|
|
|
|
params.put("mail", email);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2021-01-22 17:12:44 +01:00
|
|
|
if(response != null) {
|
|
|
|
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
|
|
|
|
if(coPeople.size() > 0) {
|
|
|
|
return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-08-04 18:18:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:40:31 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:18:08 +02:00
|
|
|
/**
|
|
|
|
* 4. Get a couId by type.id
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @param id
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Integer getCouId(String type, String id) {
|
2021-01-28 15:40:31 +01:00
|
|
|
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) {
|
2020-08-04 18:18:08 +02:00
|
|
|
JsonArray cous = getCous();
|
|
|
|
Integer couId = null;
|
|
|
|
for (JsonElement cou : cous) {
|
2021-01-28 15:40:31 +01:00
|
|
|
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type, communityMap) + "." + id)) {
|
2020-08-04 18:18:08 +02:00
|
|
|
couId = cou.getAsJsonObject().get("Id").getAsInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return couId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2020-10-05 15:09:34 +02:00
|
|
|
if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
|
2020-08-04 18:18:08 +02:00
|
|
|
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();
|
|
|
|
user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
|
|
|
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
|
|
|
emails.add(user);
|
|
|
|
});
|
|
|
|
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();
|
2020-08-14 15:34:19 +02:00
|
|
|
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
|
2020-08-04 18:18:08 +02:00
|
|
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
|
|
|
names.add(user);
|
|
|
|
});
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 14. Assign a member role to a User
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public void createRole(Role role) {
|
|
|
|
httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 17. 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 18. 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 14. 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 15. 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) {
|
|
|
|
httpUtils.delete("co_group_members/" + id.toString() + ".json");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|