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