Project

General

Profile

1
package eu.dnetlib.repo.manager.service.aai.registry;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import eu.dnetlib.repo.manager.domain.dto.Role;
7
import eu.dnetlib.repo.manager.domain.dto.User;
8
import eu.dnetlib.repo.manager.service.aai.registry.utils.RegistryUtils;
9
import eu.dnetlib.repo.manager.utils.HttpUtils;
10
import org.apache.log4j.Logger;
11
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.security.core.context.SecurityContextHolder;
15
import org.springframework.stereotype.Service;
16

    
17
import java.net.URLEncoder;
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22

    
23
@Service
24
public class RegistryCalls implements AaiRegistryService {
25

    
26
    private static final Logger logger = Logger.getLogger(RegistryCalls.class);
27

    
28
    private final String coid;
29
    public final HttpUtils httpUtils;
30
    public final RegistryUtils jsonUtils;
31

    
32
    @Autowired
33
    RegistryCalls(@Value("${aai.registry.coid:2}") String coid,
34
                  HttpUtils httpUtils, RegistryUtils registryUtils) {
35
        this.coid = coid;
36
        this.httpUtils = httpUtils;
37
        this.jsonUtils = registryUtils;
38
    }
39

    
40
    private String mapType(String type, boolean communityMap) {
41
        if (type.equals("organization")) {
42
            type = "institution";
43
        } else if (type.equals("ri") && communityMap) {
44
            type = "community";
45
        }
46
        return type;
47
    }
48

    
49
    @Override
50
    public Integer getCoPersonIdByEmail() {
51
        try {
52
            OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
53
            String email = authentication.getUserInfo().getEmail();
54
            Map<String, String> params = new HashMap<>();
55
            params.put("coid", coid);
56
            params.put("mail", email);
57
            JsonElement response = httpUtils.get("co_people.json", params);
58
            return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
59
        } catch (Exception e) {
60
            logger.error("Get User info: An error occurred ", e);
61
            return null;
62
        }
63
    }
64

    
65
    @Override
66
    public Integer getCoPersonIdByEmail(String email) {
67
        Map<String, String> params = new HashMap<>();
68
        params.put("coid", coid);
69
        params.put("mail", email);
70
        JsonElement response = httpUtils.get("co_people.json", params);
71
        if (response != null) {
72
            JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
73
            if (coPeople.size() > 0) {
74
                return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
75
            }
76
        }
77
        return null;
78
    }
79

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

    
96
    @Override
97
    public Integer getCoPersonIdByIdentifier() {
98
        try {
99
            OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
100
            String sub = authentication.getUserInfo().getSub();
101
            Map<String, String> params = new HashMap<>();
102
            params.put("coid", coid);
103
            params.put("search.identifier", sub);
104
            JsonElement response = httpUtils.get("co_people.json", params);
105
            return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
106
        } catch (Exception e) {
107
            logger.error("Get User info: An error occurred ", e);
108
            return null;
109
        }
110
    }
111

    
112
    public Integer getCoPersonIdByIdentifier(String sub) {
113
        Map<String, String> params = new HashMap<>();
114
        params.put("coid", coid);
115
        params.put("search.identifier", sub);
116
        JsonElement response = httpUtils.get("co_people.json", params);
117
        return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
118
    }
119

    
120
    @Override
121
    public JsonArray getCous(String name) {
122
        Map<String, String> params = new HashMap<>();
123
        params.put("coid", coid);
124
        if (name != null) {
125
            params.put("name", URLEncoder.encode(name).toLowerCase());
126
        }
127
        JsonElement response = httpUtils.get("cous.json", params);
128
        return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
129
    }
130

    
131
    @Override
132
    public JsonArray getCous() {
133
        return getCous(null);
134
    }
135

    
136
    @Override
137
    public Integer getCouId(String name) {
138
        JsonArray cous = getCous(name);
139
        for (JsonElement cou : cous) {
140
            if (cou.getAsJsonObject().get("Name").getAsString().equalsIgnoreCase(name)) {
141
                return cou.getAsJsonObject().get("Id").getAsInt();
142
            }
143
        }
144
        return null;
145
    }
146

    
147
    @Override
148
    public Integer getCouId(String type, String id, boolean communityMap) {
149
        return getCouId(mapType(type, communityMap) + "." + id);
150
    }
151

    
152
    @Override
153
    public Integer getCouId(String type, String id) {
154
        return getCouId(type, id, true);
155
    }
156

    
157
    @Override
158
    public JsonArray getRoles(Integer coPersonId) {
159
        Map<String, String> params = new HashMap<>();
160
        params.put("copersonid", coPersonId.toString());
161
        JsonElement response = httpUtils.get("co_person_roles.json", params);
162
        return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
163
    }
164

    
165
    @Override
166
    public JsonArray getRolesWithStatus(Integer coPersonId, RoleStatus status) {
167
        JsonArray roles = getRoles(coPersonId);
168
        if (status == null) {
169
            return roles;
170
        }
171
        JsonArray activeRoles = new JsonArray();
172
        for (JsonElement role : roles) {
173
            if (role.getAsJsonObject().get("Status").getAsString().equalsIgnoreCase(status.toString())) {
174
                activeRoles.add(role);
175
            }
176
        }
177
        return activeRoles;
178
    }
179

    
180
    @Override
181
    public Integer getRoleId(Integer coPersonId, Integer couId) {
182
        JsonArray roles = getRoles(coPersonId);
183
        for (JsonElement role : roles) {
184
            JsonObject object = role.getAsJsonObject();
185
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
186
                return object.get("Id").getAsInt();
187
            }
188
        }
189
        return null;
190
    }
