Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement.api;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonObject;
5
import com.google.gson.JsonParser;
6
import eu.dnetlib.openaire.user.login.utils.AuthoritiesUpdater;
7
import eu.dnetlib.openaire.user.pojos.RoleVerification;
8
import eu.dnetlib.openaire.user.utils.EmailSender;
9
import eu.dnetlib.openaire.usermanagement.dto.Role;
10
import eu.dnetlib.openaire.usermanagement.utils.AuthorizationService;
11
import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
12
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
13
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
14
import org.apache.log4j.Logger;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.http.HttpStatus;
17
import org.springframework.security.access.method.P;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.security.core.authority.SimpleGrantedAuthority;
20
import org.springframework.stereotype.Component;
21
import org.springframework.web.bind.annotation.RequestBody;
22

    
23
import javax.mail.MessagingException;
24
import javax.ws.rs.*;
25
import javax.ws.rs.core.MediaType;
26
import javax.ws.rs.core.Response;
27
import java.util.Collection;
28
import java.util.HashSet;
29
import java.util.List;
30

    
31
@Component(value = "RegistryService")
32
@Path("/registry")
33
public class RegistryService {
34

    
35
    private static final Logger logger = Logger.getLogger(RegistryService.class);
36

    
37
    @Autowired
38
    private RegistryCalls calls;
39

    
40
    @Autowired
41
    private JsonUtils jsonUtils;
42

    
43
    @Autowired
44
    private EmailSender emailSender;
45

    
46
    @Autowired
47
    private VerificationUtils verificationUtils;
48

    
49
    @Autowired
50
    private AuthoritiesUpdater authoritiesUpdater;
51

    
52
    @Autowired
53
    private AuthorizationService authorizationService;
54

    
55
    /**
56
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
57
     */
58
    @Path("/subscribe/{type}/{id}")
59
    @POST
60
    @Produces(MediaType.APPLICATION_JSON)
61
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
62
    public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
63
        Integer coPersonId = calls.getCoPersonIdByIdentifier();
64
        Integer couId = calls.getCouId(type, id);
65
        if (couId != null) {
66
            Integer role = calls.getRoleId(coPersonId, couId);
67
            calls.assignMemberRole(coPersonId, couId, role);
68
            authoritiesUpdater.update(authorizationService.getEmail(), old -> {
69
                HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
70
                authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
71
                return authorities;
72
            });
73
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
74
        } else {
75
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
76
        }
77
    }
78

    
79
    /**
80
     * Unsubscribe from type(Community, etc.) with id(ee, egi, etc.).
81
     * If user has manager role for this entity, it will be removed too.
82
     */
83
    @Path("/unsubscribe/{type}/{id}")
84
    @POST
85
    @Produces(MediaType.APPLICATION_JSON)
86
    @PreAuthorize("isAuthenticated() and @AuthorizationService.isCommunity(#type)")
87
    public Response unsubscribe(@PathParam("type") String type, @PathParam("id") String id) {
88
        Integer coPersonId = calls.getCoPersonIdByIdentifier();
89
        Integer couId = calls.getCouId(type, id);
90
        if (couId != null) {
91
            Integer role = calls.getRoleId(coPersonId, couId);
92
            if (role != null) {
93
                calls.removeAdminRole(coPersonId, couId);
94
                calls.removeMemberRole(coPersonId, couId, role);
95
                authoritiesUpdater.update(authorizationService.getEmail(), old -> {
96
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
97
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
98
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
99
                    return authorities;
100
                });
101
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
102
            } else
103
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User does not have this role").toString()).type(MediaType.APPLICATION_JSON).build();
104
        } else {
105
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
106
        }
107
    }
108

    
109
    /**
110
     * Create a new role with the given name and description.
111
     **/
112
    @Path("/createRole")
113
    @POST
114
    @Produces(MediaType.APPLICATION_JSON)
115
    @Consumes(MediaType.APPLICATION_JSON)
