Revision 59337
Added by Konstantinos Triantafyllou about 4 years ago
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import com.google.gson.JsonArray; |
4 | 4 |
import com.google.gson.JsonObject; |
5 |
import com.google.gson.JsonParser; |
|
5 | 6 |
import eu.dnetlib.openaire.user.pojos.ManagerVerification; |
7 |
import eu.dnetlib.openaire.user.utils.EmailSender; |
|
6 | 8 |
import eu.dnetlib.openaire.usermanagement.dto.Role; |
7 | 9 |
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils; |
8 | 10 |
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls; |
... | ... | |
14 | 16 |
import org.springframework.stereotype.Component; |
15 | 17 |
import org.springframework.web.bind.annotation.RequestBody; |
16 | 18 |
|
19 |
import javax.mail.MessagingException; |
|
17 | 20 |
import javax.ws.rs.*; |
18 | 21 |
import javax.ws.rs.core.MediaType; |
19 | 22 |
import javax.ws.rs.core.Response; |
... | ... | |
31 | 34 |
private JsonUtils jsonUtils; |
32 | 35 |
|
33 | 36 |
@Autowired |
37 |
private EmailSender emailSender; |
|
38 |
|
|
39 |
@Autowired |
|
34 | 40 |
private VerificationUtils verificationUtils; |
35 | 41 |
|
42 |
|
|
36 | 43 |
/** |
37 | 44 |
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.) |
38 |
* |
|
39 |
* */ |
|
45 |
*/ |
|
40 | 46 |
@Path("/subscribe/{type}/{id}") |
41 | 47 |
@POST |
42 | 48 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
56 | 62 |
/** |
57 | 63 |
* Subscribe from type(Community, etc.) with id(ee, egi, etc.). |
58 | 64 |
* If user has manager role for this entity, it will be removed too. |
59 |
* |
|
60 |
* */ |
|
65 |
*/ |
|
61 | 66 |
@Path("/unsubscribe/{type}/{id}") |
62 | 67 |
@POST |
63 | 68 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
80 | 85 |
|
81 | 86 |
/** |
82 | 87 |
* Create a new role with the given name and description. |
83 |
* |
|
84 | 88 |
**/ |
85 | 89 |
@Path("/createRole") |
86 | 90 |
@POST |
... | ... | |
95 | 99 |
/** |
96 | 100 |
* Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.) |
97 | 101 |
* Auto generated link and code will be sent as response. |
98 |
* |
|
99 |
* */ |
|
102 |
*/ |
|
100 | 103 |
@Path("/invite/{type}/{id}/manager/{email}") |
101 | 104 |
@POST |
102 | 105 |
@Produces(MediaType.APPLICATION_JSON) |
103 | 106 |
@PreAuthorize("hasAnyAuthority(@AuthoritiesService.SUPER_ADMIN, @AuthoritiesService.USER_ADMIN, @AuthoritiesService.PORTAL_ADMIN, " + |
104 | 107 |
"@AuthoritiesService.curator(#type), @AuthoritiesService.manager(#type, #id))") |
105 |
public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) { |
|
108 |
public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
|
|
106 | 109 |
Integer couId = calls.getCouId(type, id); |
107 | 110 |
if (couId != null) { |
108 | 111 |
Integer coPersonId = calls.getCoPersonIdByEmail(email); |
109 |
if(calls.getUserAdminGroup(coPersonId, couId) == null) { |
|
112 |
if (calls.getUserAdminGroup(coPersonId, couId) == null) {
|
|
110 | 113 |
JsonObject invitation = verificationUtils.createInvitation(email, type, id); |
111 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build(); |
|
114 |
String name = calls.getUserNames(coPersonId); |
|
115 |
JsonObject details = new JsonParser().parse(body).getAsJsonObject(); |
|
116 |
String link = details.get("link").getAsString() + "/" + invitation.get("link").getAsString(); |
|
117 |
String subject = "Invite to manage " + details.get("name").getAsString(); |
|
118 |
String message = "<p>Hello " + name + ",</p>" + |
|
119 |
"<p> You have been invited to manage " + details.get("name").getAsString() + ". " + |
|
120 |
"Use the verification code below to accept the invitation." + |
|
121 |
"</p>" + |
|
122 |
"<p>" + |
|
123 |
"The verification code is " + invitation.get("code").getAsString() + |
|
124 |
"</p>" + |
|
125 |
"Click the URL below and proceed with the process." + |
|
126 |
"<p><a href=" + link + ">" + link + "</a></p>" + |
|
127 |
"<p>Thank you,</p>" + |
|
128 |
"<p>OpenAIRE technical team</p>"; |
|
129 |
try { |
|
130 |
emailSender.sendEmail(email, subject, message); |
|
131 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build(); |
|
132 |
} catch (MessagingException e) { |
|
133 |
logger.error(e.getMessage()); |
|
134 |
verificationUtils.deleteVerification(invitation.get("link").getAsString()); |
|
135 |
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
136 |
} |
|
112 | 137 |
} else { |
113 | 138 |
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build(); |
114 | 139 |
} |
... | ... | |
119 | 144 |
|
120 | 145 |
/** |
121 | 146 |
* Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.) |
122 |
* |
|
123 |
* */ |
|
147 |
*/ |
|
124 | 148 |
@Path("/invite/{type}/{id}/manager/{email}") |
125 | 149 |
@DELETE |
126 | 150 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
138 | 162 |
|
139 | 163 |
/** |
140 | 164 |
* Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.) |
141 |
* |
|
142 |
* */ |
|
165 |
*/ |
|
143 | 166 |
@Path("/invite/{type}/{id}/managers/") |
144 | 167 |
@GET |
145 | 168 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
152 | 175 |
|
153 | 176 |
/** |
154 | 177 |
* Get the verification with a specific id only if it refers to the logged in user |
155 |
* |
|
156 |
* */ |
|
178 |
*/ |
|
157 | 179 |
@Path("verification/{id}") |
158 | 180 |
@GET |
159 | 181 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
173 | 195 |
|
174 | 196 |
/** |
175 | 197 |
* Delete the verification with a specific id. |
176 |
* |
|
177 |
* */ |
|
198 |
*/ |
|
178 | 199 |
@Path("verification/{id}") |
179 | 200 |
@DELETE |
180 | 201 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
191 | 212 |
/** |
192 | 213 |
* Verify the verification with the specific id, if the code is correct and it refers to the logged in user. |
193 | 214 |
* Manager role is assigned to this user, along with the member role. |
194 |
* |
|
195 |
* */ |
|
215 |
*/ |
|
196 | 216 |
@Path("verification/{id}") |
197 | 217 |
@POST |
198 | 218 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
234 | 254 |
|
235 | 255 |
/** |
236 | 256 |
* Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.) |
237 |
* |
|
238 |
* */ |
|
257 |
*/ |
|
239 | 258 |
@Path("/{type}/{id}/manager/{email}") |
240 | 259 |
@DELETE |
241 | 260 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
260 | 279 |
|
261 | 280 |
/** |
262 | 281 |
* Get the names of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) |
263 |
* |
|
264 |
* */ |
|
282 |
*/ |
|
265 | 283 |
@Path("/{type}/{id}/subscribers") |
266 | 284 |
@GET |
267 | 285 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
275 | 293 |
|
276 | 294 |
/** |
277 | 295 |
* Get the emails of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) |
278 |
* |
|
279 |
* */ |
|
296 |
*/ |
|
280 | 297 |
@Path("/{type}/{id}/subscribers/email") |
281 | 298 |
@GET |
282 | 299 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
290 | 307 |
|
291 | 308 |
/** |
292 | 309 |
* Get the number of the subscribers of a type(Community, etc.) with id(ee, egi, etc.) |
293 |
* |
|
294 |
* */ |
|
310 |
*/ |
|
295 | 311 |
@Path("/{type}/{id}/subscribers/count") |
296 | 312 |
@GET |
297 | 313 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
303 | 319 |
|
304 | 320 |
/** |
305 | 321 |
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.) |
306 |
* |
|
307 |
* */ |
|
322 |
*/ |
|
308 | 323 |
@Path("/{type}/{id}/managers") |
309 | 324 |
@GET |
310 | 325 |
@Produces(MediaType.APPLICATION_JSON) |
... | ... | |
316 | 331 |
|
317 | 332 |
/** |
318 | 333 |
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.) |
319 |
* |
|
320 |
* */ |
|
334 |
*/ |
|
321 | 335 |
@Path("/{type}/{id}/managers/email") |
322 | 336 |
@GET |
323 | 337 |
@Produces(MediaType.APPLICATION_JSON) |
Also available in: Unified diff
Add email sent on manager invitation