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.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, boolean communityMap) {
33
        if(type.equals("organization")) {
34
            type = "institution";
35
        } else if(type.equals("ri") && communityMap) {
36
            type = "community";
37
        }
38
        return type;
39
    }
40

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

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

    
73
    /**
74
     * 2. Get CoPersonId by AAI identifier
75
     */
76
    public Integer getCoPersonIdByIdentifier() {
77
        try {
78
            OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
79
            String sub = authentication.getUserInfo().getSub();
80
            Map<String, String> params = new HashMap<>();
81
            params.put("coid", coid);
82
            params.put("search.identifier", sub);
83
            JsonElement response = httpUtils.get("co_people.json", params);
84
            return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
85
        } catch (Exception e) {
86
            logger.error("Get User info: An error occurred ", e);
87
            return null;
88
        }
89
    }
90

    
91
    public Integer getCoPersonIdByIdentifier(String sub) {
92
        Map<String, String> params = new HashMap<>();
93
        params.put("coid", coid);
94
        params.put("search.identifier", sub);
95
        JsonElement response = httpUtils.get("co_people.json", params);
96
        return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
97
    }
98

    
99
    /**
100
     * 3. Get all OpenAIRE cous
101
     */
102
    public JsonArray getCous() {
103
        Map<String, String> params = new HashMap<>();
104
        params.put("coid", coid);
105
        JsonElement response = httpUtils.get("cous.json", params);
106
        return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
107
    }
108

    
109
    /**
110
     * 4. Get a couId by name
111
     *
112
     * @param name
113
     * @return
114
     */
115
    public Integer getCouId(String name) {
116
        JsonArray cous = getCous();
117
        Integer couId = null;
118
        for (JsonElement cou : cous) {
119
            if (cou.getAsJsonObject().get("Name").getAsString().equals(name)) {
120
                couId = cou.getAsJsonObject().get("Id").getAsInt();
121
            }
122
        }
123
        return couId;
124
    }
125

    
126
    /**
127
     * 4. Get a couId by type.id
128
     *
129
     * @param type
130
     * @param id
131
     * @return
132
     */
133
    public Integer getCouId(String type, String id) {
134
        return getCouId(type, id, true);
135
    }
136

    
137

    
138
    /**
139
     * 4. Get a couId by type.id without mapping type
140
     *
141
     * @param type
142
     * @param id
143
     * @return
144
     */
145
    public Integer getCouId(String type, String id, boolean communityMap) {
146
        JsonArray cous = getCous();
147
        Integer couId = null;
148
        for (JsonElement cou : cous) {
149
            if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type, communityMap) + "." + id)) {
150
                couId = cou.getAsJsonObject().get("Id").getAsInt();
151
            }
152
        }
153
        return couId;
154
    }
155

    
156
    /**
157
     * 5. Get User non admin roles
158
     */
159
    public JsonArray getRoles(Integer coPersonId) {
160
        Map<String, String> params = new HashMap<>();
161
        params.put("copersonid", coPersonId.toString());
162
        JsonElement response = httpUtils.get("co_person_roles.json", params);
163
        return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
164
    }
165

    
166
    /**
167
     * 6. Get Role id of User base on couId.
168
     */
169
    public Integer getRoleId(Integer coPersonId, Integer couId) {
170
        JsonArray roles = getRoles(coPersonId);
171
        for (JsonElement role : roles) {
172
            JsonObject object = role.getAsJsonObject();
173
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
174
                return object.get("Id").getAsInt();
175
            }
176
        }
177
        return null;
178
    }
179

    
180
    /**
181
     * 7. Get User Groups
182
     */
183
    public JsonArray getUserGroups(Integer coPersonId) {
184
        Map<String, String> params = new HashMap<>();
185
        params.put("copersonid", coPersonId.toString());
186
        JsonElement response = httpUtils.get("co_groups.json", params);
187
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
188
    }
189

    
190
    /**
191
     * 8. Get User Admin Group of a Cou
192
     */
193
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
194
        Map<String, String> params = new HashMap<>();
195
        params.put("copersonid", coPersonId.toString());
196
        JsonElement response = httpUtils.get("co_groups.json", params);
197
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
198
        for (JsonElement role : roles) {
199
            JsonObject object = role.getAsJsonObject();
200
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
201
                if (object.get("Name").getAsString().contains("admins")) {
202
                    return object;
203
                }
204
            }
