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.core.context.SecurityContextHolder;
|
12
|
import org.springframework.stereotype.Service;
|
13
|
|
14
|
import java.util.HashMap;
|
15
|
import java.util.Map;
|
16
|
|
17
|
@Service
|
18
|
public class RegistryCalls {
|
19
|
|
20
|
private static final Logger logger = Logger.getLogger(RegistryCalls.class);
|
21
|
|
22
|
@Value("${registry.coid}")
|
23
|
private String coid;
|
24
|
|
25
|
@Autowired
|
26
|
public HttpUtils httpUtils;
|
27
|
|
28
|
@Autowired
|
29
|
public JsonUtils jsonUtils;
|
30
|
|
31
|
|
32
|
private String mapType(String type) {
|
33
|
if(type.equals("organization")) {
|
34
|
type = "institution";
|
35
|
}
|
36
|
if(type.equals("ri")) {
|
37
|
type = "community";
|
38
|
}
|
39
|
return type;
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* 1. Get CoPersonId by Email
|
44
|
*/
|
45
|
public Integer getCoPersonIdByEmail() {
|
46
|
try {
|
47
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
48
|
String email = authentication.getUserInfo().getEmail();
|
49
|
Map<String, String> params = new HashMap<>();
|
50
|
params.put("coid", coid);
|
51
|
params.put("mail", email);
|
52
|
JsonElement response = httpUtils.get("co_people.json", params);
|
53
|
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
|
54
|
} catch (Exception e) {
|
55
|
logger.error("Get User info: An error occurred ", e);
|
56
|
return null;
|
57
|
}
|
58
|
}
|
59
|
|
60
|
public Integer getCoPersonIdByEmail(String email) {
|
61
|
Map<String, String> params = new HashMap<>();
|
62
|
params.put("coid", coid);
|
63
|
params.put("mail", email);
|
64
|
JsonElement response = httpUtils.get("co_people.json", params);
|
65
|
if(response != null) {
|
66
|
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray();
|
67
|
if(coPeople.size() > 0) {
|
68
|
return coPeople.get(0).getAsJsonObject().get("Id").getAsInt();
|
69
|
}
|
70
|
}
|
71
|
return null;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* 2. Get CoPersonId by AAI identifier
|
76
|
*/
|
77
|
public Integer getCoPersonIdByIdentifier() {
|
78
|
try {
|
79
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
80
|
String sub = authentication.getUserInfo().getSub();
|
81
|
Map<String, String> params = new HashMap<>();
|
82
|
params.put("coid", coid);
|
83
|
params.put("search.identifier", sub);
|
84
|
JsonElement response = httpUtils.get("co_people.json", params);
|
85
|
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
|
86
|
} catch (Exception e) {
|
87
|
logger.error("Get User info: An error occurred ", e);
|
88
|
return null;
|
89
|
}
|
90
|
}
|
91
|
|
92
|
public Integer getCoPersonIdByIdentifier(String sub) {
|
93
|
Map<String, String> params = new HashMap<>();
|
94
|
params.put("coid", coid);
|
95
|
params.put("search.identifier", sub);
|
96
|
JsonElement response = httpUtils.get("co_people.json", params);
|
97
|
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* 3. Get all OpenAIRE cous
|
102
|
*/
|
103
|
public JsonArray getCous() {
|
104
|
Map<String, String> params = new HashMap<>();
|
105
|
params.put("coid", coid);
|
106
|
JsonElement response = httpUtils.get("cous.json", params);
|
107
|
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* 4. Get a couId by type.id
|
112
|
*
|
113
|
* @param type
|
114
|
* @param id
|
115
|
* @return
|
116
|
*/
|
117
|
public Integer getCouId(String type, String id) {
|
118
|
JsonArray cous = getCous();
|
119
|
Integer couId = null;
|
120
|
for (JsonElement cou : cous) {
|
121
|
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type) + "." + id)) {
|
122
|
couId = cou.getAsJsonObject().get("Id").getAsInt();
|
123
|
}
|
124
|
}
|
125
|
return couId;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* 5. Get User non admin roles
|
130
|
*/
|
131
|
public JsonArray getRoles(Integer coPersonId) {
|
132
|
Map<String, String> params = new HashMap<>();
|
133
|
params.put("copersonid", coPersonId.toString());
|
134
|
JsonElement response = httpUtils.get("co_person_roles.json", params);
|
135
|
return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* 6. Get Role id of User base on couId.
|
140
|
*/
|
141
|
public Integer getRoleId(Integer coPersonId, Integer couId) {
|
142
|
JsonArray roles = getRoles(coPersonId);
|
143
|
for (JsonElement role : roles) {
|
144
|
JsonObject object = role.getAsJsonObject();
|
145
|
if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
|
146
|
return object.get("Id").getAsInt();
|
147
|
}
|
148
|
}
|
149
|
return null;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* 7. Get User Groups
|
154
|
*/
|
155
|
public JsonArray getUserGroups(Integer coPersonId) {
|
156
|
Map<String, String> params = new HashMap<>();
|
157
|
params.put("copersonid", coPersonId.toString());
|
158
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
159
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* 8. Get User Admin Group of a Cou
|
164
|
*/
|
165
|
public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
|
166
|
Map<String, String> params = new HashMap<>();
|
167
|
params.put("copersonid", coPersonId.toString());
|
168
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
169
|
JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
170
|
for (JsonElement role : roles) {
|
171
|
JsonObject object = role.getAsJsonObject();
|
172
|
if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
|
173
|
if (object.get("Name").getAsString().contains("admins")) {
|
174
|
return object;
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
return null;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* 9. Get Groups of a Cou
|
183
|
*/
|
184
|
public JsonArray getCouGroups(Integer couId) {
|
185
|
Map<String, String> params = new HashMap<>();
|
186
|
params.put("coid", coid);
|
187
|
params.put("couid", couId.toString());
|
188
|
JsonElement response = httpUtils.get("co_groups.json", params);
|
189
|
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* 10. Get Admin Group of a Cou
|
194
|
*/
|
195
|
public JsonObject getCouAdminGroup(Integer couId) {
|
196
|
JsonArray groups = getCouGroups(couId);
|
197
|
for (JsonElement group : groups) {
|
198
|
if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
|
199
|
return group.getAsJsonObject();
|
200
|
}
|
201
|
}
|
202
|
return null;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* 11. Get users of a group
|
207
|
*/
|
208
|
public JsonArray getGroupMembers(Integer coGroupId) {
|
209
|
Map<String, String> params = new HashMap<>();
|
210
|
params.put("cogroupid", coGroupId.toString());
|
211
|
JsonElement response = httpUtils.get("co_group_members.json", params);
|
212
|
return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
|
213
|
}
|
214
|
|
215
|
|
216
|
/**
|
217
|
* 12. Get Users' email of a Cou
|
218
|
*/
|
219
|
public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
|
220
|
Map<String, String> params = new HashMap<>();
|
221
|
params.put("couid", couId.toString());
|
222
|
if (admin) {
|
223
|
params.put("admin", "true");
|
224
|
}
|
225
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
226
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
|
227
|
JsonArray emails = new JsonArray();
|
228
|
infos.forEach(info -> {
|
229
|
JsonObject user = new JsonObject();
|
230
|
user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
|
231
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
232
|
emails.add(user);
|
233
|
});
|
234
|
return emails;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* 13. Get Users' names of a Cou
|
239
|
*/
|
240
|
public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
|
241
|
Map<String, String> params = new HashMap<>();
|
242
|
params.put("couid", couId.toString());
|
243
|
if (admin) {
|
244
|
params.put("admin", "true");
|
245
|
}
|
246
|
JsonElement response = httpUtils.get("names.json", params);
|
247
|
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
|
248
|
JsonArray names = new JsonArray();
|
249
|
infos.forEach(info -> {
|
250
|
JsonObject user = new JsonObject();
|
251
|
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
|
252
|
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
|
253
|
names.add(user);
|
254
|
});
|
255
|
return names;
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* 14. Assign a member role to a User
|
260
|
*/
|
261
|
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
262
|
if (id != null) {
|
263
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
264
|
} else {
|
265
|
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
|
266
|
}
|
267
|
}
|
268
|
|
269
|
/**
|
270
|
* 15. Remove a member role from a User
|
271
|
*/
|
272
|
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
|
273
|
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* 16. Create a new role
|
278
|
*/
|
279
|
public void createRole(Role role) {
|
280
|
httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* 17. Get User's email
|
285
|
*/
|
286
|
public String getUserEmail(Integer coPersonId) {
|
287
|
Map<String, String> params = new HashMap<>();
|
288
|
params.put("copersonid", coPersonId.toString());
|
289
|
JsonElement response = httpUtils.get("email_addresses.json", params);
|
290
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
|
291
|
return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
|
292
|
}
|
293
|
|
294
|
/**
|
295
|
* 18. Get User's names
|
296
|
*/
|
297
|
public String getUserNames(Integer coPersonId) {
|
298
|
Map<String, String> params = new HashMap<>();
|
299
|
params.put("copersonid", coPersonId.toString());
|
300
|
JsonElement response = httpUtils.get("names.json", params);
|
301
|
JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
|
302
|
return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
|
303
|
}
|
304
|
|
305
|
/**
|
306
|
* 14. Assign an admin role to a User
|
307
|
*/
|
308
|
public void assignAdminRole(Integer coPersonId, Integer couId) {
|
309
|
JsonObject group = getCouAdminGroup(couId);
|
310
|
if (group != null) {
|
311
|
httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
|
312
|
}
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* 15. Remove an admin role from a User
|
317
|
*/
|
318
|
public void removeAdminRole(Integer coPersonId, Integer couId) {
|
319
|
JsonObject adminGroup = this.getCouAdminGroup(couId);
|
320
|
JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
|
321
|
Integer id = null;
|
322
|
for (JsonElement admin : admins) {
|
323
|
if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
|
324
|
id = admin.getAsJsonObject().get("Id").getAsInt();
|
325
|
}
|
326
|
}
|
327
|
if (id != null) {
|
328
|
httpUtils.delete("co_group_members/" + id.toString() + ".json");
|
329
|
}
|
330
|
}
|
331
|
}
|