Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement.utils;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonElement;
5
import com.google.gson.JsonObject;
6
import eu.dnetlib.openaire.usermanagement.dto.Role;
7
import org.apache.log4j.Logger;
8
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Value;
11
import org.springframework.security.access.method.P;
12
import org.springframework.security.core.context.SecurityContextHolder;
13
import org.springframework.stereotype.Service;
14

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

    
20
@Service
21
public class RegistryCalls {
22

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

    
25
    @Value("${registry.coid}")
26
    private String coid;
27

    
28
    @Autowired
29
    public HttpUtils httpUtils;
30

    
31
    @Autowired
32
    public JsonUtils jsonUtils;
33

    
34

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

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

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

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

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

    
117
    /**
118
     * 3.1 Get OpenAIRE cous with a specific name(or substring)
119
     */
120
    public JsonArray getCous(String name) {
121
        Map<String, String> params = new HashMap<>();
122
        params.put("coid", coid);
123
        if(name != null) {
124
            params.put("name", name.toLowerCase());
125
        }
126
        JsonElement response = httpUtils.get("cous.json", params);
127
        return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
128
    }
129

    
130
    /**
131
     * 3.2 Get all OpenAIRE cous
132
     */
133
    public JsonArray getCous() {
134
        return getCous(null);
135
    }
136

    
137
    /**
138
     * 4.1 Get a couId by name
139
     *
140
     * @param name
141
     * @return
142
     */
143
    public Integer getCouId(String name) {
144
        JsonArray cous = getCous(name);
145
        Integer couId = null;
146
        for (JsonElement cou : cous) {
147
            if (cou.getAsJsonObject().get("Name").getAsString().toLowerCase().equals(name.toLowerCase())) {
148
                couId = cou.getAsJsonObject().get("Id").getAsInt();
149
            }
150
        }
151
        return couId;
152
    }
153

    
154
    /**
155
     * 4.2 Get a couId by type.id with/without mapping type
156
     *
157
     * @param type
158
     * @param id
159
     * @return
160
     */
161
    public Integer getCouId(String type, String id, boolean communityMap) {
162
        return getCouId(mapType(type, communityMap) + "." + id);
163
    }
164

    
165
    /**
166
     * 4.3 Get a couId by type.id with mapping type
167
     *
168
     * @param type
169
     * @param id
170
     * @return
171
     */
172
    public Integer getCouId(String type, String id) {
173
        return getCouId(type, id, true);
174
    }
175

    
176
    /**
177
     * 5. Get User non admin roles
178
     */
179
    public JsonArray getRoles(Integer coPersonId) {
180
        Map<String, String> params = new HashMap<>();
181
        params.put("copersonid", coPersonId.toString());
182
        JsonElement response = httpUtils.get("co_person_roles.json", params);
183
        return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
184
    }
185

    
186
    /**
187
     * 6. Get Role id of User base on couId.
188
     */
189
    public Integer getRoleId(Integer coPersonId, Integer couId) {
190
        JsonArray roles = getRoles(coPersonId);
191
        for (JsonElement role : roles) {
192
            JsonObject object = role.getAsJsonObject();
193
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
194
                return object.get("Id").getAsInt();
195
            }
196
        }
197
        return null;
198
    }
199

    
200
    /**
201
     * 7. Get User Groups
202
     */
203
    public JsonArray getUserGroups(Integer coPersonId) {
204
        Map<String, String> params = new HashMap<>();
205
        params.put("copersonid", coPersonId.toString());
206
        JsonElement response = httpUtils.get("co_groups.json", params);
207
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
208
    }
209

    
210
    /**
211
     * 8. Get User Admin Group of a Cou
212
     */
213
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
214
        Map<String, String> params = new HashMap<>();
215
        params.put("copersonid", coPersonId.toString());
216
        JsonElement response = httpUtils.get("co_groups.json", params);
217
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
218
        for (JsonElement role : roles) {
219
            JsonObject object = role.getAsJsonObject();
220
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
221
                if (object.get("Name").getAsString().contains("admins")) {
222
                    return object;
223
                }
224
            }
225
        }
226
        return null;
227
    }
228

    
229
    /**
230
     * 9. Get Groups of a Cou
231
     */
232
    public JsonArray getCouGroups(Integer couId) {
233
        Map<String, String> params = new HashMap<>();
234
        params.put("coid", coid);
235
        params.put("couid", couId.toString());
236
        JsonElement response = httpUtils.get("co_groups.json", params);
237
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
238
    }
239

    
240
    /**
241
     * 10. Get Admin Group of a Cou
242
     */