191

    
192
    @Override
193
    public JsonArray getUserGroups(Integer coPersonId) {
194
        Map<String, String> params = new HashMap<>();
195
        params.put("copersonid", coPersonId.toString());
196
        JsonElement response = httpUtils.get("co_groups.json", params);
197
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
198
    }
199

    
200
    @Override
201
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
202
        Map<String, String> params = new HashMap<>();
203
        params.put("copersonid", coPersonId.toString());
204
        JsonElement response = httpUtils.get("co_groups.json", params);
205
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
206
        for (JsonElement role : roles) {
207
            JsonObject object = role.getAsJsonObject();
208
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
209
                if (object.get("Name").getAsString().contains("admins")) {
210
                    return object;
211
                }
212
            }
213
        }
214
        return null;
215
    }
216

    
217
    @Override
218
    public JsonArray getCouGroups(Integer couId) {
219
        Map<String, String> params = new HashMap<>();
220
        params.put("coid", coid);
221
        params.put("couid", couId.toString());
222
        JsonElement response = httpUtils.get("co_groups.json", params);
223
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
224
    }
225

    
226
    @Override
227
    public JsonObject getCouAdminGroup(Integer couId) {
228
        JsonArray groups = getCouGroups(couId);
229
        for (JsonElement group : groups) {
230
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
231
                return group.getAsJsonObject();
232
            }
233
        }
234
        return null;
235
    }
236

    
237
    @Override
238
    public JsonArray getGroupMembers(Integer coGroupId) {
239
        Map<String, String> params = new HashMap<>();
240
        params.put("cogroupid", coGroupId.toString());
241
        JsonElement response = httpUtils.get("co_group_members.json", params);
242
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
243
    }
244

    
245

    
246
    @Override
247
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
248
        Map<String, String> params = new HashMap<>();
249
        if (couId == null) {
250
            throw new IllegalArgumentException("Provided 'couId' is null");
251
        }
252
        params.put("couid", couId.toString());
253
        if (admin) {
254
            params.put("admin", "true");
255
        }
256
        JsonElement response = httpUtils.get("email_addresses.json", params);
257
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
258
        JsonArray emails = new JsonArray();
259
        infos.forEach(info -> {
260
            JsonObject user = new JsonObject();
261
            boolean add = true;
262
            String email = info.getAsJsonObject().get("Mail").getAsString();
263
            for (JsonElement element : emails) {
264
                if (element.getAsJsonObject().get("email").getAsString().equals(email)) {
265
                    add = false;
266
                }
267
            }
268
            if (add) {
269
                user.addProperty("email", email);
270
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
271
                emails.add(user);
272
            }
273
        });
274
        return emails;
275
    }
276

    
277
    @Override
