Make changes regardingh new registry version

This commit is contained in:
Konstantinos Triantafyllou 2022-06-15 15:03:21 +03:00
parent 73996b7523
commit b4c4f285cf
7 changed files with 23 additions and 26 deletions

View File

@ -84,7 +84,7 @@ public class AdminController {
Integer role = registryService.getRoleId(coPersonId, couId); Integer role = registryService.getRoleId(coPersonId, couId);
if (role != null || force) { if (role != null || force) {
if (role == null) { if (role == null) {
registryService.assignMemberRole(coPersonId, couId, role); registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id))); authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
} }
if (registryService.getUserAdminGroup(coPersonId, couId) == null) { if (registryService.getUserAdminGroup(coPersonId, couId) == null) {

View File

@ -80,8 +80,7 @@ public class CuratorController {
Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.curatorRole(type), ""); Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.curatorRole(type), "");
coPersonIds.forEach(coPersonId -> { coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId); String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId); registryService.assignMemberRole(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.curator(type))); authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.curator(type)));
}); });
return ResponseEntity.ok(new Response("Role has been assigned successfully")); return ResponseEntity.ok(new Response("Role has been assigned successfully"));

View File

@ -103,8 +103,7 @@ public class MemberController {
Integer couId = (temp != null) ? temp : registryService.createRole(AuthoritiesUtils.memberRole(type, id), ""); Integer couId = (temp != null) ? temp : registryService.createRole(AuthoritiesUtils.memberRole(type, id), "");
coPersonIds.forEach(coPersonId -> { coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId); String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId); registryService.assignMemberRole(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id))); authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
}); });
return ResponseEntity.ok(new Response("Role has been assigned successfully")); return ResponseEntity.ok(new Response("Role has been assigned successfully"));

View File

@ -59,8 +59,7 @@ public class SuperAdminController {
if (couId != null) { if (couId != null) {
coPersonIds.forEach(coPersonId -> { coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId); String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId); registryService.assignMemberRole(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.PORTAL_ADMIN)); authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.PORTAL_ADMIN));
}); });
return ResponseEntity.ok(new Response("Role has been assigned successfully")); return ResponseEntity.ok(new Response("Role has been assigned successfully"));

View File

