[Users | Trunk]: Remove duplicates emails from return lists. Remove role from duplicates accounts

This commit is contained in:
Konstantinos Triantafyllou 2021-03-24 10:08:19 +00:00
parent f26ea5a8fd
commit 2cc14ea7d7
2 changed files with 65 additions and 40 deletions

View File

@ -28,6 +28,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@Component(value = "RegistryService")
@Path("/registry")
@ -53,11 +54,6 @@ public class RegistryService {
@Autowired
private AuthorizationService authorizationService;
private String sendEmail() {
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
return authenticationToken.getUserInfo().getEmail();
}
/**
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
*/
@ -71,7 +67,7 @@ public class RegistryService {
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
calls.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(sendEmail(), old -> {
authoritiesUpdater.update(authorizationService.getEmail(), old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
return authorities;
@ -98,7 +94,7 @@ public class RegistryService {
if (role != null) {
calls.removeAdminRole(coPersonId, couId);
calls.removeMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(sendEmail(), old -> {
authoritiesUpdater.update(authorizationService.getEmail(), old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
@ -130,7 +126,6 @@ public class RegistryService {
}
/**
*
* Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
* Auto generated link and code will be sent as response.
*/
@ -274,7 +269,7 @@ public class RegistryService {
public Response getVerification(@PathParam("id") String id) {
RoleVerification verification = verificationUtils.getVerification(id);
if (verification != null) {
if (calls.getCoPersonIdByEmail(verification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
@ -311,7 +306,7 @@ public class RegistryService {
public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
RoleVerification verification = verificationUtils.getVerification(id);
if (verification != null && verification.getVerificationType().equals("manager")) {
Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
Integer coPersonId = calls.getCoPersonIdByIdentifier();
if (coPersonId != null) {
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
if (verification.getVerificationCode().equals(code)) {
@ -377,7 +372,7 @@ public class RegistryService {
public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
RoleVerification verification = verificationUtils.getVerification(id);
if (verification != null && verification.getVerificationType().equals("member")) {
Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
Integer coPersonId = calls.getCoPersonIdByIdentifier();
if (coPersonId != null) {
if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
if (verification.getVerificationCode().equals(code)) {
@ -419,11 +414,13 @@ public class RegistryService {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
id, @PathParam("email") String email) {
Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (coPersonId != null) {
List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
coPersonIds.forEach(coPersonId -> {
calls.removeAdminRole(coPersonId, couId);
});
authoritiesUpdater.update(email, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
@ -448,16 +445,15 @@ public class RegistryService {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
id, @PathParam("email") String email) {
Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (coPersonId != null) {
List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
if (coPersonIds.size() > 0) {
Integer couId = calls.getCouId(type, id, false);
Integer role = null;
if (couId != null) {
role = calls.getRoleId(coPersonId, couId);
}
if (couId != null && role != null) {
coPersonIds.forEach(coPersonId -> {
Integer role = calls.getRoleId(coPersonId, couId);
calls.removeAdminRole(coPersonId, couId);
calls.removeMemberRole(coPersonId, couId, role);
});
authoritiesUpdater.update(email, old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));

View File

@ -8,10 +8,13 @@ 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.access.method.P;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@ -70,6 +73,21 @@ public class RegistryCalls {
return null;
}
public List<Integer> getCoPersonIdsByEmail(String email) {
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());
}
}
return coPersonIds;
}
/**
* 2. Get CoPersonId by AAI identifier
*/
@ -255,9 +273,18 @@ public class RegistryCalls {
JsonArray emails = new JsonArray();
infos.forEach(info -> {
JsonObject user = new JsonObject();
user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
boolean add = true;
String email = info.getAsJsonObject().get("Mail").getAsString();
for(JsonElement element : emails) {
if(element.getAsJsonObject().get("email").getAsString().equals(email)) {
add = false;
}
}
if(add) {
user.addProperty("email", email);
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
emails.add(user);
}
});
return emails;
}
@ -319,8 +346,10 @@ public class RegistryCalls {
* 16. Remove a member role from a User
*/
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
if(id != null) {
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
}
}
/**
* 17. Create a new role