[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)));
@ -121,7 +117,7 @@ public class RegistryService {
@Consumes(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
public Response createRole(@RequestBody Role role) {
if(calls.getCouId(role.getName()) == null) {
if (calls.getCouId(role.getName()) == null) {
calls.createRole(role);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -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.
*/
@ -185,7 +180,7 @@ public class RegistryService {
}
private Response sendEmail(JsonObject details, JsonObject email, Integer coPersonId, JsonObject invitation) {
String name = (coPersonId != null)?calls.getUserNames(coPersonId):"User";
String name = (coPersonId != null) ? calls.getUserNames(coPersonId) : "User";
String link = details.get("link").getAsString() + invitation.get("link").getAsString();
String subject = email.get("subject").getAsString();
String message = email.get("body").getAsString().
@ -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)) {
@ -319,9 +314,9 @@ public class RegistryService {
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
calls.assignMemberRole(coPersonId, couId, role);
if(verification.getType().equals("community") || verification.getType().equals("ri")) {
if (verification.getType().equals("community") || verification.getType().equals("ri")) {
Integer riCouId = calls.getCouId("ri", verification.getEntity(), false);
if(riCouId != null) {
if (riCouId != null) {
calls.assignMemberRole(coPersonId, riCouId, calls.getRoleId(coPersonId, riCouId));
verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
verificationUtils.deleteMemberVerifications(verification.getEmail(), "ri", verification.getEntity());
@ -332,7 +327,7 @@ public class RegistryService {
verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
}
if (calls.getUserAdminGroup(coPersonId, couId) == null) {
if(verification.getType().equals("community") || verification.getType().equals("ri")) {
if (verification.getType().equals("community") || verification.getType().equals("ri")) {
verificationUtils.deleteManagerVerifications(verification.getEmail(), "community", verification.getEntity());
verificationUtils.deleteManagerVerifications(verification.getEmail(), "ri", verification.getEntity());
} else {
@ -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) {
calls.removeAdminRole(coPersonId, couId);
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) {
calls.removeAdminRole(coPersonId, couId);
calls.removeMemberRole(coPersonId, couId, role);
if (couId != 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)));
@ -482,8 +478,8 @@ public class RegistryService {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id,false);
if(couId != null) {
Integer couId = calls.getCouId(type, id, false);
if (couId != null) {
JsonArray members = calls.getUserNamesByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -501,7 +497,7 @@ public class RegistryService {
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id, false);
if(couId != null) {
if (couId != null) {
JsonArray members = calls.getUserEmailByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -519,7 +515,7 @@ public class RegistryService {
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getMembersId(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id, false);
if(couId != null) {
if (couId != null) {
JsonArray members = calls.getUserIdByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -536,7 +532,7 @@ public class RegistryService {
public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id, false);
int count = 0;
if(couId != null) {
if (couId != null) {
count = calls.getUserNamesByCouId(couId, false).size();
}
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
@ -550,7 +546,7 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON)
public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
if(couId != null) {
if (couId != null) {
JsonArray managers = calls.getUserNamesByCouId(couId, true);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -566,7 +562,7 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON)
public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
if(couId != null) {
if (couId != null) {
JsonArray managers = calls.getUserEmailByCouId(couId, true);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
@ -582,7 +578,7 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON)
public Response getManagersId(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
if(couId != null) {
if (couId != null) {
JsonArray managers = calls.getUserIdByCouId(couId, true);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
} else {

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());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
emails.add(user);
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,7 +346,9 @@ public class RegistryCalls {
* 16. 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"));
if(id != null) {
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
}
}
/**