@ -3,7 +3,6 @@ package eu.dnetlib.dnetrolemanagement.services;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import eu.dnetlib.dnetrolemanagement.config.properties.RegistryProperties;
import eu.dnetlib.dnetrolemanagement.utils.HttpUtils; import eu.dnetlib.dnetrolemanagement.utils.HttpUtils;
import eu.dnetlib.dnetrolemanagement.utils.JsonUtils; import eu.dnetlib.dnetrolemanagement.utils.JsonUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -19,15 +18,12 @@ public class RegistryService {
private static final Logger logger = Logger.getLogger(RegistryService.class); private static final Logger logger = Logger.getLogger(RegistryService.class);
private final String coid;
public HttpUtils httpUtils; public HttpUtils httpUtils;
public JsonUtils jsonUtils; public JsonUtils jsonUtils;
@Autowired @Autowired
public RegistryService(HttpUtils httpUtils, JsonUtils jsonUtils, RegistryProperties registryProperties) { public RegistryService(HttpUtils httpUtils, JsonUtils jsonUtils) {
this.coid = registryProperties.getCoid();
this.httpUtils = httpUtils; this.httpUtils = httpUtils;
this.jsonUtils = jsonUtils; this.jsonUtils = jsonUtils;
} }
@ -39,7 +35,6 @@ public class RegistryService {
if (email != null) { if (email != null) {
List<Integer> coPersonIds = new ArrayList<>(); List<Integer> coPersonIds = new ArrayList<>();
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid);
params.put("mail", email); params.put("mail", email);
JsonElement response = httpUtils.get("co_people.json", params); JsonElement response = httpUtils.get("co_people.json", params);
if (response != null) { if (response != null) {
@ -71,7 +66,6 @@ public class RegistryService {
public Integer getCoPersonIdByIdentifier(String sub) { public Integer getCoPersonIdByIdentifier(String sub) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid);
params.put("search.identifier", sub); params.put("search.identifier", sub);
JsonElement response = httpUtils.get("co_people.json", params); JsonElement response = httpUtils.get("co_people.json", params);
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null; return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
@ -97,7 +91,6 @@ public class RegistryService {
*/ */
public JsonArray getCous(String name) { public JsonArray getCous(String name) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid);
if (name != null) { if (name != null) {
params.put("name", name.toLowerCase()); params.put("name", name.toLowerCase());
} }
@ -186,7 +179,6 @@ public class RegistryService {
*/ */
public JsonArray getCouGroups(Integer couId) { public JsonArray getCouGroups(Integer couId) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid);
params.put("couid", couId.toString()); params.put("couid", couId.toString());
JsonElement response = httpUtils.get("co_groups.json", params); JsonElement response = httpUtils.get("co_groups.json", params);
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray(); return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
@ -285,12 +277,8 @@ public class RegistryService {
/** /**
* 15. Assign a member role to a User * 15. Assign a member role to a User
*/ */
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) { public void assignMemberRole(Integer coPersonId, Integer couId) {
if (id != null) { httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
httpUtils.put("co_person_roles/" + id + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
} else {
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
}
} }
/** /**

View File

@ -12,6 +12,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@Component @Component
@ -32,7 +33,7 @@ public class HttpUtils {
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(registryProperties.getIssuer() + path, HttpMethod.POST, request, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(registryProperties.getIssuer() + path, HttpMethod.POST, request, String.class);
if (responseEntity.getBody() != null) { if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.getBody() != null) {
return new JsonParser().parse(responseEntity.getBody()); return new JsonParser().parse(responseEntity.getBody());
} else { } else {
return null; return null;
@ -54,7 +55,7 @@ public class HttpUtils {
public JsonElement get(String path, Map<String, String> params) { public JsonElement get(String path, Map<String, String> params) {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String url = registryProperties.getIssuer() + path + ((params != null) ? createParams(params) : null); String url = registryProperties.getIssuer() + path + createParams(addCoId(params));
ResponseEntity<String> responseEntity = restTemplate.exchange ResponseEntity<String> responseEntity = restTemplate.exchange
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(registryProperties.getUser(), registryProperties.getPassword())), String.class); (url, HttpMethod.GET, new HttpEntity<>(createHeaders(registryProperties.getUser(), registryProperties.getPassword())), String.class);
if (responseEntity.getBody() != null) { if (responseEntity.getBody() != null) {
@ -76,6 +77,13 @@ public class HttpUtils {
} }
} }
private Map<String, String> addCoId(Map<String, String> params) {
if(params == null) {
params = new HashMap<>();
}
params.put("coid", registryProperties.getCoid());
return params;
}
private String createParams(Map<String, String> params) { private String createParams(Map<String, String> params) {
StringBuilder ret = new StringBuilder("?"); StringBuilder ret = new StringBuilder("?");

View File

@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.Optional; import java.util.Optional;
@Component @Component
@ -51,8 +52,11 @@ public class JsonUtils {
coPersonRole.addProperty("Title", ""); coPersonRole.addProperty("Title", "");
coPersonRole.addProperty("O", "Openaire"); coPersonRole.addProperty("O", "Openaire");
coPersonRole.addProperty("Status", status); coPersonRole.addProperty("Status", status);
coPersonRole.addProperty("ValidFrom", ""); if(status.equals("Active")) {
coPersonRole.addProperty("ValidThrough", ""); coPersonRole.addProperty("ValidFrom", new Date().toString());
} else {
coPersonRole.addProperty("ValidThrough", new Date().toString());
}
coPersonRoles.add(coPersonRole); coPersonRoles.add(coPersonRole);
role.addProperty("RequestType", "CoPersonRoles"); role.addProperty("RequestType", "CoPersonRoles");
role.addProperty("Version", version); role.addProperty("Version", version);