Project

General

Profile

« Previous | Next » 

Revision 59502

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

View differences:

RegistryService.java
3 3
import com.google.gson.JsonArray;
4 4
import com.google.gson.JsonObject;
5 5
import com.google.gson.JsonParser;
6
import eu.dnetlib.openaire.user.pojos.ManagerVerification;
6
import eu.dnetlib.openaire.user.pojos.RoleVerification;
7 7
import eu.dnetlib.openaire.user.utils.EmailSender;
8 8
import eu.dnetlib.openaire.usermanagement.dto.Role;
9 9
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
......
46 46
    @Path("/subscribe/{type}/{id}")
47 47
    @POST
48 48
    @Produces(MediaType.APPLICATION_JSON)
49
    @PreAuthorize("isAuthenticated()")
49
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
50 50
    public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
51 51
        Integer coPersonId = calls.getCoPersonIdByIdentifier();
52 52
        Integer couId = calls.getCouId(type, id);
......
60 60
    }
61 61

  
62 62
    /**
63
     * Subscribe from type(Community, etc.) with id(ee, egi, etc.).
63
     * Unsubscribe from type(Community, etc.) with id(ee, egi, etc.).
64 64
     * If user has manager role for this entity, it will be removed too.
65 65
     */
66 66
    @Path("/unsubscribe/{type}/{id}")
67 67
    @POST
68 68
    @Produces(MediaType.APPLICATION_JSON)
69
    @PreAuthorize("isAuthenticated()")
