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.service.aai.registry.utils.RegistryUtils;
8
import eu.dnetlib.repo.manager.utils.HttpUtils;
9
import org.apache.log4j.Logger;
10
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.security.core.context.SecurityContextHolder;
14
import org.springframework.stereotype.Service;
15

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

    
21
@Service
22
public class RegistryCalls implements AaiRegistryService {
23

    
24
    private static final Logger logger = Logger.getLogger(RegistryCalls.class);
25

    
26
    private final String coid;
27
    public final HttpUtils httpUtils;
28
    public final RegistryUtils jsonUtils;
29

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

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

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

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

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

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

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

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

    
129
    @Override
130
    public JsonArray getCous() {
131
        return getCous(null);
132
    }
133

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

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

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

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

    
163
    @Override
164
    public Integer getRoleId(Integer coPersonId, Integer couId) {
165
        JsonArray roles = getRoles(coPersonId);
166
        for (JsonElement role : roles) {
167
            JsonObject object = role.getAsJsonObject();
168
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
169
                return object.get("Id").getAsInt();
170
            }
171
        }
172
        return null;
173
    }
174

    
175
    @Override
176
    public JsonArray getUserGroups(Integer coPersonId) {
177
        Map<String, String> params = new HashMap<>();
178
        params.put("copersonid", coPersonId.toString());
179
        JsonElement response = httpUtils.get("co_groups.json", params);
180
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
181
    }
182

    
183
    @Override
184
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
185
        Map<String, String> params = new HashMap<>();
186
        params.put("copersonid", coPersonId.toString());
187
        JsonElement response = httpUtils.get("co_groups.json", params);
188
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
189
        for (JsonElement role : roles) {
190
            JsonObject object = role.getAsJsonObject();
191
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
192
                if (object.get("Name").getAsString().contains("admins")) {
193
                    return object;
194
                }
195
            }
196
        }
197
        return null;
198
    }
199

    
200
    @Override
201
    public JsonArray getCouGroups(Integer couId) {
202
        Map<String, String> params = new HashMap<>();
203
        params.put("coid", coid);
204
        params.put("couid", couId.toString());
205
        JsonElement response = httpUtils.get("co_groups.json", params);
206
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
207
    }
208

    
209
    @Override
210
    public JsonObject getCouAdminGroup(Integer couId) {
211
        JsonArray groups = getCouGroups(couId);
212
        for (JsonElement group : groups) {
213
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
214
                return group.getAsJsonObject();
215
            }
216
        }
217
        return null;
218
    }
219

    
220
    @Override
221
    public JsonArray getGroupMembers(Integer coGroupId) {
222
        Map<String, String> params = new HashMap<>();
223
        params.put("cogroupid", coGroupId.toString());
224
        JsonElement response = httpUtils.get("co_group_members.json", params);
225
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
226
    }
227

    
228

    
229
    @Override
230
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
231
        Map<String, String> params = new HashMap<>();
232
        params.put("couid", couId.toString());
233
        if (admin) {
234
            params.put("admin", "true");
235
        }
236
        JsonElement response = httpUtils.get("email_addresses.json", params);
237
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
238
        JsonArray emails = new JsonArray();
239
        infos.forEach(info -> {
240
            JsonObject user = new JsonObject();
241
            boolean add = true;
242
            String email = info.getAsJsonObject().get("Mail").getAsString();
243
            for (JsonElement element : emails) {
244
                if (element.getAsJsonObject().get("email").getAsString().equals(email)) {
245
                    add = false;
246
                }
247
            }
248
            if (add) {
249
                user.addProperty("email", email);
250
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
251
                emails.add(user);
252
            }
253
        });
254
        return emails;
255
    }
256

    
257
    @Override
