[Users | Trunk]: Add methods for: 1. Get invited users. 2. Delete a verification. 3. Cancel invitation for a user.

This commit is contained in:
Konstantinos Triantafyllou 2020-08-10 11:30:25 +00:00
parent d909d6610a
commit 18f34014d9
3 changed files with 95 additions and 13 deletions

View File

@ -33,8 +33,6 @@ public class RegistryService {
@Autowired
private VerificationUtils verificationUtils;
/**
* Subscribe to type(Community, etc.) with id(ee, egi, etc.)
*
@ -114,6 +112,39 @@ public class RegistryService {
}
}
/**
* Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
*
* */
@Path("/invite/{type}/{id}/manager/{email}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response cancelUserInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
verificationUtils.deleteUserVerifications(email, type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
}
/**
* Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
*
* */
@Path("/invite/{type}/{id}/manager/")
@GET
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
JsonArray invited = verificationUtils.getInvitedUsers(type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
}
/**
* Get the verification with a specific id only if it refers to the logged in user
*
@ -135,6 +166,24 @@ public class RegistryService {
}
}
/**
* Delete the verification with a specific id.
*
* */
@Path("verification/{id}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN," +
"@AuthoritiesService.PORTAL_ADMIN, @AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response deleteVerification(@PathParam("id") String id) {
if (verificationUtils.getVerification(id) != null) {
verificationUtils.deleteVerification(id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
}
}
/**
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
* Manager role is assigned to this user, along with the member role.
@ -151,7 +200,7 @@ public class RegistryService {
if (coPersonId != null) {
if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
if (managerVerification.getVerificationCode().equals(code)) {
verificationUtils.deleteVerification(managerVerification.getId());
verificationUtils.deleteRelatedVerifications(managerVerification);
Integer couId = calls.getCouId(managerVerification.getType(), managerVerification.getEntity());
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
@ -212,8 +261,8 @@ public class RegistryService {
@Path("/{type}/{id}/subscribers")
@GET
@Produces(MediaType.APPLICATION_JSON)
/* @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")*/
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response getSubscribers(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
JsonArray subscribers = calls.getUserNamesByCouId(couId, false);
@ -227,8 +276,8 @@ public class RegistryService {
@Path("/{type}/{id}/subscribers/email")
@GET
@Produces(MediaType.APPLICATION_JSON)
/* @PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")*/
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.PORTAL_ADMIN," +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response getSubscribersEmail(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id);
JsonArray subscribers = calls.getUserEmailByCouId(couId, false);

View File

@ -5,8 +5,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO;
import eu.dnetlib.openaire.user.ldap.MUserActionsLDAP;
import eu.dnetlib.openaire.user.login.utils.AuthoritiesMapper;
import eu.dnetlib.openaire.user.pojos.migration.LDAPUser;
import eu.dnetlib.openaire.user.store.DataSourceConnector;
import org.apache.commons.io.IOUtils;
@ -124,11 +126,19 @@ public class Test3Service {
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer " + accessToken);
HttpEntity<String> request = new HttpEntity<>(headers);
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class));
ResponseEntity<String> response = restTemplate.exchange(issuer +"userinfo", HttpMethod.GET, request, String.class);
return Response.status(response.getStatusCode().value()).entity(response.getBody()).type(MediaType.APPLICATION_JSON).build();
if(response.getStatusCode() == HttpStatus.OK) {
JsonObject userInfo = new JsonParser().parse(response.getBody()).getAsJsonObject();
JsonArray roles = new JsonArray();
AuthoritiesMapper.map(userInfo.get("edu_person_entitlements").getAsJsonArray()).forEach(grantedAuthority -> {
roles.add(grantedAuthority.getAuthority());
});
userInfo.add("roles", roles);
return Response.status(response.getStatusCode().value()).entity(userInfo.toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(response.getStatusCode().value()).entity(response.getBody()).type(MediaType.APPLICATION_JSON).build();
}
}
@ -159,7 +169,7 @@ public class Test3Service {
userInfoJson.addProperty("email", userInfo.getEmail());
JsonArray roles = new JsonArray();
authentication.getAuthorities().forEach(grantedAuthority -> {
roles.add(grantedAuthority.toString());
roles.add(grantedAuthority.getAuthority());
});
userInfoJson.add("roles", roles);
}catch (Exception e){

View File

@ -1,5 +1,6 @@
package eu.dnetlib.openaire.usermanagement.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import eu.dnetlib.openaire.user.pojos.ManagerVerification;
import eu.dnetlib.openaire.user.utils.ManagerVerificationActions;
@ -9,8 +10,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;
import java.util.*;
@Component
@ -33,10 +33,33 @@ public class VerificationUtils {
return invitation;
}
public void deleteRelatedVerifications(ManagerVerification managerVerification) {
List<ManagerVerification> related = actions.
getUserVerificationsForAnEntity(managerVerification.getEmail(), managerVerification.getType(), managerVerification.getEntity());
for(ManagerVerification verification : related) {
deleteVerification(verification.getId());
}
}
public void deleteUserVerifications(String email, String type, String entity) {
List<ManagerVerification> managerVerifications = actions.
getUserVerificationsForAnEntity(email, type, entity);
for(ManagerVerification verification : managerVerifications) {
deleteVerification(verification.getId());
}
}
public void deleteVerification(String id) {
actions.deleteVerificationEntry(id);
}
public JsonArray getInvitedUsers(String type, String id) {
List<String> emails = actions.getVerificationsForAnEntity(type, id);
JsonArray users = new JsonArray();
emails.forEach(users::add);
return users;
}
public ManagerVerification getVerification(String id) {
return actions.getManagerVerification(id);
}