Add email sent on manager invitation

This commit is contained in:
Konstantinos Triantafyllou 2020-09-03 19:45:38 +00:00
parent b1dd3ba21d
commit 35020bbf95
1 changed files with 46 additions and 32 deletions

View File

@ -2,7 +2,9 @@ package eu.dnetlib.openaire.usermanagement.api;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import eu.dnetlib.openaire.user.pojos.ManagerVerification;
import eu.dnetlib.openaire.user.utils.EmailSender;
import eu.dnetlib.openaire.usermanagement.dto.Role;
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
@ -14,6 +16,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import javax.mail.MessagingException;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ -30,13 +33,16 @@ public class RegistryService {
@Autowired
private JsonUtils jsonUtils;
@Autowired
private EmailSender emailSender;
@Autowired
private VerificationUtils verificationUtils;
/**
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/subscribe/{type}/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@ -56,8 +62,7 @@ public class RegistryService {
/**
* Subscribe from type(Community, etc.) with id(ee, egi, etc.).
* If user has manager role for this entity, it will be removed too.
*
* */
*/
@Path("/unsubscribe/{type}/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@ -80,7 +85,6 @@ public class RegistryService {
/**
* Create a new role with the given name and description.
*
**/
@Path("/createRole")
@POST
@ -95,20 +99,41 @@ 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.
*
* */
*/
@Path("/invite/{type}/{id}/manager/{email}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " +
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))")
public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
public Response inviteUser(@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(calls.getUserAdminGroup(coPersonId, couId) == null) {
if (calls.getUserAdminGroup(coPersonId, couId) == null) {
JsonObject invitation = verificationUtils.createInvitation(email, type, id);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
String name = calls.getUserNames(coPersonId);
JsonObject details = new JsonParser().parse(body).getAsJsonObject();
String link = details.get("link").getAsString() + "/" + invitation.get("link").getAsString();
String subject = "Invite to manage " + details.get("name").getAsString();
String message = "<p>Hello " + name + ",</p>" +
"<p> You have been invited to manage " + 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 manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
}
@ -119,8 +144,7 @@ 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)
@ -138,8 +162,7 @@ public class RegistryService {
/**
* Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/invite/{type}/{id}/managers/")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -152,8 +175,7 @@ public class RegistryService {
/**
* Get the verification with a specific id only if it refers to the logged in user
*
* */
*/
@Path("verification/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -173,8 +195,7 @@ public class RegistryService {
/**
* Delete the verification with a specific id.
*
* */
*/
@Path("verification/{id}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@ -191,8 +212,7 @@ public class RegistryService {
/**
* 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.
*
* */
*/
@Path("verification/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@ -234,8 +254,7 @@ public class RegistryService {
/**
* Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/manager/{email}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@ -260,8 +279,7 @@ public class RegistryService {
/**
* Get the names of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/subscribers")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -275,8 +293,7 @@ public class RegistryService {
/**
* Get the emails of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/subscribers/email")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -290,8 +307,7 @@ public class RegistryService {
/**
* Get the number of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/subscribers/count")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -303,8 +319,7 @@ public class RegistryService {
/**
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/managers")
@GET
@Produces(MediaType.APPLICATION_JSON)
@ -316,8 +331,7 @@ public class RegistryService {
/**
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*
* */
*/
@Path("/{type}/{id}/managers/email")
@GET
@Produces(MediaType.APPLICATION_JSON)