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
|
for (JsonElement cou : cous) {
|
146
|
if (cou.getAsJsonObject().get("Name").getAsString().toLowerCase().equals(name.toLowerCase())) {
|
147
|
return cou.getAsJsonObject().get("Id").getAsInt();
|
148
|
}
|
149
|
}
|
150
|
return null;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* 4.2 Get a couId by type.id with/without mapping type
|
155
|
*
|
156
|
* @param type
|
157
|
* @param id
|
158
|
* @return
|
159
|
*/
|
160
|
public Integer getCouId(String type, String id, boolean communityMap) {
|
161
|
return getCouId(mapType(type, communityMap) + "." + id);
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* 4.3 Get a couId by type.id with mapping type
|
166
|
*
|
167
|
* @param type
|
168
|
* @param id
|
169
|
* @return
|
170
|
*/
|
171
|
public Integer getCouId(String type, String id) {
|
172
|
return getCouId(type, id, true);
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* 5. Get User non admin roles
|
177
|
*/
|
178
|
public JsonArray getRoles(Integer coPersonId) {
|
179
|
Map<String, String> params = new HashMap<>();
|
180
|
params.put("copersonid", coPersonId.toString());
|
181
|
JsonElement response = httpUtils.get("co_person_roles.json", params);
|
182
|
return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* 6. Get Role id of User base on couId.
|
187
|
*/
|
188
|
public Integer getRoleId(Integer coPersonId, Integer couId) {
|
189
|
JsonArray roles = getRoles(coPersonId);
|
190
|
for (JsonElement role : roles) {
|
191
|
JsonObject object = role.getAsJsonObject();
|
192
|
if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
|
193
|
return object.get("Id").getAsInt();
|
194
|
}
|
195
|
}
|
196
|
return null;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* 7. Get User Groups
|
201
|
*/
|
202
|
public JsonArray getUserGroups(Integer coPersonId) {
|
203
|
Map<String, String> params = new HashMap<>();
|
204
|
params.put("copersonid", coPersonId.toString());
|
205
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
206
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* 8. Get User Admin Group of a Cou
|
211
|
*/
|
212
|
public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
|
213
|
Map<String, String> params = new HashMap<>();
|
214
|
params.put("copersonid", coPersonId.toString());
|
215
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
216
|
JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
217
|
for (JsonElement role : roles) {
|
218
|
JsonObject object = role.getAsJsonObject();
|
219
|
if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
|
220
|
if (object.get("Name").getAsString().contains("admins")) {
|
221
|
return object;
|
222
|
}
|
223
|
}
|
224
|
}
|
225
|
return null;
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* 9. Get Groups of a Cou
|
230
|
*/
|
231
|
public JsonArray getCouGroups(Integer couId) {
|
232
|
Map<String, String> params = new HashMap<>();
|
233
|
params.put("coid", coid);
|
234
|
params.put("couid", couId.toString());
|
235
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
236
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* 10. Get Admin Group of a Cou
|
241
|
*/
|
242
|
public JsonObject getCouAdminGroup(Integer couId) {
|
243
|
JsonArray groups = getCouGroups(couId);
|
244
|
for (JsonElement group : groups) {
|
245
|
if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
|
246
|
return group.getAsJsonObject();
|
247
|
}
|
248
|
}
|
249
|
return null;
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* 11. Get users of a group
|
254
|
*/
|
255
|
public JsonArray getGroupMembers(Integer coGroupId) {
|
256
|
Map<String, String> params = new HashMap<>();
|
257
|
params.put("cogroupid", coGroupId.toString());
|
258
|
JsonElement response = httpUtils.get("co_group_members.json", params);
|
259
|
return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
|
260
|
}
|
261
|
|
262
|
|
263
|
/**
|
264
|
* 12. Get Users' email of a Cou
|
265
|
*/
|
266
|
public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
|
267
|
Map<String, String> params = new HashMap<>();
|
268
|
params.put("couid", couId.toString());
|
269
|
if (admin) {
|
270
|
params.put("admin", "true");
|
271
|
}
|
272
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
273
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
|
274
|
JsonArray emails = new JsonArray();
|
275
|
infos.forEach(info -> {
|
276
|
JsonObject user = new JsonObject();
|
277
|
boolean add = true;
|
278
|
String email = info.getAsJsonObject().get("Mail").getAsString();
|
279
|
for(JsonElement element : emails) {
|
280
|
if(element.getAsJsonObject().get("email").getAsString().equals(email)) {
|
281
|
add = false;
|
282
|
}
|
283
|
}
|
284
|
if(add) {
|
285
|
user.addProperty("email", email);
|
286
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
287
|
emails.add(user);
|
288
|
}
|
289
|
});
|
290
|
return emails;
|
291
|
}
|
292
|
|
293
|
/**
|
294
|
* 13. Get Users' names of a Cou
|
295
|
*/
|
296
|
public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
|
297
|
Map<String, String> params = new HashMap<>();
|
298
|
params.put("couid", couId.toString());
|
299
|
if (admin) {
|
300
|
params.put("admin", "true");
|
301
|
}
|
302
|
JsonElement response = httpUtils.get("names.json", params);
|
303
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
|
304
|
JsonArray names = new JsonArray();
|
305
|
infos.forEach(info -> {
|
306
|
JsonObject user = new JsonObject();
|
307
|
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
|
308
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
309
|
names.add(user);
|
310
|
});
|
311
|
return names;
|
312
|
}
|
313
|
|
314
|
/**
|
315
|
* 14. Get Users' identifiers of a Cou
|
316
|
*/
|
317
|
public JsonArray getUserIdByCouId(Integer couId, boolean admin) {
|
318
|
Map<String, String> params = new HashMap<>();
|
319
|
params.put("couid", couId.toString());
|
320
|
if (admin) {
|
321
|
params.put("admin", "true");
|
322
|
}
|
323
|
JsonElement response = httpUtils.get("identifiers.json", params);
|
324
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
|
325
|
JsonArray emails = new JsonArray();
|
326
|
infos.forEach(info -> {
|
327
|
JsonObject user = new JsonObject();
|
328
|
user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString());
|
329
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
330
|
emails.add(user);
|
331
|
});
|
332
|
return emails;
|
333
|
}
|
334
|
|
335
|
/**
|
336
|
* 15. Assign a member role to a User
|
337
|
*/
|
338
|
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
339
|
if (id != null) {
|
340
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
341
|
} else {
|
342
|
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
343
|
}
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* 16. Remove a member role from a User
|
348
|
*/
|
349
|
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
350
|
if(id != null) {
|
351
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
|
352
|
}
|
353
|
}
|
354
|
|
355
|
/**
|
356
|
* 17. Create a new role
|
357
|
*/
|
358
|
public Integer createRole(Role role) {
|
359
|
JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
360
|
return element.getAsJsonObject().get("Id").getAsInt();
|
361
|
}
|
362
|
|
363
|
/**
|
364
|
* 18. Get User's email
|
365
|
*/
|
366
|
public String getUserEmail(Integer coPersonId) {
|
367
|
Map<String, String> params = new HashMap<>();
|
368
|
params.put("copersonid", coPersonId.toString());
|
369
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
370
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
|
371
|
return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* 19. Get User's names
|
376
|
*/
|
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
|
/**
|
386
|
* 20. Get User's identifier
|
387
|
*/
|
388
|
public String getUserId(Integer coPersonId) {
|
389
|
Map<String, String> params = new HashMap<>();
|
390
|
params.put("copersonid", coPersonId.toString());
|
391
|
JsonElement response = httpUtils.get("identifiers.json", params);
|
392
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray().get(0).getAsJsonObject() : null;
|
393
|
return (info != null) ? info.getAsJsonObject().get("Identifier").getAsString() : null;
|
394
|
}
|
395
|
|
396
|
/**
|
397
|
* 21. Assign an admin role to a User
|
398
|
*/
|
399
|
public void assignAdminRole(Integer coPersonId, Integer couId) {
|
400
|
JsonObject group = getCouAdminGroup(couId);
|
401
|
if (group != null) {
|
402
|
httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
|
403
|
}
|
404
|
}
|
405
|
|
406
|
/**
|
407
|
* 22. Remove an admin role from a User
|
408
|
*/
|
409
|
public void removeAdminRole(Integer coPersonId, Integer couId) {
|
410
|
JsonObject adminGroup = this.getCouAdminGroup(couId);
|
411
|
JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
|
412
|
Integer id = null;
|
413
|
for (JsonElement admin : admins) {
|
414
|
if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
|
415
|
id = admin.getAsJsonObject().get("Id").getAsInt();
|
416
|
}
|
417
|
}
|
418
|
if (id != null) {
|
419
|
httpUtils.delete("co_group_members/" + id.toString() + ".json");
|
420
|
}
|
421
|
}
|
422
|
}
|