116
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
117
    public Response createRole(@RequestBody Role role) {
118
        if (calls.getCouId(role.getName()) == null) {
119
            if(calls.createRole(role) != null) {
120
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
121
            } else {
122
                return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("An error has occurred. Please try again later").toString()).type(MediaType.APPLICATION_JSON).build();
123
            }
124
        } else {
125
            return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build();
126
        }
127
    }
128

    
129
    /**
130
     * Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
131
     * Auto generated link and code will be sent as response.
132
     */
133
    @Path("/invite/{type}/{id}/manager")
134
    @POST
135
    @Produces(MediaType.APPLICATION_JSON)
136
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
137
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
138
    public Response inviteManager(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
139
        Integer couId = calls.getCouId(type, id);
140
        if (couId != null) {
141
            JsonObject details = new JsonParser().parse(body).getAsJsonObject();
142
            JsonObject email = details.get("email").getAsJsonObject();
143
            String recipient = email.get("recipient").getAsString();
144
            Integer coPersonId = calls.getCoPersonIdByEmail(recipient);
145
            if (coPersonId == null || calls.getUserAdminGroup(coPersonId, couId) == null) {
146
                JsonObject invitation = verificationUtils.createManagerInvitation(recipient, type, id);
147
                return sendEmail(details, email, coPersonId, invitation);
148
            } else {
149
                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already manager of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
150
            }
151
        } else {
152
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
153
        }
154
    }
155

    
156
    /**
157
     * Invite user with email to be a member of a type(Community, etc.) with id(ee, egi, etc.)
158
     * Auto generated link and code will be sent as response.
159
     */
160
    @Path("/invite/{type}/{id}/member")
161
    @POST
162
    @Produces(MediaType.APPLICATION_JSON)
163
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
164
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
165
    public Response inviteMember(@PathParam("type") String type, @PathParam("id") String id, @RequestBody String body) {
166
        Integer couId = calls.getCouId(type, id, false);
167
        if (couId != null) {
168
            JsonObject details = new JsonParser().parse(body).getAsJsonObject();
169
            JsonObject email = details.get("email").getAsJsonObject();
170
            String recipient = email.get("recipient").getAsString();
171
            Integer coPersonId = calls.getCoPersonIdByEmail(recipient);
172
            if (coPersonId == null || calls.getRoleId(coPersonId, couId) == null) {
173
                JsonObject invitation = verificationUtils.createMemberInvitation(recipient, type, id);
174
                return sendEmail(details, email, coPersonId, invitation);
175
            } else {
176
                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User has been already member of this " + type).toString()).type(MediaType.APPLICATION_JSON).build();
177
            }
178
        } else {
179
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
180
        }
181
    }
182

    
183
    private Response sendEmail(JsonObject details, JsonObject email, Integer coPersonId, JsonObject invitation) {
184
        String name = (coPersonId != null) ? calls.getUserNames(coPersonId) : "User";
185
        String link = details.get("link").getAsString() + invitation.get("link").getAsString();
186
        String subject = email.get("subject").getAsString();
187
        String message = email.get("body").getAsString().
188
                replace("((__user__))", name).
189
                replace("((__link__))", link).
190
                replace("((__code__))", invitation.get("code").getAsString());
191
        try {
192
            emailSender.sendEmail(email.get("recipient").getAsString(), subject, message);
193
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invitation).toString()).type(MediaType.APPLICATION_JSON).build();
194
        } catch (MessagingException e) {
195
            logger.error(e.getMessage());
196
            verificationUtils.deleteVerification(invitation.get("link").getAsString());
197
            return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Email sent failed").toString()).type(MediaType.APPLICATION_JSON).build();
198
        }
199
    }
200

    
201
    /**
202
     * Cancel invitation to user with email for managing a type(Community, etc.) with id(ee, egi, etc.)
203
     */
204
    @Path("/invite/{type}/{id}/manager/{email}")
205
    @DELETE
206
    @Produces(MediaType.APPLICATION_JSON)
207
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
208
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
209
    public Response cancelManagerInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
210
        Integer couId = calls.getCouId(type, id);
