[Dnet-Users | Trunk]: Add member invitation methods

This commit is contained in:
Konstantinos Triantafyllou 2020-10-05 13:09:34 +00:00
parent f33a5b3e2d
commit 50de8755e6
5 changed files with 238 additions and 71 deletions

View File

@ -3,7 +3,7 @@ package eu.dnetlib.openaire.usermanagement.api;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import eu.dnetlib.openaire.user.pojos.ManagerVerification; import eu.dnetlib.openaire.user.pojos.RoleVerification;
import eu.dnetlib.openaire.user.utils.EmailSender; import eu.dnetlib.openaire.user.utils.EmailSender;
import eu.dnetlib.openaire.usermanagement.dto.Role; import eu.dnetlib.openaire.usermanagement.dto.Role;
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils; import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
@ -46,7 +46,7 @@ public class RegistryService {
@Path("/subscribe/{type}/{id}") @Path("/subscribe/{type}/{id}")
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) { public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
Integer coPersonId = calls.getCoPersonIdByIdentifier(); Integer coPersonId = calls.getCoPersonIdByIdentifier();
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
@ -60,13 +60,13 @@ public class RegistryService {
} }
/** /**
* Subscribe from type(Community, etc.) with id(ee, egi, etc.). * Unsubscribe from type(Community, etc.) with id(ee, egi, etc.).
* If user has manager role for this entity, it will be removed too. * If user has manager role for this entity, it will be removed too.
*/ */
@Path("/unsubscribe/{type}/{id}") @Path("/unsubscribe/{type}/{id}")
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) { public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) {
Integer coPersonId = calls.getCoPersonIdByIdentifier(); Integer coPersonId = calls.getCoPersonIdByIdentifier();
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
@ -105,17 +105,17 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " + @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) { public Response inviteManager(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
if (couId != null) { if (couId != null) {
Integer coPersonId = calls.getCoPersonIdByEmail(email); Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (calls.getUserAdminGroup(coPersonId, couId) == null) { if (coPersonId == null || calls.getUserAdminGroup(coPersonId, couId) == null) {
JsonObject invitation = verificationUtils.createInvitation(email, type, id); JsonObject invitation = verificationUtils.createManagerInvitation(email, type, id);
String name = calls.getUserNames(coPersonId); String name = (coPersonId != null)?calls.getUserNames(coPersonId):null;
JsonObject details = new JsonParser().parse(body).getAsJsonObject(); JsonObject details = new JsonParser().parse(body).getAsJsonObject();
String link = details.get("link").getAsString() + "/" + invitation.get("link").getAsString(); String link = details.get("link").getAsString() + invitation.get("link").getAsString();
String subject = "Invite to manage " + details.get("name").getAsString(); String subject = "Invite to manage " + details.get("name").getAsString();
String message = "<p>Hello " + name + ",</p>" + String message = "<p>Hello" + ((name != null)?(" " + name):"") + ",</p>" +
"<p> You have been invited to manage " + details.get("name").getAsString() + ". " + "<p> You have been invited to manage " + details.get("name").getAsString() + ". " +
"Use the verification code below to accept the invitation." + "Use the verification code below to accept the invitation." +
"</p>" + "</p>" +
@ -142,6 +142,52 @@ public class RegistryService {
} }
} }
/**
* Invite user with email to be a member of a type(Community, etc.) with id(ee, egi, etc.)
* Auto generated link and code will be sent as response.
*/
@Path("/invite/{type}/{id}/member/{email}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (coPersonId == null || calls.getRoleId(coPersonId, couId) == null) {
JsonObject invitation = verificationUtils.createMemberInvitation(email, type, id);
String name = (coPersonId != null)?calls.getUserNames(coPersonId):null;
JsonObject details = new JsonParser().parse(body).getAsJsonObject();
String link = details.get("link").getAsString() + invitation.get("link").getAsString();
String subject = "Invite to be a member of " + details.get("name").getAsString();
String message = "<p>Hello" + ((name != null)?(" " + name):"") + ",</p>" +
"<p> You have been invited to be a member of " + details.get("name").getAsString() + ". " +
"Use the verification code below to accept the invitation." +
"</p>" +
"<p>" +
"The verification code is " + invitation.get("code").getAsString() +
"</p>" +
"Click the URL below and proceed with the process." +
"<p><a href=" + link + ">" + link + "</a></p>" +
"<p>Thank you,</p>" +
"<p>OpenAIRE technical team</p>";
try {
emailSender.sendEmail(email, subject, message);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
} catch (MessagingException e) {
logger.error(e.getMessage());
verificationUtils.deleteVerification(invitation.get("link").getAsString());
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already member of this " + type).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();
}
}
/** /**
* Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.) * Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
*/ */
@ -150,10 +196,28 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " + @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response cancelUserInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) { public Response cancelManagerInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
if (couId != null) { if (couId != null) {
verificationUtils.deleteUserVerifications(email, type, id); verificationUtils.deleteManagerVerifications(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();
}
}
/**
* Cancel invitation to user with email for being member of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/invite/{type}/{id}/member/{email}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
Integer couId = calls.getCouId(type, id);
if (couId != null) {
verificationUtils.deleteMemberVerifications(email, type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
} else { } else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
@ -169,7 +233,20 @@ public class RegistryService {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " + @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) { public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
JsonArray invited = verificationUtils.getInvitedUsers(type, id); JsonArray invited = verificationUtils.getInvitedManagers(type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
}
/**
* Get the invited members for a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/invite/{type}/{id}/members/")
@GET
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getInviteMembers(@PathParam("type") String type, @PathParam("id") String id) {
JsonArray invited = verificationUtils.getInvitedMembers(type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
} }
@ -181,10 +258,10 @@ public class RegistryService {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public Response getVerification(@PathParam("id") String id) { public Response getVerification(@PathParam("id") String id) {
ManagerVerification managerVerification = verificationUtils.getVerification(id); RoleVerification verification = verificationUtils.getVerification(id);
if (managerVerification != null) { if (verification != null) {
if (calls.getCoPersonIdByEmail(managerVerification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) { if (calls.getCoPersonIdByEmail(verification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(managerVerification)).toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
} else { } else {
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
} }
@ -213,23 +290,24 @@ public class RegistryService {
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user. * 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. * Manager role is assigned to this user, along with the member role.
*/ */
@Path("verification/{id}") @Path("verification/manager/{id}")
@POST @POST
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
public Response verify(@PathParam("id") String id, @RequestBody String code) { public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
ManagerVerification managerVerification = verificationUtils.getVerification(id); RoleVerification verification = verificationUtils.getVerification(id);
if (managerVerification != null) { if (verification != null && verification.getVerificationType().equals("manager")) {
Integer coPersonId = calls.getCoPersonIdByEmail(managerVerification.getEmail()); Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
if (coPersonId != null) { if (coPersonId != null) {
if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) { if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
if (managerVerification.getVerificationCode().equals(code)) { if (verification.getVerificationCode().equals(code)) {
verificationUtils.deleteRelatedVerifications(managerVerification); Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
Integer couId = calls.getCouId(managerVerification.getType(), managerVerification.getEntity());
if (couId != null) { if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId); Integer role = calls.getRoleId(coPersonId, couId);
calls.assignMemberRole(coPersonId, couId, role); calls.assignMemberRole(coPersonId, couId, role);
verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
if (calls.getUserAdminGroup(coPersonId, couId) == null) { if (calls.getUserAdminGroup(coPersonId, couId) == null) {
verificationUtils.deleteManagerVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
calls.assignAdminRole(coPersonId, couId); calls.assignAdminRole(coPersonId, couId);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
} else { } else {
@ -252,6 +330,44 @@ public class RegistryService {
} }
} }
/**
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
* Member role is assigned to this user, along with the member role.
*/
@Path("verification/member/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("isAuthenticated()")
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());
if (coPersonId != null) {
if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
if (verification.getVerificationCode().equals(code)) {
Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
calls.assignMemberRole(coPersonId, couId, role);
verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").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();
}
} else {
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
}
/** /**
* Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.) * Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
*/ */
@ -278,40 +394,67 @@ public class RegistryService {
} }
/** /**
* Get the names of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
*/ */
@Path("/{type}/{id}/subscribers") @Path("/{type}/{id}/member/{email}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN," +
"@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) {
Integer couId = calls.getCouId(type, id);
Integer role = calls.getRoleId(coPersonId, couId);
if (couId != null && role != null) {
calls.removeAdminRole(coPersonId, couId);
calls.removeMemberRole(coPersonId, couId, role);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").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();
}
} else {
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
}
/**
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@Path("/{type}/{id}/members")
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," + @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getSubscribers(@PathParam("type") String type, @PathParam("id") String id) { public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
JsonArray subscribers = calls.getUserNamesByCouId(couId, false); JsonArray subscribers = calls.getUserNamesByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
} }
/** /**
* Get the emails of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) * Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/ */
@Path("/{type}/{id}/subscribers/email") @Path("/{type}/{id}/members/email")
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," + @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," +
"@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))") "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
public Response getSubscribersEmail(@PathParam("type") String type, @PathParam("id") String id) { public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
JsonArray subscribers = calls.getUserEmailByCouId(couId, false); JsonArray subscribers = calls.getUserEmailByCouId(couId, false);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
} }
/** /**
* Get the number of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/ */
@Path("/{type}/{id}/subscribers/count") @Path("/{type}/{id}/members/count")
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getSubscribersCount(@PathParam("type") String type, @PathParam("id") String id) { public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
Integer couId = calls.getCouId(type, id); Integer couId = calls.getCouId(type, id);
int count = calls.getUserNamesByCouId(couId, false).size(); int count = calls.getUserNamesByCouId(couId, false).size();
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();

View File

@ -34,4 +34,8 @@ public class AuthorizationService {
public String member(String type, String id) { public String member(String type, String id) {
return type.toUpperCase() + "_" + id.toUpperCase(); return type.toUpperCase() + "_" + id.toUpperCase();
} }
public boolean isCommunity(String type) {
return type.equals("community");
}
} }

View File

@ -3,14 +3,11 @@ package eu.dnetlib.openaire.usermanagement.utils;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import eu.dnetlib.openaire.user.pojos.ManagerVerification; import eu.dnetlib.openaire.user.pojos.RoleVerification;
import eu.dnetlib.openaire.usermanagement.dto.Role; import eu.dnetlib.openaire.usermanagement.dto.Role;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.sql.Timestamp;
import java.util.Date;
@Component @Component
public class JsonUtils { public class JsonUtils {
@ -79,13 +76,14 @@ public class JsonUtils {
return cou; return cou;
} }
public JsonObject createVerification(ManagerVerification managerVerification) { public JsonObject createVerification(RoleVerification roleVerification) {
JsonObject verification = new JsonObject(); JsonObject verification = new JsonObject();
verification.addProperty("id", managerVerification.getId()); verification.addProperty("id", roleVerification.getId());
verification.addProperty("email", managerVerification.getEmail()); verification.addProperty("email", roleVerification.getEmail());
verification.addProperty("type", managerVerification.getType()); verification.addProperty("type", roleVerification.getType());
verification.addProperty("entity", managerVerification.getEntity()); verification.addProperty("entity", roleVerification.getEntity());
verification.addProperty("date", managerVerification.getDate().getTime()); verification.addProperty("verificationType", roleVerification.getVerificationType());
verification.addProperty("date", roleVerification.getDate().getTime());
return verification; return verification;
} }

View File

@ -125,7 +125,7 @@ public class RegistryCalls {
JsonArray roles = getRoles(coPersonId); JsonArray roles = getRoles(coPersonId);
for (JsonElement role : roles) { for (JsonElement role : roles) {
JsonObject object = role.getAsJsonObject(); JsonObject object = role.getAsJsonObject();
if (object.get("CouId").getAsInt() == couId) { if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
return object.get("Id").getAsInt(); return object.get("Id").getAsInt();
} }
} }

View File

@ -2,18 +2,18 @@ package eu.dnetlib.openaire.usermanagement.utils;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import eu.dnetlib.openaire.user.pojos.ManagerVerification; import eu.dnetlib.openaire.user.pojos.RoleVerification;
import eu.dnetlib.openaire.user.utils.ManagerVerificationActions; import eu.dnetlib.openaire.user.utils.ManagerVerificationActions;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.*; import java.util.Date;
import java.util.List;
import java.util.Random;
@Component("VerificationUtils") @Component("VerificationUtils")
@ -25,56 +25,78 @@ public class VerificationUtils {
@Autowired @Autowired
private ManagerVerificationActions actions; private ManagerVerificationActions actions;
public JsonObject createInvitation(String email, String type, String entity) { public JsonObject createManagerInvitation(String email, String type, String entity) {
RoleVerification roleVerification = actions.getManagerVerification(email, type, entity);
if(roleVerification == null) {
String id; String id;
do { do {
id = createId(); id = createId();
} while (exists(id)); } while (exists(id));
ManagerVerification managerVerification = actions.addVerificationEntry(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime())); roleVerification = actions.addManagerVerification(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime()));
}
JsonObject invitation = new JsonObject(); JsonObject invitation = new JsonObject();
invitation.addProperty("link", managerVerification.getId()); invitation.addProperty("link", roleVerification.getId());
invitation.addProperty("code", managerVerification.getVerificationCode()); invitation.addProperty("code", roleVerification.getVerificationCode());
return invitation; return invitation;
} }
public void deleteRelatedVerifications(ManagerVerification managerVerification) { public JsonObject createMemberInvitation(String email, String type, String entity) {
List<ManagerVerification> related = actions. String id;
getUserVerificationsForAnEntity(managerVerification.getEmail(), managerVerification.getType(), managerVerification.getEntity()); do {
for (ManagerVerification verification : related) { id = createId();
deleteVerification(verification.getId()); } while (exists(id));
RoleVerification roleVerification = actions.getMemberVerification(email, type, entity);
if(roleVerification == null) {
roleVerification = actions.addMemberVerification(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime()));
} JsonObject invitation = new JsonObject();
invitation.addProperty("link", roleVerification.getId());
invitation.addProperty("code", roleVerification.getVerificationCode());
return invitation;
}
public void deleteManagerVerifications(String email, String type, String entity) {
RoleVerification roleVerification = actions.getManagerVerification(email, type, entity);
if (roleVerification != null) {
deleteVerification(roleVerification.getId());
} }
} }
public void deleteUserVerifications(String email, String type, String entity) { public void deleteMemberVerifications(String email, String type, String entity) {
List<ManagerVerification> managerVerifications = actions. RoleVerification roleVerification = actions.getMemberVerification(email, type, entity);
getUserVerificationsForAnEntity(email, type, entity); if (roleVerification != null) {
for (ManagerVerification verification : managerVerifications) { deleteVerification(roleVerification.getId());
deleteVerification(verification.getId());
} }
} }
public void deleteVerification(String id) { public void deleteVerification(String id) {
actions.deleteVerificationEntry(id); actions.delete(id);
} }
public JsonArray getInvitedUsers(String type, String id) { public JsonArray getInvitedManagers(String type, String id) {
List<String> emails = actions.getVerificationsForAnEntity(type, id); List<String> emails = actions.getInvitedManagers(type, id);
JsonArray users = new JsonArray(); JsonArray users = new JsonArray();
emails.forEach(users::add); emails.forEach(users::add);
return users; return users;
} }
public ManagerVerification getVerification(String id) { public JsonArray getInvitedMembers(String type, String id) {
return actions.getManagerVerification(id); List<String> emails = actions.getInviteMembers(type, id);
JsonArray users = new JsonArray();
emails.forEach(users::add);
return users;
}
public RoleVerification getVerification(String id) {
return actions.get(id);
} }
public boolean exists(String id) { public boolean exists(String id) {
return actions.verificationEntryExists(id); return actions.exists(id);
} }
public boolean ownedVerification(String id) { public boolean ownedVerification(String id) {
try { try {
ManagerVerification managerVerification = getVerification(id); RoleVerification managerVerification = getVerification(id);
if (managerVerification != null) { if (managerVerification != null) {
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getUserInfo().getEmail().toLowerCase(); String email = authentication.getUserInfo().getEmail().toLowerCase();