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