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);
if (role != null || force) {
if (role == null) {
registryService.assignMemberRole(coPersonId, couId, role);
registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
}
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), "");
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.curator(type)));
});
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), "");
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
});
return ResponseEntity.ok(new Response("Role has been assigned successfully"));

View File

@ -59,8 +59,7 @@ public class SuperAdminController {
if (couId != null) {
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.PORTAL_ADMIN));
});
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.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;
import org.apache.log4j.Logger;
@ -19,15 +18,12 @@ public class RegistryService {
private static final Logger logger = Logger.getLogger(RegistryService.class);
private final String coid;
public HttpUtils httpUtils;
public JsonUtils jsonUtils;
@Autowired
public RegistryService(HttpUtils httpUtils, JsonUtils jsonUtils, RegistryProperties registryProperties) {
this.coid = registryProperties.getCoid();
public RegistryService(HttpUtils httpUtils, JsonUtils jsonUtils) {
this.httpUtils = httpUtils;
this.jsonUtils = jsonUtils;
}
@ -39,7 +35,6 @@ public class RegistryService {
if (email != null) {
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) {
@ -71,7 +66,6 @@ public class RegistryService {
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;
@ -97,7 +91,6 @@ public class RegistryService {
*/
public JsonArray getCous(String name) {
Map<String, String> params = new HashMap<>();
params.put("coid", coid);
if (name != null) {
params.put("name", name.toLowerCase());
}
@ -186,7 +179,6 @@ public class RegistryService {
*/
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();
@ -285,12 +277,8 @@ public class RegistryService {
/**
* 15. 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 + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
} else {
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
}
public void assignMemberRole(Integer coPersonId, Integer couId) {
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 java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
@Component
@ -32,7 +33,7 @@ public class HttpUtils {
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
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());
} else {
return null;
@ -54,7 +55,7 @@ public class HttpUtils {
public JsonElement get(String path, Map<String, String> params) {
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
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(registryProperties.getUser(), registryProperties.getPassword())), String.class);
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) {
StringBuilder ret = new StringBuilder("?");

View File

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