Project

General

Profile

« Previous | Next » 

Revision 60714

[Users | Trunk]: Remove duplicates emails from return lists. Remove role from duplicates accounts

View differences:

modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java
8 8
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
9 9
import org.springframework.beans.factory.annotation.Autowired;
10 10
import org.springframework.beans.factory.annotation.Value;
11
import org.springframework.security.access.method.P;
11 12
import org.springframework.security.core.context.SecurityContextHolder;
12 13
import org.springframework.stereotype.Service;
13 14

  
15
import java.util.ArrayList;
14 16
import java.util.HashMap;
17
import java.util.List;
15 18
import java.util.Map;
16 19

  
17 20
@Service
......
70 73
        return null;
71 74
    }
72 75

  
76
    public List<Integer> getCoPersonIdsByEmail(String email) {
77
        List<Integer> coPersonIds = new ArrayList<>();
78
        Map<String, String> params = new HashMap<>();
79
        params.put("coid", coid);
80
        params.put("mail", email);
81
        JsonElement response = httpUtils.get("co_people.json", params);
82
        if(response != null) {
83
            JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
84
            for(int i = 0; i < coPeople.size(); i++) {
85
                coPersonIds.add(coPeople.get(i).getAsJsonObject().get("Id").getAsInt());
86
            }
87
        }
88
        return coPersonIds;
89
    }
90

  
73 91
    /**
74 92
     * 2. Get CoPersonId by AAI identifier
75 93
     */
......
255 273
        JsonArray emails = new JsonArray();
256 274
        infos.forEach(info -> {
257 275
            JsonObject user = new JsonObject();
258
            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
259
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
260
            emails.add(user);
276
            boolean add = true;
277
            String email = info.getAsJsonObject().get("Mail").getAsString();
278
            for(JsonElement element : emails) {
279
                if(element.getAsJsonObject().get("email").getAsString().equals(email)) {
280
                    add = false;
281
                }
282
            }
283
            if(add) {
284
                user.addProperty("email", email);
285
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
286
                emails.add(user);
287
            }
261 288
        });
262 289
        return emails;
263 290
    }
......
319 346
     * 16. Remove a member role from a User
320 347
     */
321 348
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
322
        httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
349
        if(id != null) {
350
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
351
        }
323 352
    }
324 353

  
325 354
    /**
modules/dnet-openaire-users/trunk/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java
28 28
import javax.ws.rs.core.Response;
29 29
import java.util.Collection;
30 30
import java.util.HashSet;
31
import java.util.List;
31 32

  
32 33
@Component(value = "RegistryService")
33 34
@Path("/registry")
......
53 54
    @Autowired
54 55
    private AuthorizationService authorizationService;
55 56

  
56
    private String sendEmail() {
57
        OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
58
        return authenticationToken.getUserInfo().getEmail();
59
    }
60

  
61 57
    /**
62 58
     * Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
63 59
     */