205
        }
206
        return null;
207
    }
208

    
209
    /**
210
     * 9. Get Groups of a Cou
211
     */
212
    public JsonArray getCouGroups(Integer couId) {
213
        Map<String, String> params = new HashMap<>();
214
        params.put("coid", coid);
215
        params.put("couid", couId.toString());
216
        JsonElement response = httpUtils.get("co_groups.json", params);
217
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
218
    }
219

    
220
    /**
221
     * 10. Get Admin Group of a Cou
222
     */
223
    public JsonObject getCouAdminGroup(Integer couId) {
224
        JsonArray groups = getCouGroups(couId);
225
        for (JsonElement group : groups) {
226
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
227
                return group.getAsJsonObject();
228
            }
229
        }
230
        return null;
231
    }
232

    
233
    /**
234
     * 11. Get users of a group
235
     */
236
    public JsonArray getGroupMembers(Integer coGroupId) {
237
        Map<String, String> params = new HashMap<>();
238
        params.put("cogroupid", coGroupId.toString());
239
        JsonElement response = httpUtils.get("co_group_members.json", params);
240
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
241
    }
242

    
243

    
244
    /**
245
     * 12. Get Users' email of a Cou
246
     */
247
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
248
        Map<String, String> params = new HashMap<>();
249
        params.put("couid", couId.toString());
250
        if (admin) {
251
            params.put("admin", "true");
252
        }
253
        JsonElement response = httpUtils.get("email_addresses.json", params);
254
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
255
        JsonArray emails = new JsonArray();
256
        infos.forEach(info -> {
257
            JsonObject user = new JsonObject();
258
            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
259
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
260
            emails.add(user);
261
        });
262
        return emails;
263
    }
264

    
265
    /**
266
     * 13. Get Users' names of a Cou
267
     */
268
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
269
        Map<String, String> params = new HashMap<>();
270
        params.put("couid", couId.toString());
271
        if (admin) {
272
            params.put("admin", "true");
273
        }
274
        JsonElement response = httpUtils.get("names.json", params);
275
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
276
        JsonArray names = new JsonArray();
277
        infos.forEach(info -> {
278
            JsonObject user = new JsonObject();
279
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
280
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
281
            names.add(user);
282
        });
283
        return names;
284
    }
285

    
286
    /**
287
     * 14. Assign a member role to a User
288
     */
289
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
290
        if (id != null) {
291
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
292
        } else {
293
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
294
        }
295
    }
296

    
297
    /**
298
     * 15. Remove a member role from a User
299
     */
300
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
301
        httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
302
    }
303

    
304
    /**
305
     * 16. Create a new role
306
     */
307
    public void createRole(Role role) {
308
        httpUtils.post("cous.json", jsonUtils.createNewCou(role));
309
    }
310

    
311
    /**
312
     * 17. Get User's email
313
     */
314
    public String getUserEmail(Integer coPersonId) {
315
        Map<String, String> params = new HashMap<>();
316
        params.put("copersonid", coPersonId.toString());
317
        JsonElement response = httpUtils.get("email_addresses.json", params);
318
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
319
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
320
    }
321

    
322
    /**
323
     * 18. Get User's names
324
     */
325
    public String getUserNames(Integer coPersonId) {
326
        Map<String, String> params = new HashMap<>();
327
        params.put("copersonid", coPersonId.toString());
328
        JsonElement response = httpUtils.get("names.json", params);
329
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
330
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
331
    }
332

    
333
    /**
334
     * 14. Assign an admin role to a User
335
     */
336
    public void assignAdminRole(Integer coPersonId, Integer couId) {
337
        JsonObject group = getCouAdminGroup(couId);
338
        if (group != null) {
339
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
340
        }
341
    }
342

    
343
    /**
344
     * 15. Remove an admin role from a User
345
     */
346
    public void removeAdminRole(Integer coPersonId, Integer couId) {
347
        JsonObject adminGroup = this.getCouAdminGroup(couId);
348
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
349
        Integer id = null;
350
        for (JsonElement admin : admins) {
351
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
352
                id = admin.getAsJsonObject().get("Id").getAsInt();
353
            }
354
        }
355
        if (id != null) {
356
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
357
        }
358
    }
359
}
(6-6/9)