211
        if (couId != null) {
212
            verificationUtils.deleteManagerVerifications(email, type, id);
213
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
214
        } else {
215
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
216
        }
217
    }
218

    
219
    /**
220
     * Cancel invitation to user with email for being member of a type(Community, etc.) with id(ee, egi, etc.)
221
     */
222
    @Path("/invite/{type}/{id}/member/{email}")
223
    @DELETE
224
    @Produces(MediaType.APPLICATION_JSON)
225
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
226
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
227
    public Response cancelMemberInvitations(@PathParam("type") String type, @PathParam("id") String id, @PathParam("email") String email) {
228
        Integer couId = calls.getCouId(type, id, false);
229
        if (couId != null) {
230
            verificationUtils.deleteMemberVerifications(email, type, id);
231
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Invitations have been deleted").toString()).type(MediaType.APPLICATION_JSON).build();
232
        } else {
233
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
234
        }
235
    }
236

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

    
250
    /**
251
     * Get the invited members for a type(Community, etc.) with id(ee, egi, etc.)
252
     */
253
    @Path("/invite/{type}/{id}/members/")
254
    @GET
255
    @Produces(MediaType.APPLICATION_JSON)
256
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, " +
257
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
258
    public Response getInviteMembers(@PathParam("type") String type, @PathParam("id") String id) {
259
        JsonArray invited = verificationUtils.getInvitedMembers(type, id);
260
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(invited).toString()).type(MediaType.APPLICATION_JSON).build();
261
    }
262

    
263
    /**
264
     * Get the verification with a specific id only if it refers to the logged in user
265
     */
266
    @Path("verification/{id}")
267
    @GET
268
    @Produces(MediaType.APPLICATION_JSON)
269
    @PreAuthorize("isAuthenticated()")
270
    public Response getVerification(@PathParam("id") String id) {
271
        RoleVerification verification = verificationUtils.getVerification(id);
272
        if (verification != null) {
273
            if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
274
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
275
            } else {
276
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
277
            }
278
        } else {
279
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
280
        }
281
    }
282

    
283
    /**
284
     * Delete the verification with a specific id.
285
     */
286
    @Path("verification/{id}")
287
    @DELETE
288
    @Produces(MediaType.APPLICATION_JSON)
289
    @PreAuthorize("isAuthenticated() && @VerificationUtils.ownedVerification(#id)")
290
    public Response deleteVerification(@PathParam("id") String id) {
291
        if (verificationUtils.getVerification(id) != null) {
292
            verificationUtils.deleteVerification(id);
293
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification deleted")).toString()).type(MediaType.APPLICATION_JSON).build();
294
        } else {
295
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse(jsonUtils.createResponse("Verification has not been found")).toString()).type(MediaType.APPLICATION_JSON).build();
296
        }
297
    }
298

    
299
    /**
300
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
301
     * Manager role is assigned to this user, along with the member role.
302
     */
303
    @Path("verification/manager/{id}")
304
    @POST
305
    @Produces(MediaType.APPLICATION_JSON)
306
    @PreAuthorize("isAuthenticated()")
307
    public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
308
        RoleVerification verification = verificationUtils.getVerification(id);