278
    public JsonArray getUsersByCouId(Integer couId) {
279
        Map<String, String> params = new HashMap<>();
280
        params.put("couid", couId.toString());
281
        JsonElement response = httpUtils.get("co_person_roles.json", params);
282
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
283
//        JsonArray users = new JsonArray();
284
//        infos.forEach(info -> {
285
//            JsonObject user = new JsonObject();
286
//            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
287
//            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
288
//            emails.add(user);
289
//        });
290
        return infos;
291
    }
292

    
293
    @Override
294
    public List<User> getUsers(Integer couId) {
295
        List<User> users = new ArrayList<>();
296
        JsonArray infos = getUserEmailByCouId(couId, false);
297

    
298
        infos.forEach(info -> {
299
            User user = new User();
300
            user.setEmail(info.getAsJsonObject().get("email").getAsString());
301

    
302
            users.add(user);
303
        });
304
        return users;
305
    }
306

    
307
    @Override
308
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
309
        Map<String, String> params = new HashMap<>();
310
        params.put("couid", couId.toString());
311
        if (admin) {
312
            params.put("admin", "true");
313
        }
314
        JsonElement response = httpUtils.get("names.json", params);
315
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
316
        JsonArray names = new JsonArray();
317
        infos.forEach(info -> {
318
            JsonObject user = new JsonObject();
319
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
320
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
321
            names.add(user);
322
        });
323
        return names;
324
    }
325

    
326
    @Override
327
    public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
328
        Map<String, String> params = new HashMap<>();
329
        params.put("couid", couId.toString());
330
        if (admin) {
331
            params.put("admin", "true");
332
        }
333
        JsonElement response = httpUtils.get("identifiers.json", params);
334
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
335
        JsonArray emails = new JsonArray();
336
        infos.forEach(info -> {
337
            JsonObject user = new JsonObject();
338
            user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
339
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
340
            emails.add(user);
341
        });
342
        return emails;
343
    }
344

    
345
    @Override
346
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
347
        if (id != null) {
348
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
349
        } else {
350
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
351
        }
352
    }
353

    
354
    @Override
355
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
356
        if (id != null) {
357
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
358
        }
359
    }
360

    
361
    @Override
362
    public Integer createRole(Role role) {
363
        JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
364
        return element.getAsJsonObject().get("Id").getAsInt();
365
    }
366

    
367
    @Override
368
    public String getUserEmail(Integer coPersonId) {
369
        Map<String, String> params = new HashMap<>();
370
        params.put("copersonid", coPersonId.toString());
371
        JsonElement response = httpUtils.get("email_addresses.json", params);
372
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
373
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
374
    }
375

    
376
    @Override
377
    public String getUserNames(Integer coPersonId) {
378
        Map<String, String> params = new HashMap<>();
379
        params.put("copersonid", coPersonId.toString());
380
        JsonElement response = httpUtils.get("names.json", params);
381
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
382
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
383
    }
384

    
385
    @Override
386
    public String getUserId(Integer coPersonId) {
387
        Map<String, String> params = new HashMap<>();
388
        params.put("copersonid", coPersonId.toString());
389
        JsonElement response = httpUtils.get("identifiers.json", params);
390
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
391
        return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
392
    }
393

    
394
    @Override
395
    public void assignAdminRole(Integer coPersonId, Integer couId) {
396
        JsonObject group = getCouAdminGroup(couId);
397
        if (group != null) {
398
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
399
        }
400
    }
401

    
402
    @Override
403
    public void removeAdminRole(Integer coPersonId, Integer couId) {
404
        JsonObject adminGroup = this.getCouAdminGroup(couId);
405
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
406
        Integer id = null;
407
        for (JsonElement admin : admins) {
408
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
409
                id = admin.getAsJsonObject().get("Id").getAsInt();
410
            }
411
        }
412
        if (id != null) {
413
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
414
        }
415
    }
416

    
417
    @Override
418
    public Map<Integer, String> getCouNames(List<Integer> couIds) {
419
        Map<Integer, String> idNameMap = new HashMap<>();
420
        for (Integer id : couIds) {
421
            idNameMap.put(id, null);
422
        }
423

    
424
        JsonArray cous = getCous();
425
        int count = 0;
426
        int total = couIds.size();
427
        for (JsonElement cou : cous) {
428
            if (count < total) {
429
                if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) {
430
                    idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString());
431
                    count++;
432
                }
433
            } else {
434
                break;
435
            }
436
        }
437
        return idNameMap;
438
    }
439
}
(2-2/2)