69
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
70 70
    public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) {
71 71
        Integer coPersonId = calls.getCoPersonIdByIdentifier();
72 72
        Integer couId = calls.getCouId(type, id);
......
105 105
    @Produces(MediaType.APPLICATION_JSON)
106 106
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
107 107
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
108
    public Response inviteUser(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
108
    public Response inviteManager(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
109 109
        Integer couId = calls.getCouId(type, id);
110 110
        if (couId != null) {
111 111
            Integer coPersonId = calls.getCoPersonIdByEmail(email);
112
            if (calls.getUserAdminGroup(coPersonId, couId) == null) {
113
                JsonObject invitation = verificationUtils.createInvitation(email, type, id);
114
                String name = calls.getUserNames(coPersonId);
112
            if (coPersonId == null || calls.getUserAdminGroup(coPersonId, couId) == null) {
113
                JsonObject invitation = verificationUtils.createManagerInvitation(email, type, id);
114
                String name = (coPersonId != null)?calls.getUserNames(coPersonId):null;
115 115
                JsonObject details = new JsonParser().parse(body).getAsJsonObject();
116
                String link = details.get("link").getAsString() + "/" + invitation.get("link").getAsString();
116
                String link = details.get("link").getAsString() + invitation.get("link").getAsString();
117 117
                String subject = "Invite to manage " + details.get("name").getAsString();
118
                String message = "<p>Hello " + name + ",</p>" +
118
                String message = "<p>Hello" + ((name != null)?(" " + name):"") + ",</p>" +
119 119
                        "<p> You have been invited to manage " + details.get("name").getAsString() + ". " +
120 120
                        "Use the verification code below to accept the invitation." +
121 121
                        "</p>" +
......
143 143
    }
144 144

  
145 145
    /**
146
     * Invite user with email to be a member of a type(Community, etc.) with id(ee, egi, etc.)
147
     * Auto generated link and code will be sent as response.
148
     */
149
    @Path("/invite/{type}/{id}/member/{email}")
150
    @POST
151
    @Produces(MediaType.APPLICATION_JSON)
152
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
153
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
154
    public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email, @RequestBody String body) {
155
        Integer couId = calls.getCouId(type, id);
156
        if (couId != null) {
157
            Integer coPersonId = calls.getCoPersonIdByEmail(email);
158
            if (coPersonId == null || calls.getRoleId(coPersonId, couId) == null) {
159
                JsonObject invitation = verificationUtils.createMemberInvitation(email, type, id);
160
                String name = (coPersonId != null)?calls.getUserNames(coPersonId):null;
161
                JsonObject details = new JsonParser().parse(body).getAsJsonObject();
162
                String link = details.get("link").getAsString() + invitation.get("link").getAsString();
163
                String subject = "Invite to be a member of " + details.get("name").getAsString();
164
                String message = "<p>Hello" + ((name != null)?(" " + name):"") + ",</p>" +
165
                        "<p> You have been invited to be a member of " + details.get("name").getAsString() + ". " +
166
                        "Use the verification code below to accept the invitation." +
167
                        "</p>" +
168
                        "<p>" +
169
                        "The verification code is " + invitation.get("code").getAsString() +
170
                        "</p>" +
171
                        "Click the URL below and proceed with the process." +
172
                        "<p><a href=" + link + ">" + link + "</a></p>" +
173
                        "<p>Thank you,</p>" +
174
                        "<p>OpenAIRE technical team</p>";
175
                try {
176
                    emailSender.sendEmail(email, subject, message);
177
                    return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
178
                } catch (MessagingException e) {
179
                    logger.error(e.getMessage());
180
                    verificationUtils.deleteVerification(invitation.get("link").getAsString());
181
                    return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build();
182
                }
183
            } else {
184
                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already member of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
185
            }
186
        } else {
187
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
188
        }
189
    }
190

  
191
    /**
146 192
     * Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
147 193
     */
148 194
    @Path("/invite/{type}/{id}/manager/{email}")
......
150 196
    @Produces(MediaType.APPLICATION_JSON)
151 197
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
152 198
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
153
    public Response cancelUserInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
199
    public Response cancelManagerInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
154 200
        Integer couId = calls.getCouId(type, id);
155 201
        if (couId != null) {
156
            verificationUtils.deleteUserVerifications(email, type, id);
202
            verificationUtils.deleteManagerVerifications(email, type, id);
157 203
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
158 204
        } else {
159 205
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
......
161 207
    }
162 208

  
163 209
    /**
210
     * Cancel invitation to user with email for being member of a type(Community, etc.) with id(ee, egi, etc.)
211
     */
212
    @Path("/invite/{type}/{id}/member/{email}")
213
    @DELETE
214
    @Produces(MediaType.APPLICATION_JSON)
215
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
216
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
217
    public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
218
        Integer couId = calls.getCouId(type, id);
219
        if (couId != null) {
220
            verificationUtils.deleteMemberVerifications(email, type, id);
221
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
222
        } else {
223
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
224
        }
225
    }
226

  
227
    /**
164 228
     * Get the invited managers for a type(Community, etc.) with id(ee, egi, etc.)
165 229
     */
166 230
    @Path("/invite/{type}/{id}/managers/")
......
169 233
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
170 234
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
171 235
    public Response getInvitedManagers(@PathParam("type") String type, @PathParam("id") String id) {
172
        JsonArray invited = verificationUtils.getInvitedUsers(type, id);
236
        JsonArray invited = verificationUtils.getInvitedManagers(type, id);
173 237
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
174 238
    }
175 239

  
176 240
    /**
241
     * Get the invited members for a type(Community, etc.) with id(ee, egi, etc.)
242
     */
243
    @Path("/invite/{type}/{id}/members/")
244
    @GET
245
    @Produces(MediaType.APPLICATION_JSON)
246
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN, @AuthorizationService.PORTAL_ADMIN, " +
247
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
248
    public Response getInviteMembers(@PathParam("type") String type, @PathParam("id") String id) {
249
        JsonArray invited = verificationUtils.getInvitedMembers(type, id);
250
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
251
    }
252

  
253
    /**
177 254
     * Get the verification with a specific id only if it refers to the logged in user
178 255
     */
179 256
    @Path("verification/{id}")
......
181 258
    @Produces(MediaType.APPLICATION_JSON)
182 259
    @PreAuthorize("isAuthenticated()")
183 260
    public Response getVerification(@PathParam("id") String id) {
184
        ManagerVerification managerVerification = verificationUtils.getVerification(id);
185
        if (managerVerification != null) {
186
            if (calls.getCoPersonIdByEmail(managerVerification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
187
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(managerVerification)).toString()).type(MediaType.APPLICATION_JSON).build();
261
        RoleVerification verification = verificationUtils.getVerification(id);
262
        if (verification != null) {
263
            if (calls.getCoPersonIdByEmail(verification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
264
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
188 265
            } else {
189 266
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
190 267
            }
......
213 290
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
214 291
     * Manager role is assigned to this user, along with the member role.
215 292
     */
216
    @Path("verification/{id}")
293
    @Path("verification/manager/{id}")
217 294
    @POST
218 295
    @Produces(MediaType.APPLICATION_JSON)
219 296
    @PreAuthorize("isAuthenticated()")
220
    public Response verify(@PathParam("id") String id, @RequestBody String code) {
221
        ManagerVerification managerVerification = verificationUtils.getVerification(id);
222
        if (managerVerification != null) {
223
            Integer coPersonId = calls.getCoPersonIdByEmail(managerVerification.getEmail());
297
    public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
298
        RoleVerification verification = verificationUtils.getVerification(id);
299
        if (verification != null && verification.getVerificationType().equals("manager")) {
300
            Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
224 301
            if (coPersonId != null) {
225 302
                if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
226
                    if (managerVerification.getVerificationCode().equals(code)) {
227
                        verificationUtils.deleteRelatedVerifications(managerVerification);
228
                        Integer couId = calls.getCouId(managerVerification.getType(), managerVerification.getEntity());
303
                    if (verification.getVerificationCode().equals(code)) {
304
                        Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
229 305
                        if (couId != null) {
230 306
                            Integer role = calls.getRoleId(coPersonId, couId);
231 307
                            calls.assignMemberRole(coPersonId, couId, role);
308
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
232 309
                            if (calls.getUserAdminGroup(coPersonId, couId) == null) {
310
                                verificationUtils.deleteManagerVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
233 311
                                calls.assignAdminRole(coPersonId, couId);
234 312
                                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
235 313
                            } else {
......
253 331
    }
254 332

  
255 333
    /**
334
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
335
     * Member role is assigned to this user, along with the member role.
336
     */
337
    @Path("verification/member/{id}")
338
    @POST
339
    @Produces(MediaType.APPLICATION_JSON)
340
    @PreAuthorize("isAuthenticated()")
341
    public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
342
        RoleVerification verification = verificationUtils.getVerification(id);
343
        if (verification != null && verification.getVerificationType().equals("member")) {
344
            Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
345
            if (coPersonId != null) {
346
                if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
347
                    if (verification.getVerificationCode().equals(code)) {
348
                        Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
349
                        if (couId != null) {
350
                            Integer role = calls.getRoleId(coPersonId, couId);
351
                            calls.assignMemberRole(coPersonId, couId, role);
352
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
353
                            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
354
                        } else {
355
                            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
356
                        }
357
                    } else {
358
                        return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
359
                    }
360
                } else {
361
                    return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
362
                }
363
            } else {
364
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
365
            }
366
        } else {
367
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
368
        }
369
    }
370

  
371
    /**
256 372
     * Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
257 373
     */
258 374
    @Path("/{type}/{id}/manager/{email}")
......
278 394
    }
279 395

  
280 396
    /**
281
     * Get the names of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
397
     * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
282 398
     */
283
    @Path("/{type}/{id}/subscribers")
399
    @Path("/{type}/{id}/member/{email}")
400
    @DELETE
401
    @Produces(MediaType.APPLICATION_JSON)
402
    @Consumes(MediaType.APPLICATION_JSON)
403
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.USER_ADMIN," +
404
            "@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
405
    public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
406
            id, @PathParam("email") String email) {
407
        Integer coPersonId = calls.getCoPersonIdByEmail(email);
408
        if (coPersonId != null) {
409
            Integer couId = calls.getCouId(type, id);
410
            Integer role = calls.getRoleId(coPersonId, couId);
411
            if (couId != null && role != null) {
412
                calls.removeAdminRole(coPersonId, couId);
413
                calls.removeMemberRole(coPersonId, couId, role);
414
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
415
            } else {
416
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
417
            }
418
        } else {
419
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
420
        }
421
    }
422

  
423
    /**
424
     * Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
425
     */
426
    @Path("/{type}/{id}/members")
284 427
    @GET
285 428
    @Produces(MediaType.APPLICATION_JSON)
286 429
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," +
287 430
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
288
    public Response getSubscribers(@PathParam("type") String type, @PathParam("id") String id) {
431
    public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
289 432
        Integer couId = calls.getCouId(type, id);
290 433
        JsonArray subscribers = calls.getUserNamesByCouId(couId, false);
291 434
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
292 435
    }
293 436

  
294 437
    /**
295
     * Get the emails of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
438
     * Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
296 439
     */
297
    @Path("/{type}/{id}/subscribers/email")
440
    @Path("/{type}/{id}/members/email")
298 441
    @GET
299 442
    @Produces(MediaType.APPLICATION_JSON)
300 443
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.SUPER_ADMIN, @AuthorizationService.PORTAL_ADMIN," +
301 444
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
302
    public Response getSubscribersEmail(@PathParam("type") String type, @PathParam("id") String id) {
445
    public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
303 446
        Integer couId = calls.getCouId(type, id);
304 447
        JsonArray subscribers = calls.getUserEmailByCouId(couId, false);
305 448
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(subscribers).toString()).type(MediaType.APPLICATION_JSON).build();
306 449
    }
307 450

  
308 451
    /**
309
     * Get the number of the subscribers of a type(Community, etc.) with id(ee, egi, etc.)
452
     * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
310 453
     */
311
    @Path("/{type}/{id}/subscribers/count")
454
    @Path("/{type}/{id}/members/count")
312 455
    @GET
313 456
    @Produces(MediaType.APPLICATION_JSON)
314
    public Response getSubscribersCount(@PathParam("type") String type, @PathParam("id") String id) {
457
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
315 458
        Integer couId = calls.getCouId(type, id);
316 459
        int count = calls.getUserNamesByCouId(couId, false).size();
317 460
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();

Also available in: Unified diff