309
        if (verification != null && verification.getVerificationType().equals("manager")) {
310
            Integer coPersonId = calls.getCoPersonIdByIdentifier();
311
            if (coPersonId != null) {
312
                if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
313
                    if (verification.getVerificationCode().equals(code)) {
314
                        Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
315
                        if (couId != null) {
316
                            Integer role = calls.getRoleId(coPersonId, couId);
317
                            calls.assignMemberRole(coPersonId, couId, role);
318
                            if (verification.getType().equals("community") || verification.getType().equals("ri")) {
319
                                Integer riCouId = calls.getCouId("ri", verification.getEntity(), false);
320
                                if (riCouId != null) {
321
                                    calls.assignMemberRole(coPersonId, riCouId, calls.getRoleId(coPersonId, riCouId));
322
                                    verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
323
                                    verificationUtils.deleteMemberVerifications(verification.getEmail(), "ri", verification.getEntity());
324
                                } else {
325
                                    verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
326
                                }
327
                            } else {
328
                                verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
329
                            }
330
                            if (calls.getUserAdminGroup(coPersonId, couId) == null) {
331
                                if (verification.getType().equals("community") || verification.getType().equals("ri")) {
332
                                    verificationUtils.deleteManagerVerifications(verification.getEmail(), "community", verification.getEntity());
333
                                    verificationUtils.deleteManagerVerifications(verification.getEmail(), "ri", verification.getEntity());
334
                                } else {
335
                                    verificationUtils.deleteManagerVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
336
                                }
337
                                calls.assignAdminRole(coPersonId, couId);
338
                                authoritiesUpdater.update(verification.getEmail(), old -> {
339
                                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
340
                                    authorities.add(new SimpleGrantedAuthority(authorizationService.member(verification.getType(), verification.getEntity())));
341
                                    authorities.add(new SimpleGrantedAuthority(authorizationService.manager(verification.getType(), verification.getEntity())));
342
                                    return authorities;
343
                                });
344
                                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Admin role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
345
                            } else {
346
                                return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("User is already admin of this cou").toString()).type(MediaType.APPLICATION_JSON).build();
347
                            }
348
                        } else {
349
                            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
350
                        }
351
                    } else {
352
                        return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
353
                    }
354
                } else {
355
                    return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
356
                }
357
            } else {
358
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
359
            }
360
        } else {
361
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
362
        }
363
    }
364

    
365
    /**
366
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
367
     * Member role is assigned to this user, along with the member role.
368
     */
369
    @Path("verification/member/{id}")
370
    @POST
371
    @Produces(MediaType.APPLICATION_JSON)
372
    @PreAuthorize("isAuthenticated()")
373
    public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
374
        RoleVerification verification = verificationUtils.getVerification(id);
375
        if (verification != null && verification.getVerificationType().equals("member")) {
376
            Integer coPersonId = calls.getCoPersonIdByIdentifier();
377
            if (coPersonId != null) {
378
                if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
379
                    if (verification.getVerificationCode().equals(code)) {
380
                        Integer couId = calls.getCouId(verification.getType(), verification.getEntity(), false);
381
                        if (couId != null) {
382
                            Integer role = calls.getRoleId(coPersonId, couId);
383
                            calls.assignMemberRole(coPersonId, couId, role);
384
                            authoritiesUpdater.update(verification.getEmail(), old -> {
385
                                HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
386
                                authorities.add(new SimpleGrantedAuthority(authorizationService.member(verification.getType(), verification.getEntity())));
387
                                return authorities;
388
                            });
389
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
390
                            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
391
                        } else {
392
                            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
393
                        }
394
                    } else {
395
                        return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
396
                    }
397
                } else {
398
                    return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
399
                }
400
            } else {
401
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
402
            }
403
        } else {
404
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
405
        }
406
    }
407

    
408
    /**
409
     * Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
410
     */
411
    @Path("/{type}/{id}/manager/{email}")
412
    @DELETE
413
    @Produces(MediaType.APPLICATION_JSON)
414
    @Consumes(MediaType.APPLICATION_JSON)
415
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
416
    public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
417
            id, @PathParam("email") String email) {
418
        List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
419
        if (coPersonIds.size() > 0) {
420
            Integer couId = calls.getCouId(type, id);
421
            if (couId != null) {
422
                coPersonIds.forEach(coPersonId -> {
423
                    calls.removeAdminRole(coPersonId, couId);
424
                });
425
                authoritiesUpdater.update(email, old -> {
426
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
427
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
428
                    return authorities;
429
                });
430
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
431
            } else {
432
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
433
            }
434
        } else {
435
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
436
        }
437
    }
438

    
439
    /**
440
     * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
441
     */
442
    @Path("/{type}/{id}/member/{email}")
443
    @DELETE
444
    @Produces(MediaType.APPLICATION_JSON)
445
    @Consumes(MediaType.APPLICATION_JSON)
446
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
447
    public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
