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) {
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
        return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
66
    }
67

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

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

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

    
104
    /**
105
     * 4. Get a couId by type.id
106
     *
107
     * @param type
108
     * @param id
109
     * @return
110
     */
111
    public Integer getCouId(String type, String id) {
112
        JsonArray cous = getCous();
113
        Integer couId = null;
114
        for (JsonElement cou : cous) {
115
            if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type) + "." + id)) {
116
                couId = cou.getAsJsonObject().get("Id").getAsInt();
117
            }
118
        }
119
        return couId;
120
    }
121

    
122
    /**
123
     * 5. Get User non admin roles
124
     */
125
    public JsonArray getRoles(Integer coPersonId) {
126
        Map<String, String> params = new HashMap<>();
127
        params.put("copersonid", coPersonId.toString());
128
        JsonElement response = httpUtils.get("co_person_roles.json", params);
129
        return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray();
130
    }
131

    
132
    /**
133
     * 6. Get Role id of User base on couId.
134
     */
135
    public Integer getRoleId(Integer coPersonId, Integer couId) {
136
        JsonArray roles = getRoles(coPersonId);
137
        for (JsonElement role : roles) {
138
            JsonObject object = role.getAsJsonObject();
139
            if (object.get("CouId").getAsInt() == couId && !object.get("Status").getAsString().equals("Deleted")) {
140
                return object.get("Id").getAsInt();
141
            }
142
        }
143
        return null;
144
    }
145

    
146
    /**
147
     * 7. Get User Groups
148
     */
149
    public JsonArray getUserGroups(Integer coPersonId) {
150
        Map<String, String> params = new HashMap<>();
151
        params.put("copersonid", coPersonId.toString());
152
        JsonElement response = httpUtils.get("co_groups.json", params);
153
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
154
    }
155

    
156
    /**
157
     * 8. Get User Admin Group of a Cou
158
     */
159
    public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) {
160
        Map<String, String> params = new HashMap<>();
161
        params.put("copersonid", coPersonId.toString());
162
        JsonElement response = httpUtils.get("co_groups.json", params);
163
        JsonArray roles = (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
164
        for (JsonElement role : roles) {
165
            JsonObject object = role.getAsJsonObject();
166
            if (object.get("CouId") != null && object.get("CouId").getAsInt() == couId) {
167
                if (object.get("Name").getAsString().contains("admins")) {
168
                    return object;
169
                }
170
            }
171
        }
172
        return null;
173
    }
174

    
175
    /**
176
     * 9. Get Groups of a Cou
177
     */
178
    public JsonArray getCouGroups(Integer couId) {
179
        Map<String, String> params = new HashMap<>();
180
        params.put("coid", coid);
181
        params.put("couid", couId.toString());
182
        JsonElement response = httpUtils.get("co_groups.json", params);
183
        return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray();
184
    }
185

    
186
    /**
187
     * 10. Get Admin Group of a Cou
188
     */
189
    public JsonObject getCouAdminGroup(Integer couId) {
190
        JsonArray groups = getCouGroups(couId);
191
        for (JsonElement group : groups) {
192
            if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) {
193
                return group.getAsJsonObject();
194
            }
195
        }
196
        return null;
197
    }
198

    
199
    /**
200
     * 11. Get users of a group
201
     */
202
    public JsonArray getGroupMembers(Integer coGroupId) {
203
        Map<String, String> params = new HashMap<>();
204
        params.put("cogroupid", coGroupId.toString());
205
        JsonElement response = httpUtils.get("co_group_members.json", params);
206
        return (response != null) ? response.getAsJsonObject().get("CoGroupMembers").getAsJsonArray() : new JsonArray();
207
    }
208

    
209

    
210
    /**
211
     * 12. Get Users' email of a Cou
212
     */
213
    public JsonArray getUserEmailByCouId(Integer couId, boolean admin) {
214
        Map<String, String> params = new HashMap<>();
215
        params.put("couid", couId.toString());
216
        if (admin) {
217
            params.put("admin", "true");
218
        }
219
        JsonElement response = httpUtils.get("email_addresses.json", params);
220
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray() : new JsonArray();
221
        JsonArray emails = new JsonArray();
222
        infos.forEach(info -> {
223
            JsonObject user = new JsonObject();
224
            user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString());
225
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
226
            emails.add(user);
227
        });
228
        return emails;
229
    }
230

    
231
    /**
232
     * 13. Get Users' names of a Cou
233
     */
234
    public JsonArray getUserNamesByCouId(Integer couId, boolean admin) {
235
        Map<String, String> params = new HashMap<>();
236
        params.put("couid", couId.toString());
237
        if (admin) {
238
            params.put("admin", "true");
239
        }
240
        JsonElement response = httpUtils.get("names.json", params);
241
        JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
242
        JsonArray names = new JsonArray();
243
        infos.forEach(info -> {
244
            JsonObject user = new JsonObject();
245
            user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString());
246
            user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString());
247
            names.add(user);
248
        });
249
        return names;
250
    }
251

    
252
    /**
253
     * 14. Assign a member role to a User
254
     */
255
    public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) {
256
        if (id != null) {
257
            httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
258
        } else {
259
            httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
260
        }
261
    }
262

    
263
    /**
264
     * 15. Remove a member role from a User
265
     */
266
    public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) {
267
        httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
268
    }
269

    
270
    /**
271
     * 16. Create a new role
272
     */
273
    public void createRole(Role role) {
274
        httpUtils.post("cous.json", jsonUtils.createNewCou(role));
275
    }
276

    
277
    /**
278
     * 17. Get User's email
279
     */
280
    public String getUserEmail(Integer coPersonId) {
281
        Map<String, String> params = new HashMap<>();
282
        params.put("copersonid", coPersonId.toString());
283
        JsonElement response = httpUtils.get("email_addresses.json", params);
284
        JsonObject info = (response != null) ? response.getAsJsonObject().get("EmailAddresses").getAsJsonArray().get(0).getAsJsonObject() : null;
285
        return (info != null) ? info.getAsJsonObject().get("Mail").getAsString() : null;
286
    }
287

    
288
    /**
289
     * 18. Get User's names
290
     */
291
    public String getUserNames(Integer coPersonId) {
292
        Map<String, String> params = new HashMap<>();
293
        params.put("copersonid", coPersonId.toString());
294
        JsonElement response = httpUtils.get("names.json", params);
295
        JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
296
        return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null;
297
    }
298

    
299
    /**
300
     * 14. Assign an admin role to a User
301
     */
302
    public void assignAdminRole(Integer coPersonId, Integer couId) {
303
        JsonObject group = getCouAdminGroup(couId);
304
        if (group != null) {
305
            httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
306
        }
307
    }
308

    
309
    /**
310
     * 15. Remove an admin role from a User
311
     */
312
    public void removeAdminRole(Integer coPersonId, Integer couId) {
313
        JsonObject adminGroup = this.getCouAdminGroup(couId);
314
        JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
315
        Integer id = null;
316
        for (JsonElement admin : admins) {
317
            if (admin.getAsJsonObject().get("Person").getAsJsonObject().get("Id").getAsInt() == coPersonId) {
318
                id = admin.getAsJsonObject().get("Id").getAsInt();
319
            }
320
        }
321
        if (id != null) {
322
            httpUtils.delete("co_group_members/" + id.toString() + ".json");
323
        }
324
    }
325
}
(6-6/9)