2020-08-04 18:18:08 +02:00
package eu.dnetlib.openaire.usermanagement.api ;
2021-10-08 16:45:31 +02:00
import com.google.gson.JsonArray ;
import com.google.gson.JsonElement ;
import com.google.gson.JsonObject ;
import com.google.gson.JsonParser ;
2020-10-05 15:09:34 +02:00
import eu.dnetlib.openaire.user.pojos.RoleVerification ;
2020-09-03 21:45:38 +02:00
import eu.dnetlib.openaire.user.utils.EmailSender ;
2020-11-14 11:17:54 +01:00
import eu.dnetlib.openaire.usermanagement.utils.AuthorizationService ;
2020-08-04 18:18:08 +02:00
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils ;
2021-10-08 16:45:31 +02:00
import eu.dnetlib.openaire.usermanagement.utils.RoleManagement ;
2020-08-04 18:18:08 +02:00
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils ;
import org.apache.log4j.Logger ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.stereotype.Component ;
2022-07-27 16:41:36 +02:00
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.RequestBody ;
2021-10-08 16:45:31 +02:00
import org.springframework.web.client.HttpClientErrorException ;
2020-08-04 18:18:08 +02:00
2020-09-03 21:45:38 +02:00
import javax.mail.MessagingException ;
2021-10-08 16:45:31 +02:00
import javax.servlet.http.HttpServletRequest ;
2020-08-04 18:18:08 +02:00
import javax.ws.rs.* ;
2021-10-08 16:45:31 +02:00
import javax.ws.rs.core.Context ;
2020-08-04 18:18:08 +02:00
import javax.ws.rs.core.MediaType ;
import javax.ws.rs.core.Response ;
@Component ( value = " RegistryService " )
2021-10-08 16:45:31 +02:00
@CrossOrigin ( " * " )
2020-08-04 18:18:08 +02:00
@Path ( " /registry " )
public class RegistryService {
private static final Logger logger = Logger . getLogger ( RegistryService . class ) ;
@Autowired
2021-10-08 16:45:31 +02:00
private RoleManagement calls ;
2020-08-04 18:18:08 +02:00
@Autowired
private JsonUtils jsonUtils ;
@Autowired
private VerificationUtils verificationUtils ;
2020-11-14 11:17:54 +01:00
@Autowired
2021-10-08 16:45:31 +02:00
private EmailSender emailSender ;
2020-11-14 11:17:54 +01:00
@Autowired
private AuthorizationService authorizationService ;
2020-08-04 18:18:08 +02:00
/ * *
2020-08-14 12:38:59 +02:00
* Subscribe to a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2020-08-04 18:18:08 +02:00
@Path ( " /subscribe/{type}/{id} " )
@POST
@Produces ( MediaType . APPLICATION_JSON )
2020-10-05 15:09:34 +02:00
@PreAuthorize ( " isAuthenticated() and @AuthorizationService.isCommunity(#type) " )
2021-10-08 16:45:31 +02:00
public Response subscribe ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @Context final HttpServletRequest request ) {
try {
JsonElement response = calls . assignMemberRole ( type , id , request ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( response . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
}
/ * *
2020-10-05 15:09:34 +02:00
* Unsubscribe from type ( Community , etc . ) with id ( ee , egi , etc . ) .
2020-08-04 18:18:08 +02:00
* If user has manager role for this entity , it will be removed too .
2020-09-03 21:45:38 +02:00
* /
2020-08-04 18:18:08 +02:00
@Path ( " /unsubscribe/{type}/{id} " )
@POST
@Produces ( MediaType . APPLICATION_JSON )
2020-10-05 15:09:34 +02:00
@PreAuthorize ( " isAuthenticated() and @AuthorizationService.isCommunity(#type) " )
2021-10-08 16:45:31 +02:00
public Response unsubscribe ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @Context final HttpServletRequest request ) {
try {
JsonElement response = calls . removeMemberRole ( type , id , request ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( response . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
}
/ * *
* Create a new curator role with the given type ( Community , etc . ) .
* * /
@Path ( " /create/{type} " )
@POST
@Produces ( MediaType . APPLICATION_JSON )
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN) " )
public Response createCuratorRole ( @PathParam ( " type " ) String type ) {
try {
JsonElement response = calls . createCuratorRole ( type ) ;
return Response . status ( HttpStatus . CREATED . value ( ) ) . entity ( response . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
}
2021-09-08 18:33:33 +02:00
/ * *
* Create a new role with the given type ( Community , etc . ) with id ( ee , egi , etc . ) .
* * /
@Path ( " /create/{type}/{id} " )
@POST
@Produces ( MediaType . APPLICATION_JSON )
2022-07-27 16:41:36 +02:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type)) " )
2021-09-08 18:33:33 +02:00
public Response createMemberRole ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
2022-07-27 16:41:36 +02:00
JsonElement element = null ;
Response response = null ;
2021-10-08 16:45:31 +02:00
try {
2022-07-27 16:41:36 +02:00
if ( type . equals ( " ri " ) | | type . equals ( " community " ) ) {
element = calls . createMemberRole ( " ri " , id ) ;
} else {
element = calls . createMemberRole ( type , id ) ;
}
2021-10-08 16:45:31 +02:00
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
2022-07-27 16:41:36 +02:00
response = Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} finally {
try {
if ( type . equals ( " ri " ) | | type . equals ( " community " ) ) {
element = calls . createMemberRole ( " community " , id ) ;
}
if ( element ! = null ) {
response = Response . status ( HttpStatus . CREATED . value ( ) ) . entity ( element . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
response = Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
2021-01-28 15:40:31 +01:00
}
2022-07-27 16:41:36 +02:00
return response ;
2020-08-04 18:18:08 +02:00
}
/ * *
* 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 .
2020-09-03 21:45:38 +02:00
* /
2020-11-19 18:15:08 +01:00
@Path ( " /invite/{type}/{id}/manager " )
2020-08-04 18:18:08 +02:00
@POST
@Produces ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-09-08 15:00:33 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-11-19 18:15:08 +01:00
public Response inviteManager ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @RequestBody String body ) {
2021-10-08 16:45:31 +02:00
try {
2020-11-19 18:15:08 +01:00
JsonObject details = new JsonParser ( ) . parse ( body ) . getAsJsonObject ( ) ;
JsonObject email = details . get ( " email " ) . getAsJsonObject ( ) ;
String recipient = email . get ( " recipient " ) . getAsString ( ) ;
2021-10-08 16:45:31 +02:00
if ( ! calls . isManager ( type , id , recipient ) ) {
2020-11-19 18:15:08 +01:00
JsonObject invitation = verificationUtils . createManagerInvitation ( recipient , type , id ) ;
2021-10-08 16:45:31 +02:00
return sendEmail ( details , email , invitation ) ;
2020-08-14 12:38:59 +02:00
} else {
2020-08-14 16:51:39 +02:00
return Response . status ( HttpStatus . CONFLICT . value ( ) ) . entity ( jsonUtils . createResponse ( " User has been already manager of this " + type ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-14 12:38:59 +02:00
}
2021-10-08 16:45:31 +02:00
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
}
2020-10-05 15:09:34 +02:00
/ * *
* 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 .
* /
2020-11-19 18:15:08 +01:00
@Path ( " /invite/{type}/{id}/member " )
2020-10-05 15:09:34 +02:00
@POST
@Produces ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-10-05 15:09:34 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-11-19 18:15:08 +01:00
public Response inviteMember ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @RequestBody String body ) {
2021-10-08 16:45:31 +02:00
try {
2020-11-19 18:15:08 +01:00
JsonObject details = new JsonParser ( ) . parse ( body ) . getAsJsonObject ( ) ;
JsonObject email = details . get ( " email " ) . getAsJsonObject ( ) ;
String recipient = email . get ( " recipient " ) . getAsString ( ) ;
2021-10-08 16:45:31 +02:00
if ( ! calls . isMember ( type , id , recipient ) ) {
2020-11-19 18:15:08 +01:00
JsonObject invitation = verificationUtils . createMemberInvitation ( recipient , type , id ) ;
2021-10-08 16:45:31 +02:00
return sendEmail ( details , email , invitation ) ;
2020-10-05 15:09:34 +02:00
} else {
return Response . status ( HttpStatus . CONFLICT . value ( ) ) . entity ( jsonUtils . createResponse ( " User has been already member of this " + type ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
2021-10-08 16:45:31 +02:00
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
}
2021-10-08 16:45:31 +02:00
private Response sendEmail ( JsonObject details , JsonObject email , JsonObject invitation ) {
2020-11-19 18:15:08 +01:00
String link = details . get ( " link " ) . getAsString ( ) + invitation . get ( " link " ) . getAsString ( ) ;
String subject = email . get ( " subject " ) . getAsString ( ) ;
String message = email . get ( " body " ) . getAsString ( ) .
2021-10-08 16:45:31 +02:00
replace ( " ((__user__)) " , " User " ) .
2020-11-19 18:15:08 +01:00
replace ( " ((__link__)) " , link ) .
replace ( " ((__code__)) " , invitation . get ( " code " ) . getAsString ( ) ) ;
try {
emailSender . sendEmail ( email . get ( " recipient " ) . getAsString ( ) , subject , message ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( invitation ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( MessagingException e ) {
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 ( ) ;
}
}
2020-08-10 13:30:25 +02:00
/ * *
* Cancel invitation to user with email for managing a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2020-08-10 13:30:25 +02:00
@Path ( " /invite/{type}/{id}/manager/{email} " )
@DELETE
@Produces ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-09-08 15:00:33 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-10-05 15:09:34 +02:00
public Response cancelManagerInvitations ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @PathParam ( " email " ) String email ) {
2021-10-08 16:45:31 +02:00
verificationUtils . deleteManagerVerifications ( email , type , id ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( " Invitations have been deleted " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
/ * *
* 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 )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-10-05 15:09:34 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
public Response cancelMemberInvitations ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id , @PathParam ( " email " ) String email ) {
2021-10-08 16:45:31 +02:00
verificationUtils . deleteMemberVerifications ( email , type , id ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( " Invitations have been deleted " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-10 13:30:25 +02:00
}
/ * *
* Get the invited managers for a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2020-08-14 12:38:59 +02:00
@Path ( " /invite/{type}/{id}/managers/ " )
2020-08-10 13:30:25 +02:00
@GET
@Produces ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-09-08 15:00:33 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-08-10 13:30:25 +02:00
public Response getInvitedManagers ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
2020-10-05 15:09:34 +02:00
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 )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
2020-10-05 15:09:34 +02:00
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
public Response getInviteMembers ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
JsonArray invited = verificationUtils . getInvitedMembers ( type , id ) ;
2020-08-10 13:30:25 +02:00
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( invited ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
2020-08-04 18:18:08 +02:00
/ * *
* Get the verification with a specific id only if it refers to the logged in user
2020-09-03 21:45:38 +02:00
* /
2021-10-08 16:45:31 +02:00
@Path ( " /verification/{id} " )
2020-08-04 18:18:08 +02:00
@GET
@Produces ( MediaType . APPLICATION_JSON )
@PreAuthorize ( " isAuthenticated() " )
public Response getVerification ( @PathParam ( " id " ) String id ) {
2020-10-05 15:09:34 +02:00
RoleVerification verification = verificationUtils . getVerification ( id ) ;
if ( verification ! = null ) {
2021-03-24 11:08:19 +01:00
if ( verification . getEmail ( ) . equalsIgnoreCase ( authorizationService . getEmail ( ) ) ) {
2020-10-05 15:09:34 +02:00
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( jsonUtils . createVerification ( verification ) ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
} 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 ( " Verification has not been found " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
}
2020-08-10 13:30:25 +02:00
/ * *
* Delete the verification with a specific id .
2020-09-03 21:45:38 +02:00
* /
2021-10-08 16:45:31 +02:00
@Path ( " /verification/{id} " )
2020-08-10 13:30:25 +02:00
@DELETE
@Produces ( MediaType . APPLICATION_JSON )
2020-08-14 12:38:59 +02:00
@PreAuthorize ( " isAuthenticated() && @VerificationUtils.ownedVerification(#id) " )
2020-08-10 13:30:25 +02:00
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 ( ) ;
}
}
2020-08-04 18:18:08 +02:00
/ * *
* 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 .
2020-09-03 21:45:38 +02:00
* /
2021-10-08 16:45:31 +02:00
@Path ( " /verification/manager/{id} " )
2020-08-04 18:18:08 +02:00
@POST
@Produces ( MediaType . APPLICATION_JSON )
@PreAuthorize ( " isAuthenticated() " )
2021-10-08 16:45:31 +02:00
public Response verifyManager ( @PathParam ( " id " ) String id , @RequestBody String code , @Context final HttpServletRequest request ) {
2020-10-05 15:09:34 +02:00
RoleVerification verification = verificationUtils . getVerification ( id ) ;
if ( verification ! = null & & verification . getVerificationType ( ) . equals ( " manager " ) ) {
2021-10-08 16:45:31 +02:00
if ( verification . getEmail ( ) . equalsIgnoreCase ( authorizationService . getEmail ( ) ) ) {
if ( verification . getVerificationCode ( ) . equals ( code ) ) {
try {
calls . assignManagerRole ( verification . getType ( ) , verification . getEntity ( ) , request ) ;
if ( verification . getType ( ) . equals ( " community " ) | | verification . getType ( ) . equals ( " ri " ) ) {
calls . assignMemberRole ( " ri " , verification . getEntity ( ) , request ) ;
verificationUtils . deleteMemberVerifications ( verification . getEmail ( ) , " community " , verification . getEntity ( ) ) ;
verificationUtils . deleteMemberVerifications ( verification . getEmail ( ) , " ri " , verification . getEntity ( ) ) ;
verificationUtils . deleteManagerVerifications ( verification . getEmail ( ) , " community " , verification . getEntity ( ) ) ;
verificationUtils . deleteManagerVerifications ( verification . getEmail ( ) , " ri " , verification . getEntity ( ) ) ;
2020-08-04 18:18:08 +02:00
} else {
2021-10-08 16:45:31 +02:00
verificationUtils . deleteMemberVerifications ( verification . getEmail ( ) , verification . getType ( ) , verification . getEntity ( ) ) ;
verificationUtils . deleteManagerVerifications ( verification . getEmail ( ) , verification . getType ( ) , verification . getEntity ( ) ) ;
2020-08-04 18:18:08 +02:00
}
2021-10-08 16:45:31 +02:00
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( " Admin role has been assigned " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
} else {
2021-10-08 16:45:31 +02:00
return Response . status ( HttpStatus . BAD_REQUEST . value ( ) ) . entity ( jsonUtils . createResponse ( " Verification code is wrong " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
} else {
2021-10-08 16:45:31 +02:00
return Response . status ( HttpStatus . FORBIDDEN . value ( ) ) . entity ( jsonUtils . createResponse ( " Forbidden verification " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
} else {
return Response . status ( HttpStatus . NOT_FOUND . value ( ) ) . entity ( jsonUtils . createResponse ( " Verification has not been found " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
}
2020-10-05 15:09:34 +02:00
/ * *
* 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 .
* /
2021-10-08 16:45:31 +02:00
@Path ( " /verification/member/{id} " )
2020-10-05 15:09:34 +02:00
@POST
@Produces ( MediaType . APPLICATION_JSON )
@PreAuthorize ( " isAuthenticated() " )
2021-10-08 16:45:31 +02:00
public Response verifyMember ( @PathParam ( " id " ) String id , @RequestBody String code , @Context final HttpServletRequest request ) {
2020-10-05 15:09:34 +02:00
RoleVerification verification = verificationUtils . getVerification ( id ) ;
if ( verification ! = null & & verification . getVerificationType ( ) . equals ( " member " ) ) {
2021-10-08 16:45:31 +02:00
if ( verification . getEmail ( ) . equalsIgnoreCase ( authorizationService . getEmail ( ) ) ) {
if ( verification . getVerificationCode ( ) . equals ( code ) ) {
try {
calls . assignMemberRole ( verification . getType ( ) , verification . getEntity ( ) , request ) ;
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 ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
} else {
2021-10-08 16:45:31 +02:00
return Response . status ( HttpStatus . BAD_REQUEST . value ( ) ) . entity ( jsonUtils . createResponse ( " Verification code is wrong " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
} else {
2021-10-08 16:45:31 +02:00
return Response . status ( HttpStatus . FORBIDDEN . value ( ) ) . entity ( jsonUtils . createResponse ( " Forbidden verification " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
} else {
return Response . status ( HttpStatus . NOT_FOUND . value ( ) ) . entity ( jsonUtils . createResponse ( " Verification has not been found " ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
}
2020-08-04 18:18:08 +02:00
/ * *
* Remove the manager role from user with email for a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2020-08-04 18:18:08 +02:00
@Path ( " /{type}/{id}/manager/{email} " )
@DELETE
@Produces ( MediaType . APPLICATION_JSON )
@Consumes ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-08-04 18:18:08 +02:00
public Response removeManagerRole ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String
id , @PathParam ( " email " ) String email ) {
2021-10-08 16:45:31 +02:00
try {
JsonElement response = calls . removeManagerRole ( type , id , email ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( response . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-08-04 18:18:08 +02:00
}
}
/ * *
2020-10-05 15:09:34 +02:00
* Remove the member role from user with email for a type ( Community , etc . ) with id ( ee , egi , etc . )
* /
@Path ( " /{type}/{id}/member/{email} " )
@DELETE
@Produces ( MediaType . APPLICATION_JSON )
@Consumes ( MediaType . APPLICATION_JSON )
2020-11-19 18:15:08 +01:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
2020-10-05 15:09:34 +02:00
public Response removeMemberRole ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String
id , @PathParam ( " email " ) String email ) {
2021-10-08 16:45:31 +02:00
try {
JsonElement response = calls . removeMemberRole ( type , id , email ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( response . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-10-05 15:09:34 +02:00
}
}
2020-08-04 18:18:08 +02:00
/ * *
2020-10-05 15:09:34 +02:00
* Get the number of the members of a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2020-10-05 15:09:34 +02:00
@Path ( " /{type}/{id}/members/count " )
2020-08-04 18:18:08 +02:00
@GET
@Produces ( MediaType . APPLICATION_JSON )
2020-10-05 15:09:34 +02:00
public Response getMembersCount ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
2021-10-08 16:45:31 +02:00
try {
int response = calls . getAllMembersCount ( type , id ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( response ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-11-04 11:59:49 +01:00
}
2020-08-04 18:18:08 +02:00
}
/ * *
2021-10-08 16:45:31 +02:00
* Get infos of the members of a type ( Community , etc . ) with id ( ee , egi , etc . )
2020-09-03 21:45:38 +02:00
* /
2021-09-08 18:33:33 +02:00
@Path ( " /{type}/{id}/members{var:.*} " )
2020-08-04 18:18:08 +02:00
@GET
@Produces ( MediaType . APPLICATION_JSON )
2021-09-08 18:33:33 +02:00
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
" @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id)) " )
public Response getMembers ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
2021-10-08 16:45:31 +02:00
try {
JsonElement response = calls . getAllMembers ( type , id ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( response ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2020-11-04 11:59:49 +01:00
}
2020-08-04 18:18:08 +02:00
}
2021-03-04 15:45:06 +01:00
/ * *
2021-09-08 18:33:33 +02:00
* Get infos of the managers of a type ( Community , etc . ) with id ( ee , egi , etc . )
2021-03-04 15:45:06 +01:00
* /
2021-09-08 18:33:33 +02:00
@Path ( " /{type}/{id}/managers{var:.*} " )
2021-03-04 15:45:06 +01:00
@GET
@Produces ( MediaType . APPLICATION_JSON )
2021-09-08 18:33:33 +02:00
public Response getManagers ( @PathParam ( " type " ) String type , @PathParam ( " id " ) String id ) {
2021-10-08 16:45:31 +02:00
try {
JsonElement response = calls . getAllManagers ( type , id ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( response ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
}
}
/ * *
* Get infos of the curators of a type ( Community , etc . )
* /
@Path ( " /{type}/curators{var:.*} " )
@GET
@Produces ( MediaType . APPLICATION_JSON )
@PreAuthorize ( " hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type)) " )
public Response getCurators ( @PathParam ( " type " ) String type ) {
try {
JsonElement response = calls . getAllCurators ( type ) ;
return Response . status ( HttpStatus . OK . value ( ) ) . entity ( jsonUtils . createResponse ( response . toString ( ) ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
} catch ( HttpClientErrorException e ) {
String message = new JsonParser ( ) . parse ( e . getResponseBodyAsString ( ) ) . getAsJsonObject ( ) . get ( " message " ) . getAsString ( ) ;
return Response . status ( e . getStatusCode ( ) . value ( ) ) . entity ( jsonUtils . createResponse ( message ) . toString ( ) ) . type ( MediaType . APPLICATION_JSON ) . build ( ) ;
2021-03-04 15:45:06 +01:00
}
}
2021-10-08 16:45:31 +02:00
}