448
            id, @PathParam("email") String email) {
449
        List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
450
        if (coPersonIds.size() > 0) {
451
            Integer couId = calls.getCouId(type, id, false);
452
            if (couId != null) {
453
                coPersonIds.forEach(coPersonId -> {
454
                    Integer role = calls.getRoleId(coPersonId, couId);
455
                    calls.removeAdminRole(coPersonId, couId);
456
                    calls.removeMemberRole(coPersonId, couId, role);
457
                });
458
                authoritiesUpdater.update(email, old -> {
459
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
460
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
461
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
462
                    return authorities;
463
                });
464
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
465
            } else {
466
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
467
            }
468
        } else {
469
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
470
        }
471
    }
472

    
473
    /**
474
     * Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
475
     */
476
    @Path("/{type}/{id}/members")
477
    @GET
478
    @Produces(MediaType.APPLICATION_JSON)
479
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
480
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
481
    public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
482
        Integer couId = calls.getCouId(type, id, false);
483
        if (couId != null) {
484
            JsonArray members = calls.getUserNamesByCouId(couId, false);
485
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
486
        } else {
487
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
488
        }
489
    }
490

    
491
    /**
492
     * Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
493
     */
494
    @Path("/{type}/{id}/members/email")
495
    @GET
496
    @Produces(MediaType.APPLICATION_JSON)
497
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
498
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
499
    public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
500
        Integer couId = calls.getCouId(type, id, false);
501
        if (couId != null) {
502
            JsonArray members = calls.getUserEmailByCouId(couId, false);
503
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
504
        } else {
505
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
506
        }
507
    }
508

    
509
    /**
510
     * Get the Identifiers of the members of a type(Community, etc.) with id(ee, egi, etc.)
511
     */
512
    @Path("/{type}/{id}/members/id")
513
    @GET
514
    @Produces(MediaType.APPLICATION_JSON)
515
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
516
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
517
    public Response getMembersId(@PathParam("type") String type, @PathParam("id") String id) {
518
        Integer couId = calls.getCouId(type, id, false);
519
        if (couId != null) {
520
            JsonArray members = calls.getUserIdByCouId(couId, false);
521
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
522
        } else {
523
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
524
        }
525
    }
526

    
527
    /**
528
     * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
529
     */
530
    @Path("/{type}/{id}/members/count")
531
    @GET
532
    @Produces(MediaType.APPLICATION_JSON)
533
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
534
        Integer couId = calls.getCouId(type, id, false);
535
        int count = 0;
536
        if (couId != null) {
537
            count = calls.getUserNamesByCouId(couId, false).size();
538
        }
539
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
540
    }
541

    
542
    /**
543
     * Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
544
     */
545
    @Path("/{type}/{id}/managers")
546
    @GET
547
    @Produces(MediaType.APPLICATION_JSON)
548
    public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
549
        Integer couId = calls.getCouId(type, id);
550
        if (couId != null) {
551
            JsonArray managers = calls.getUserNamesByCouId(couId, true);
552
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
553
        } else {
554
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
555
        }
556
    }
557

    
558
    /**
559
     * Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
560
     */
561
    @Path("/{type}/{id}/managers/email")
562
    @GET
563
    @Produces(MediaType.APPLICATION_JSON)
564
    public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
565
        Integer couId = calls.getCouId(type, id);
566
        if (couId != null) {
567
            JsonArray managers = calls.getUserEmailByCouId(couId, true);
568
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
569
        } else {
570
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
571
        }
572
    }
573

    
574
    /**
575
     * Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.)
576
     */
577
    @Path("/{type}/{id}/managers/id")
578
    @GET
579
    @Produces(MediaType.APPLICATION_JSON)
580
    public Response getManagersId(@PathParam("type") String type, @PathParam("id") String id) {
581
        Integer couId = calls.getCouId(type, id);
582
        if (couId != null) {
583
            JsonArray managers = calls.getUserIdByCouId(couId, true);
584
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
585
        } else {
586
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
587
        }
588
    }
589
}
(1-1/2)