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.mitre.openid.connect.model.OIDCAuthenticationToken;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.http.HttpStatus;
18
import org.springframework.security.access.prepost.PreAuthorize;
19
import org.springframework.security.core.authority.SimpleGrantedAuthority;
20
import org.springframework.security.core.context.SecurityContextHolder;
21
import org.springframework.stereotype.Component;
22
import org.springframework.web.bind.annotation.RequestBody;
23

    
24
import javax.mail.MessagingException;
25
import javax.ws.rs.*;
26
import javax.ws.rs.core.MediaType;
27
import javax.ws.rs.core.Response;
28
import java.util.Collection;
29
import java.util.HashSet;
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
    private String sendEmail() {
56
        OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
57
        return authenticationToken.getUserInfo().getEmail();
58
    }
59

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

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

    
114
    /**
115
     * Create a new role with the given name and description.
116
     **/
117
    @Path("/createRole")
118
    @POST
119
    @Produces(MediaType.APPLICATION_JSON)
120
    @Consumes(MediaType.APPLICATION_JSON)
121
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
122
    public Response createRole(@RequestBody Role role) {
123
        calls.createRole(role);
124
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
125
    }
126

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

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

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

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

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

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

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

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

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

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

    
348
    /**
349
     * Verify the verification with the specific id, if the code is correct and it refers to the logged in user.
350
     * Member role is assigned to this user, along with the member role.
351
     */
352
    @Path("verification/member/{id}")
353
    @POST
354
    @Produces(MediaType.APPLICATION_JSON)
355
    @PreAuthorize("isAuthenticated()")
356
    public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
357
        RoleVerification verification = verificationUtils.getVerification(id);
358
        if (verification != null && verification.getVerificationType().equals("member")) {
359
            Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
360
            if (coPersonId != null) {
361
                if (coPersonId.equals(calls.getCoPersonIdByIdentifier())) {
362
                    if (verification.getVerificationCode().equals(code)) {
363
                        Integer couId = calls.getCouId(verification.getType(), verification.getEntity());
364
                        if (couId != null) {
365
                            Integer role = calls.getRoleId(coPersonId, couId);
366
                            calls.assignMemberRole(coPersonId, couId, role);
367
                            authoritiesUpdater.update(verification.getEmail(), old -> {
368
                                HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
369
                                authorities.add(new SimpleGrantedAuthority(authorizationService.member(verification.getType(), verification.getEntity())));
370
                                return authorities;
371
                            });
372
                            verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
373
                            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Member role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
374
                        } else {
375
                            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
376
                        }
377
                    } else {
378
                        return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Verification code is wrong").toString()).type(MediaType.APPLICATION_JSON).build();
379
                    }
380
                } else {
381
                    return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
382
                }
383
            } else {
384
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
385
            }
386
        } else {
387
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Verification has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
388
        }
389
    }
390

    
391
    /**
392
     * Remove the manager role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
393
     */
394
    @Path("/{type}/{id}/manager/{email}")
395
    @DELETE
396
    @Produces(MediaType.APPLICATION_JSON)
397
    @Consumes(MediaType.APPLICATION_JSON)
398
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
399
    public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
400
            id, @PathParam("email") String email) {
401
        Integer coPersonId = calls.getCoPersonIdByEmail(email);
402
        if (coPersonId != null) {
403
            Integer couId = calls.getCouId(type, id);
404
            if (couId != null) {
405
                calls.removeAdminRole(coPersonId, couId);
406
                authoritiesUpdater.update(email, old -> {
407
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
408
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
409
                    return authorities;
410
                });
411
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
412
            } else {
413
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
414
            }
415
        } else {
416
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
417
        }
418
    }
419

    
420
    /**
421
     * Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
422
     */
423
    @Path("/{type}/{id}/member/{email}")
424
    @DELETE
425
    @Produces(MediaType.APPLICATION_JSON)
426
    @Consumes(MediaType.APPLICATION_JSON)
427
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
428
    public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
429
            id, @PathParam("email") String email) {
430
        Integer coPersonId = calls.getCoPersonIdByEmail(email);
431
        if (coPersonId != null) {
432
            Integer couId = calls.getCouId(type, id);
433
            Integer role = null;
434
            if(couId != null) {
435
                role = calls.getRoleId(coPersonId, couId);
436
            }
437
            if (couId != null && role != null) {
438
                calls.removeAdminRole(coPersonId, couId);
439
                calls.removeMemberRole(coPersonId, couId, role);
440
                authoritiesUpdater.update(email, old -> {
441
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
442
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
443
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
444
                    return authorities;
445
                });
446
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
447
            } else {
448
                return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
449
            }
450
        } else {
451
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
452
        }
453
    }
454

    
455
    /**
456
     * Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
457
     */
458
    @Path("/{type}/{id}/members")
459
    @GET
460
    @Produces(MediaType.APPLICATION_JSON)
461
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
462
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
463
    public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
464
        Integer couId = calls.getCouId(type, id);
465
        if(couId != null) {
466
            JsonArray members = calls.getUserNamesByCouId(couId, false);
467
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
468
        } else {
469
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
470
        }
471
    }
472

    
473
    /**
474
     * Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
475
     */
476
    @Path("/{type}/{id}/members/email")
477
    @GET
478
    @Produces(MediaType.APPLICATION_JSON)
479
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
480
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
481
    public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
482
        Integer couId = calls.getCouId(type, id);
483
        if(couId != null) {
484
            JsonArray members = calls.getUserEmailByCouId(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 number of the members of a type(Community, etc.) with id(ee, egi, etc.)
493
     */
494
    @Path("/{type}/{id}/members/count")
495
    @GET
496
    @Produces(MediaType.APPLICATION_JSON)
497
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
498
        Integer couId = calls.getCouId(type, id);
499
        int count = 0;
500
        if(couId != null) {
501
            count = calls.getUserNamesByCouId(couId, false).size();
502
        }
503
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
504
    }
505

    
506
    /**
507
     * Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
508
     */
509
    @Path("/{type}/{id}/managers")
510
    @GET
511
    @Produces(MediaType.APPLICATION_JSON)
512
    public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
513
        Integer couId = calls.getCouId(type, id);
514
        if(couId != null) {
515
            JsonArray managers = calls.getUserNamesByCouId(couId, true);
516
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
517
        } else {
518
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
519
        }
520
    }
521

    
522
    /**
523
     * Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
524
     */
525
    @Path("/{type}/{id}/managers/email")
526
    @GET
527
    @Produces(MediaType.APPLICATION_JSON)
528
    public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
529
        Integer couId = calls.getCouId(type, id);
530
        if(couId != null) {
531
            JsonArray managers = calls.getUserEmailByCouId(couId, true);
532
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
533
        } else {
534
            return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
535
        }
536
    }
537
}
(1-1/2)