......
71 67
        if (couId != null) {
72 68
            Integer role = calls.getRoleId(coPersonId, couId);
73 69
            calls.assignMemberRole(coPersonId, couId, role);
74
            authoritiesUpdater.update(sendEmail(), old -> {
70
            authoritiesUpdater.update(authorizationService.getEmail(), old -> {
75 71
                HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
76 72
                authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
77 73
                return authorities;
......
98 94
            if (role != null) {
99 95
                calls.removeAdminRole(coPersonId, couId);
100 96
                calls.removeMemberRole(coPersonId, couId, role);
101
                authoritiesUpdater.update(sendEmail(), old -> {
97
                authoritiesUpdater.update(authorizationService.getEmail(), old -> {
102 98
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
103 99
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
104 100
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
......
121 117
    @Consumes(MediaType.APPLICATION_JSON)
122 118
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
123 119
    public Response createRole(@RequestBody Role role) {
124
        if(calls.getCouId(role.getName()) == null) {
120
        if (calls.getCouId(role.getName()) == null) {
125 121
            calls.createRole(role);
126 122
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
127 123
        } else {
......
130 126
    }
131 127

  
132 128
    /**
133
     *
134 129
     * Invite user with email to manage a type(Community, etc.) with id(ee, egi, etc.)
135 130
     * Auto generated link and code will be sent as response.
136 131
     */
......
185 180
    }
186 181

  
187 182
    private Response sendEmail(JsonObject details, JsonObject email, Integer coPersonId, JsonObject invitation) {
188
        String name = (coPersonId != null)?calls.getUserNames(coPersonId):"User";
183
        String name = (coPersonId != null) ? calls.getUserNames(coPersonId) : "User";
189 184
        String link = details.get("link").getAsString() + invitation.get("link").getAsString();
190 185
        String subject = email.get("subject").getAsString();
191 186
        String message = email.get("body").getAsString().
......
274 269
    public Response getVerification(@PathParam("id") String id) {
275 270
        RoleVerification verification = verificationUtils.getVerification(id);
276 271
        if (verification != null) {
277
            if (calls.getCoPersonIdByEmail(verification.getEmail()).equals(calls.getCoPersonIdByIdentifier())) {
272
            if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
278 273
                return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(jsonUtils.createVerification(verification)).toString()).type(MediaType.APPLICATION_JSON).build();
279 274
            } else {
280 275
                return Response.status(HttpStatus.FORBIDDEN.value()).entity(jsonUtils.createResponse("Forbidden verification").toString()).type(MediaType.APPLICATION_JSON).build();
......
311 306
    public Response verifyManager(@PathParam("id") String id, @RequestBody String code) {
312 307
        RoleVerification verification = verificationUtils.getVerification(id);
313 308
        if (verification != null && verification.getVerificationType().equals("manager")) {
314
            Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
309
            Integer coPersonId = calls.getCoPersonIdByIdentifier();
315 310
            if (coPersonId != null) {
316 311
                if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
317 312
                    if (verification.getVerificationCode().equals(code)) {
......
319 314
                        if (couId != null) {
320 315
                            Integer role = calls.getRoleId(coPersonId, couId);
321 316
                            calls.assignMemberRole(coPersonId, couId, role);
322
                            if(verification.getType().equals("community") || verification.getType().equals("ri")) {
317
                            if (verification.getType().equals("community") || verification.getType().equals("ri")) {
323 318
                                Integer riCouId = calls.getCouId("ri", verification.getEntity(), false);
324
                                if(riCouId != null) {
319
                                if (riCouId != null) {
325 320
                                    calls.assignMemberRole(coPersonId, riCouId, calls.getRoleId(coPersonId, riCouId));
326 321
                                    verificationUtils.deleteMemberVerifications(verification.getEmail(), "community", verification.getEntity());
327 322
                                    verificationUtils.deleteMemberVerifications(verification.getEmail(), "ri", verification.getEntity());
......
332 327
                                verificationUtils.deleteMemberVerifications(verification.getEmail(), verification.getType(), verification.getEntity());
333 328
                            }
334 329
                            if (calls.getUserAdminGroup(coPersonId, couId) == null) {
335
                                if(verification.getType().equals("community") || verification.getType().equals("ri")) {
330
                                if (verification.getType().equals("community") || verification.getType().equals("ri")) {
336 331
                                    verificationUtils.deleteManagerVerifications(verification.getEmail(), "community", verification.getEntity());
337 332
                                    verificationUtils.deleteManagerVerifications(verification.getEmail(), "ri", verification.getEntity());
338 333
                                } else {
......
377 372
    public Response verifyMember(@PathParam("id") String id, @RequestBody String code) {
378 373
        RoleVerification verification = verificationUtils.getVerification(id);
379 374
        if (verification != null && verification.getVerificationType().equals("member")) {
380
            Integer coPersonId = calls.getCoPersonIdByEmail(verification.getEmail());
375
            Integer coPersonId = calls.getCoPersonIdByIdentifier();
381 376
            if (coPersonId != null) {
382 377
                if (verification.getEmail().equalsIgnoreCase(authorizationService.getEmail())) {
383 378
                    if (verification.getVerificationCode().equals(code)) {
......
419 414
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
420 415
    public Response removeManagerRole(@PathParam("type") String type, @PathParam("id") String
421 416
            id, @PathParam("email") String email) {
422
        Integer coPersonId = calls.getCoPersonIdByEmail(email);
423
        if (coPersonId != null) {
417
        List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
418
        if (coPersonIds.size() > 0) {
424 419
            Integer couId = calls.getCouId(type, id);
425 420
            if (couId != null) {
426
                calls.removeAdminRole(coPersonId, couId);
421
                coPersonIds.forEach(coPersonId -> {
422
                    calls.removeAdminRole(coPersonId, couId);
423
                });
427 424
                authoritiesUpdater.update(email, old -> {
428 425
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
429 426
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
......
448 445
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN, @AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
449 446
    public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
450 447
            id, @PathParam("email") String email) {
451
        Integer coPersonId = calls.getCoPersonIdByEmail(email);
452
        if (coPersonId != null) {
448
        List<Integer> coPersonIds = calls.getCoPersonIdsByEmail(email);
449
        if (coPersonIds.size() > 0) {
453 450
            Integer couId = calls.getCouId(type, id, false);
454
            Integer role = null;
455
            if(couId != null) {
456
                role = calls.getRoleId(coPersonId, couId);
457
            }
458
            if (couId != null && role != null) {
459
                calls.removeAdminRole(coPersonId, couId);
460
                calls.removeMemberRole(coPersonId, couId, role);
451
            if (couId != null) {
452
                coPersonIds.forEach(coPersonId -> {
453
                    Integer role = calls.getRoleId(coPersonId, couId);
454
                    calls.removeAdminRole(coPersonId, couId);
455
                    calls.removeMemberRole(coPersonId, couId, role);
456
                });
461 457
                authoritiesUpdater.update(email, old -> {
462 458
                    HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
463 459
                    authorities.remove(new SimpleGrantedAuthority(authorizationService.manager(type, id)));
......
482 478
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN," +
483 479
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
484 480
    public Response getMembers(@PathParam("type") String type, @PathParam("id") String id) {
485
        Integer couId = calls.getCouId(type, id,false);
486
        if(couId != null) {
481
        Integer couId = calls.getCouId(type, id, false);
482
        if (couId != null) {
487 483
            JsonArray members = calls.getUserNamesByCouId(couId, false);
488 484
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
489 485
        } else {
......
501 497
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
502 498
    public Response getMembersEmail(@PathParam("type") String type, @PathParam("id") String id) {
503 499
        Integer couId = calls.getCouId(type, id, false);
504
        if(couId != null) {
500
        if (couId != null) {
505 501
            JsonArray members = calls.getUserEmailByCouId(couId, false);
506 502
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
507 503
        } else {
......
519 515
            "@AuthorizationService.curator(#type), @AuthorizationService.manager(#type, #id))")
520 516
    public Response getMembersId(@PathParam("type") String type, @PathParam("id") String id) {
521 517
        Integer couId = calls.getCouId(type, id, false);
522
        if(couId != null) {
518
        if (couId != null) {
523 519
            JsonArray members = calls.getUserIdByCouId(couId, false);
524 520
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(members).toString()).type(MediaType.APPLICATION_JSON).build();
525 521
        } else {
......
536 532
    public Response getMembersCount(@PathParam("type") String type, @PathParam("id") String id) {
537 533
        Integer couId = calls.getCouId(type, id, false);
538 534
        int count = 0;
539
        if(couId != null) {
535
        if (couId != null) {
540 536
            count = calls.getUserNamesByCouId(couId, false).size();
541 537
        }
542 538
        return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(count).toString()).type(MediaType.APPLICATION_JSON).build();
......
550 546
    @Produces(MediaType.APPLICATION_JSON)
551 547
    public Response getManagers(@PathParam("type") String type, @PathParam("id") String id) {
552 548
        Integer couId = calls.getCouId(type, id);
553
        if(couId != null) {
549
        if (couId != null) {
554 550
            JsonArray managers = calls.getUserNamesByCouId(couId, true);
555 551
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
556 552
        } else {
......
566 562
    @Produces(MediaType.APPLICATION_JSON)
567 563
    public Response getManagersEmail(@PathParam("type") String type, @PathParam("id") String id) {
568 564
        Integer couId = calls.getCouId(type, id);
569
        if(couId != null) {
565
        if (couId != null) {
570 566
            JsonArray managers = calls.getUserEmailByCouId(couId, true);
571 567
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
572 568
        } else {
......
582 578
    @Produces(MediaType.APPLICATION_JSON)
583 579
    public Response getManagersId(@PathParam("type") String type, @PathParam("id") String id) {
584 580
        Integer couId = calls.getCouId(type, id);
585
        if(couId != null) {
581
        if (couId != null) {
586 582
            JsonArray managers = calls.getUserIdByCouId(couId, true);
587 583
            return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(managers).toString()).type(MediaType.APPLICATION_JSON).build();
588 584
        } else {

Also available in: Unified diff