Merge branch 'master' into production

This commit is contained in:
Konstantinos Triantafyllou 2023-02-01 21:20:44 +02:00
commit 484bd63395
7 changed files with 67 additions and 48 deletions

View File

@ -51,13 +51,14 @@ public class AdminController {
}
/**
* Assign admin role to logged in user or user with @email
* If role doesn't exists or user is not a member of this group already, use force=true to create and assign both roles.
* Assign admin role to logged-in user or user with @email
* If role doesn't exist or user is not a member of this group already, use force=true to create and assign both roles.
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
@RequestParam(required = false) String identifier,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
if (coPersonIds.size() > 0) {
Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (temp != null || force) {
@ -84,7 +85,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) {
@ -98,18 +99,19 @@ public class AdminController {
}
/**
* Remove admin role from logged in user or user with @email
* Remove admin role from logged-in user or user with @email
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id,
@RequestParam(required = false) String identifier,
@RequestParam(required = false) String email) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
coPersonIds.forEach(coPersonId -> {
registryService.removeAdminRole(coPersonId, couId);
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
authoritiesUpdater.removeRole(registryService.getIdentifierByCoPersonId(coPersonId), new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
});
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
}

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

@ -90,22 +90,22 @@ public class MemberController {
}
/**
* Assign member role to logged in user or user with @email
* Assign member role to logged-in user or user with @email
* If role doesn't exist, use force=true to create and assign the role
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id,
@RequestParam(required = false) String identifier,
@RequestParam(required = false) String email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
if (coPersonIds.size() > 0) {
Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (temp != null || force) {
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);
authoritiesUpdater.addRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
registryService.assignMemberRole(coPersonId, couId);
authoritiesUpdater.addRole(registryService.getIdentifierByCoPersonId(coPersonId), new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
});
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
}
@ -115,27 +115,29 @@ public class MemberController {
}
/**
* Remove member role from logged in user or user with @email
* Remove member role from logged-in user or user with @email
* If user is an admin of this group, use force=true to revoke both roles
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email,
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id,
@RequestParam(required = false) String identifier,
@RequestParam(required = false) String email,
@RequestParam(value = "force", defaultValue = "false") boolean force) {
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email, identifier);
if (coPersonIds.size() > 0) {
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
// If none of the accounts has admin role for this group remove member role
if (force || coPersonIds.stream().noneMatch(coPersonId -> registryService.getUserAdminGroup(coPersonId, couId) != null)) {
coPersonIds.forEach(coPersonId -> {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
String aaiIdentifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
if (force) {
registryService.removeAdminRole(coPersonId, couId);
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
authoritiesUpdater.removeRole(aaiIdentifier, new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id)));
}
registryService.removeMemberRole(coPersonId, couId, role);
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
authoritiesUpdater.removeRole(aaiIdentifier, new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
});
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
}

View File

@ -1,6 +1,7 @@
package eu.dnetlib.dnetrolemanagement.controllers;
import eu.dnetlib.dnetrolemanagement.entities.Response;
import eu.dnetlib.dnetrolemanagement.entities.User;
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/super")
@ -59,8 +61,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"));
@ -83,7 +84,7 @@ public class SuperAdminController {
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.removeMemberRole(coPersonId, couId, role);
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.portalAdminRole()));
authoritiesUpdater.removeRole(identifier, new SimpleGrantedAuthority(AuthoritiesUtils.PORTAL_ADMIN));
});
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
}
@ -91,4 +92,15 @@ public class SuperAdminController {
}
throw new ResourceNotFoundException("User has not been found");
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ResponseEntity<List<User>> getUsersByEmail(@RequestParam String email) {
return ResponseEntity.ok(registryService.getCoPersonIdsByEmail(email).stream().map(id -> {
User user = new User();
user.setEmail(registryService.getUserEmail(id));
user.setId(registryService.getUserId(id));
user.setName(registryService.getUserNames(id));
return user;
}).collect(Collectors.toList()));
}
}

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.logging.log4j.LogManager;
@ -20,15 +19,12 @@ public class RegistryService {
private static final Logger logger = LogManager.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;
}
@ -36,11 +32,10 @@ public class RegistryService {
/**
* 1.1 Get CoPersonId by Email
*/
public List<Integer> getCoPersonIdsByEmail(String email) {
public List<Integer> getCoPersonIdsByEmail(String email, String identifier) {
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) {
@ -51,11 +46,15 @@ public class RegistryService {
}
return coPersonIds;
} else {
Integer coPersonId = getCoPersonIdByIdentifier();
Integer coPersonId = identifier != null ? getCoPersonIdByIdentifier(identifier): getCoPersonIdByIdentifier();
return (coPersonId != null) ? Collections.singletonList(coPersonId) : new ArrayList<>();
}
}
public List<Integer> getCoPersonIdsByEmail(String email) {
return getCoPersonIdsByEmail(email, null);
}
/**
* 1.2 Get CoPersonId by AAI identifier
*/
@ -72,7 +71,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;
@ -98,7 +96,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());
}
@ -187,7 +184,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();
@ -286,12 +282,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

@ -11,6 +11,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
@ -29,7 +30,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;
@ -51,7 +52,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) {
@ -73,6 +74,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);