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
        if(calls.getCouId(role.getName()) == null) {
124
        calls.createRole(role);
125
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
126
        } else {
127
            return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build();
128
        }
129
    }
130

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
495
    /**
496
     * Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
497
     */
498
    @Path("/{type}/{id}/members/count")
499
    @GET
500
    @Produces(MediaType.APPLICATION_JSON)
501
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
502
        Integer couId = calls.getCouId(type, id, false);
503
        int count = 0;
504
        if(couId != null) {
505
            count = calls.getUserNamesByCouId(couId, false).size();
506
        }
507
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
508
    }
509

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

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