243
    public JsonObject getCouAdminGroup(Integer couId) {
244
        JsonArray groups = getCouGroups(couId);
245
        for (JsonElement group : groups) {
246
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
247
                return group.getAsJsonObject();
248
            }
249
        }
250
        return null;
251
    }
252

    
253
    /**
254
     * 11. Get users of a group
255
     */
256
    public JsonArray getGroupMembers(Integer coGroupId) {
257
        Map<String, String> params = new HashMap<>();
258
        params.put("cogroupid", coGroupId.toString());
259
        JsonElement response = httpUtils.get("co_group_members.json", params);
260
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
261
    }
262

    
263

    
264
    /**
265
     * 12. Get Users' email of a Cou
266
     */
267
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
268
        Map<String, String> params = new HashMap<>();
269
        params.put("couid", couId.toString());
270
        if (admin) {
271
            params.put("admin", "true");
272
        }
273
        JsonElement response = httpUtils.get("email_addresses.json", params);
274
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
275
        JsonArray emails = new JsonArray();
276
        infos.forEach(info -> {
277
            JsonObject user = new JsonObject();
278
            boolean add = true;
279
            String email = info.getAsJsonObject().get("Mail").getAsString();
280
            for(JsonElement element : emails) {
281
                if(element.getAsJsonObject().get("email").getAsString().equals(email)) {
282
                    add = false;
283
                }
284
            }
285
            if(add) {
286
                user.addProperty("email", email);
287
                user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
288
                emails.add(user);
289
            }
290
        });
291
        return emails;
292
    }
293

    
294
    /**
295
     * 13. Get Users' names of a Cou
296
     */
297
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
298
        Map<String, String> params = new HashMap<>();
299
        params.put("couid", couId.toString());
300
        if (admin) {
301
            params.put("admin", "true");
302
        }
303
        JsonElement response = httpUtils.get("names.json", params);
304
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
305
        JsonArray names = new JsonArray();
306
        infos.forEach(info -> {
307
            JsonObject user = new JsonObject();
308
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
309
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
310
            names.add(user);
311
        });
312
        return names;
313
    }
314

    
315
    /**
316
     * 14. Get Users' identifiers of a Cou
317
     */
318
    public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
319
        Map<String, String> params = new HashMap<>();
320
        params.put("couid", couId.toString());
321
        if (admin) {
322
            params.put("admin", "true");
323
        }
324
        JsonElement response = httpUtils.get("identifiers.json", params);
325
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
326
        JsonArray emails = new JsonArray();
327
        infos.forEach(info -> {
328
            JsonObject user = new JsonObject();
329
            user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
330
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
331
            emails.add(user);
332
        });
333
        return emails;
334
    }
335

    
336
    /**
337
     * 15. Assign a member role to a User
338
     */
339
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
340
        if (id != null) {
341
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
342
        } else {
343
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
344
        }
345
    }
346

    
347
    /**
348
     * 16. Remove a member role from a User
349
     */
350
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
351
        if(id != null) {
352
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
353
        }
354
    }
355

    
356
    /**
357
     * 17. Create a new role
358
     */
359
    public Integer createRole(Role role) {
360
        JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
361
        return element.getAsJsonObject().get("Id").getAsInt();
362
    }
363

    
364
    /**
365
     * 18. Get User's email
366
     */
367
    public String getUserEmail(Integer coPersonId) {
368
        Map<String, String> params = new HashMap<>();
369
        params.put("copersonid", coPersonId.toString());
370
        JsonElement response = httpUtils.get("email_addresses.json", params);
371
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
372
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
373
    }
374

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

    
386
    /**
387
     * 20. Get User's identifier
388
     */
389
    public String getUserId(Integer coPersonId) {
390
        Map<String, String> params = new HashMap<>();
391
        params.put("copersonid", coPersonId.toString());
392
        JsonElement response = httpUtils.get("identifiers.json", params);
393
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
394
        return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
395
    }
396

    
397
    /**
398
     * 21. Assign an admin role to a User
399
     */
400
    public void assignAdminRole(Integer coPersonId, Integer couId) {
401
        JsonObject group = getCouAdminGroup(couId);
402
        if (group != null) {
403
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
404
        }
405
    }
406

    
407
    /**
408
     * 22. Remove an admin role from a User
409
     */
410
    public void removeAdminRole(Integer coPersonId, Integer couId) {
411
        JsonObject adminGroup = this.getCouAdminGroup(couId);
412
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
413
        Integer id = null;
414
        for (JsonElement admin : admins) {
415
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
416
                id = admin.getAsJsonObject().get("Id").getAsInt();
417
            }
418
        }
419
        if (id != null) {
420
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
421
        }
422
    }
423
}
(6-6/9)