258
    public JsonArray getUsersByCouId(Integer couId) {
259
        Map<String, String> params = new HashMap<>();
260
        params.put("couid", couId.toString());
261
        JsonElement response = httpUtils.get("co_person_roles.json", params);
262
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
263
//        JsonArray users = new JsonArray();
264
//        infos.forEach(info -> {
265
//            JsonObject user = new JsonObject();
266
//            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
267
//            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
268
//            emails.add(user);
269
//        });
270
        return infos;
271
    }
272

    
273
    @Override
274
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
275
        Map<String, String> params = new HashMap<>();
276
        params.put("couid", couId.toString());
277
        if (admin) {
278
            params.put("admin", "true");
279
        }
280
        JsonElement response = httpUtils.get("names.json", params);
281
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
282
        JsonArray names = new JsonArray();
283
        infos.forEach(info -> {
284
            JsonObject user = new JsonObject();
285
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
286
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
287
            names.add(user);
288
        });
289
        return names;
290
    }
291

    
292
    @Override
293
    public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
294
        Map<String, String> params = new HashMap<>();
295
        params.put("couid", couId.toString());
296
        if (admin) {
297
            params.put("admin", "true");
298
        }
299
        JsonElement response = httpUtils.get("identifiers.json", params);
300
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
301
        JsonArray emails = new JsonArray();
302
        infos.forEach(info -> {
303
            JsonObject user = new JsonObject();
304
            user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
305
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
306
            emails.add(user);
307
        });
308
        return emails;
309
    }
310

    
311
    @Override
312
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
313
        if (id != null) {
314
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
315
        } else {
316
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
317
        }
318
    }
319

    
320
    @Override
321
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
322
        if (id != null) {
323
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
324
        }
325
    }
326

    
327
    @Override
328
    public Integer createRole(Role role) {
329
        JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
330
        return element.getAsJsonObject().get("Id").getAsInt();
331
    }
332

    
333
    @Override
334
    public String getUserEmail(Integer coPersonId) {
335
        Map<String, String> params = new HashMap<>();
336
        params.put("copersonid", coPersonId.toString());
337
        JsonElement response = httpUtils.get("email_addresses.json", params);
338
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
339
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
340
    }
341

    
342
    @Override
343
    public String getUserNames(Integer coPersonId) {
344
        Map<String, String> params = new HashMap<>();
345
        params.put("copersonid", coPersonId.toString());
346
        JsonElement response = httpUtils.get("names.json", params);
347
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
348
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
349
    }
350

    
351
    @Override
352
    public String getUserId(Integer coPersonId) {
353
        Map<String, String> params = new HashMap<>();
354
        params.put("copersonid", coPersonId.toString());
355
        JsonElement response = httpUtils.get("identifiers.json", params);
356
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
357
        return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
358
    }
359

    
360
    @Override
361
    public void assignAdminRole(Integer coPersonId, Integer couId) {
362
        JsonObject group = getCouAdminGroup(couId);
363
        if (group != null) {
364
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
365
        }
366
    }
367

    
368
    @Override
369
    public void removeAdminRole(Integer coPersonId, Integer couId) {
370
        JsonObject adminGroup = this.getCouAdminGroup(couId);
371
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
372
        Integer id = null;
373
        for (JsonElement admin : admins) {
374
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
375
                id = admin.getAsJsonObject().get("Id").getAsInt();
376
            }
377
        }
378
        if (id != null) {
379
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
380
        }
381
    }
382

    
383
    @Override
384
    public Map<Integer, String> getCouNames(List<Integer> couIds) {
385
        Map<Integer, String> idNameMap = new HashMap<>();
386
        for (Integer id : couIds) {
387
            idNameMap.put(id, null);
388
        }
389

    
390
        JsonArray cous = getCous();
391
        int count = 0;
392
        int total = couIds.size();
393
        for (JsonElement cou : cous) {
394
            if (count < total) {
395
                if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) {
396
                    idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString());
397
                    count++;
398
                }
399
            } else {
400
                break;
401
            }
402
        }
403
        return idNameMap;
404
    }
405
}
(2-2/2)