Revision 60749
Added by Antonis Koulalis about 3 years ago
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/service/AaiUserRoleServiceImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.service; |
|
2 |
|
|
3 |
import com.google.gson.JsonArray; |
|
4 |
import com.google.gson.JsonElement; |
|
5 |
import com.google.gson.JsonObject; |
|
6 |
import eu.dnetlib.repo.manager.domain.dto.Role; |
|
7 |
import eu.dnetlib.repo.manager.utils.HttpUtils; |
|
8 |
import eu.dnetlib.repo.manager.utils.JsonUtils; |
|
9 |
import org.apache.log4j.Logger; |
|
10 |
import org.mitre.openid.connect.model.OIDCAuthenticationToken; |
|
11 |
import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
import org.springframework.beans.factory.annotation.Value; |
|
13 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
14 |
import org.springframework.stereotype.Service; |
|
15 |
|
|
16 |
import java.util.HashMap; |
|
17 |
import java.util.Map; |
|
18 |
|
|
19 |
@Service |
|
20 |
public class AaiUserRoleServiceImpl implements AaiUserRoleService { |
|
21 |
|
|
22 |
private static final Logger logger = Logger.getLogger(AaiUserRoleServiceImpl.class); |
|
23 |
|
|
24 |
@Value("2") |
|
25 |
private String coid; |
|
26 |
|
|
27 |
@Autowired |
|
28 |
public HttpUtils httpUtils; |
|
29 |
|
|
30 |
@Autowired |
|
31 |
public JsonUtils jsonUtils; |
|
32 |
|
|
33 |
private String mapType(String type) { |
|
34 |
if(type.equals("datasource")) { |
|
35 |
type = "datasource"; |
|
36 |
} |
|
37 |
return type; |
|
38 |
} |
|
39 |
|
|
40 |
public String getRepoNameWithoutType(String fullName, String prefix) { |
|
41 |
if ( fullName != null && prefix != null && fullName.startsWith(prefix) ) { |
|
42 |
return fullName.substring(prefix.length()); |
|
43 |
} |
|
44 |
return null; |
|
45 |
} |
|
46 |
|
|
47 |
public String getRoleIdByRepoId(String repoId, String prefix) { |
|
48 |
String roleId = ""; |
|
49 |
if ( repoId != null && prefix != null ) { |
|
50 |
roleId = prefix + "." + repoId.replaceAll(":", "$"); |
|
51 |
return roleId; |
|
52 |
} else { |
|
53 |
return null; |
|
54 |
} |
|
55 |
|
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* 1. Get CoPersonId by Email |
|
60 |
* @param email |
|
61 |
* @return |
|
62 |
*/ |
|
63 |
public Integer getCoPersonIdByEmail(String email) { |
|
64 |
Map<String, String> params = new HashMap<>(); |
|
65 |
params.put("coid", coid); |
|
66 |
params.put("mail", email); |
|
67 |
JsonElement response = httpUtils.get("co_people.json", params); |
|
68 |
if(response != null) { |
|
69 |
JsonArray coPeople = response.getAsJsonObject().get("CoPeople").getAsJsonArray(); |
|
70 |
if(coPeople.size() > 0) { |
|
71 |
return coPeople.get(0).getAsJsonObject().get("Id").getAsInt(); |
|
72 |
} |
|
73 |
} |
|
74 |
return null; |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* 2. Get CoPersonId by AAI identifier |
|
79 |
*/ |
|
80 |
public Integer getCoPersonIdByIdentifier() { |
|
81 |
try { |
|
82 |
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
|
83 |
String sub = authentication.getUserInfo().getSub(); |
|
84 |
Map<String, String> params = new HashMap<>(); |
|
85 |
params.put("coid", coid); |
|
86 |
params.put("search.identifier", sub); |
|
87 |
JsonElement response = httpUtils.get("co_people.json", params); |
|
88 |
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null; |
|
89 |
} catch (Exception e) { |
|
90 |
logger.error("Get User info: An error occurred ", e); |
|
91 |
return null; |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* 3. Get all OpenAIRE cous |
|
97 |
*/ |
|
98 |
public JsonArray getCous() { |
|
99 |
Map<String, String> params = new HashMap<>(); |
|
100 |
params.put("coid", coid); |
|
101 |
JsonElement response = httpUtils.get("cous.json", params); |
|
102 |
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* 4. Get a couId by type.id |
|
107 |
* |
|
108 |
* @param type |
|
109 |
* @param id It is the datasourceId (e.g openaire____$$45e3c7b69bcee6cc5fa945c9e183deb9) |
|
110 |
* @return |
|
111 |
*/ |
|
112 |
public Integer getCouId(String type, String id) { |
|
113 |
JsonArray cous = getCous(); |
|
114 |
Integer couId = null; |
|
115 |
for (JsonElement cou : cous) { |
|
116 |
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type) + "." + id)) { |
|
117 |
couId = cou.getAsJsonObject().get("Id").getAsInt(); |
|
118 |
} |
|
119 |
} |
|
120 |
return couId; |
|
121 |
} |
|
122 |
|
|
123 |
/** |
|
124 |
* 5. Get User non admin roles |
|
125 |
* @param coPersonId |
|
126 |
* @return |
|
127 |
*/ |
|
128 |
public JsonArray getRoles(Integer coPersonId) { |
|
129 |
Map<String, String> params = new HashMap<>(); |
|
130 |
params.put("copersonid", coPersonId.toString()); |
|
131 |
JsonElement response = httpUtils.get("co_person_roles.json", params); |
|
132 |
return (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray(); |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* 6. Get Role id of User base on couId. |
|
137 |
* @param coPersonId |
|
138 |
* @param couId |
|
139 |
* @return |
|
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 |
* 16. Create a new role |
|
154 |
* @param role |
|
155 |
*/ |
|
156 |
public void createRole(Role role) { |
|
157 |
httpUtils.post("cous.json", jsonUtils.createNewCou(role)); |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* 14. Assign a member role to a User |
|
162 |
* @param coPersonId The id of a person in OpenAIRE |
|
163 |
* @param couId The id of a role in OpenAIRE (result of getCouId) |
|
164 |
* @param id The id that is returned from getRoleId (role's id) |
|
165 |
*/ |
|
166 |
public void assignMemberRole(Integer coPersonId, Integer couId, Integer id) { |
|
167 |
if (id != null) { |
|
168 |
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Active")); |
|
169 |
} else { |
|
170 |
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active")); |
|
171 |
} |
|
172 |
} |
|
173 |
|
|
174 |
/** |
|
175 |
* 15. Remove a member role from a User |
|
176 |
* @param coPersonId The id of a person in OpenAIRE |
|
177 |
* @param couId The id of a role in OpenAIRE (result of getCouId) |
|
178 |
* @param id The id that is returned from getRoleId (role's id) |
|
179 |
*/ |
|
180 |
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) { |
|
181 |
httpUtils.put("co_person_roles/" + id.toString() + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted")); |
|
182 |
} |
|
183 |
|
|
184 |
/** |
|
185 |
* 12. Get All Users that have a specific role |
|
186 |
*/ |
|
187 |
public JsonArray getUsersByCouId(Integer couId) { |
|
188 |
Map<String, String> params = new HashMap<>(); |
|
189 |
params.put("couid", couId.toString()); |
|
190 |
JsonElement response = httpUtils.get("co_person_roles.json", params); |
|
191 |
JsonArray infos = (response != null) ? response.getAsJsonObject().get("CoPersonRoles").getAsJsonArray() : new JsonArray(); |
|
192 |
// JsonArray users = new JsonArray(); |
|
193 |
// infos.forEach(info -> { |
|
194 |
// JsonObject user = new JsonObject(); |
|
195 |
// user.addProperty("email", info.getAsJsonObject().get("Mail").getAsString()); |
|
196 |
// user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); |
|
197 |
// emails.add(user); |
|
198 |
// }); |
|
199 |
return infos; |
|
200 |
} |
|
201 |
|
|
202 |
/** |
|
203 |
* 9. Get Groups of a Cou |
|
204 |
*/ |
|
205 |
public JsonArray getCouGroups(Integer couId) { |
|
206 |
Map<String, String> params = new HashMap<>(); |
|
207 |
params.put("coid", coid); |
|
208 |
params.put("couid", couId.toString()); |
|
209 |
JsonElement response = httpUtils.get("co_groups.json", params); |
|
210 |
return (response != null) ? response.getAsJsonObject().get("CoGroups").getAsJsonArray() : new JsonArray(); |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* 10. Get Admin Group of a Cou |
|
215 |
*/ |
|
216 |
public JsonObject getCouAdminGroup(Integer couId) { |
|
217 |
JsonArray groups = getCouGroups(couId); |
|
218 |
for (JsonElement group : groups) { |
|
219 |
if (group.getAsJsonObject().get("Name").getAsString().contains("admins")) { |
|
220 |
return group.getAsJsonObject(); |
|
221 |
} |
|
222 |
} |
|
223 |
return null; |
|
224 |
} |
|
225 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/service/AaiUserRoleService.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.service; |
|
2 |
|
|
3 |
import com.google.gson.JsonArray; |
|
4 |
import eu.dnetlib.repo.manager.domain.dto.Role; |
|
5 |
|
|
6 |
public interface AaiUserRoleService { |
|
7 |
|
|
8 |
/** |
|
9 |
* 1. Get CoPersonId by Email |
|
10 |
*/ |
|
11 |
Integer getCoPersonIdByEmail(String email); |
|
12 |
|
|
13 |
Integer getCoPersonIdByIdentifier(); |
|
14 |
|
|
15 |
|
|
16 |
/** |
|
17 |
* 3. Get all OpenAIRE cous |
|
18 |
*/ |
|
19 |
JsonArray getCous(); |
|
20 |
|
|
21 |
/** |
|
22 |
* 4. Get a couId by type.id |
|
23 |
* |
|
24 |
* @param type |
|
25 |
* @param id |
|
26 |
* @return |
|
27 |
*/ |
|
28 |
Integer getCouId(String type, String id); |
|
29 |
|
|
30 |
/** |
|
31 |
* 5. Get User non admin roles |
|
32 |
*/ |
|
33 |
JsonArray getRoles(Integer coPersonId); |
|
34 |
|
|
35 |
/** |
|
36 |
* 6. Get Role id of User base on couId. |
|
37 |
*/ |
|
38 |
Integer getRoleId(Integer coPersonId, Integer couId); |
|
39 |
|
|
40 |
/** |
|
41 |
* 16. Create a new role |
|
42 |
*/ |
|
43 |
void createRole(Role role); |
|
44 |
|
|
45 |
/** |
|
46 |
* 14. Assign a member role to a User |
|
47 |
*/ |
|
48 |
void assignMemberRole(Integer coPersonId, Integer couId, Integer id); |
|
49 |
|
|
50 |
/** |
|
51 |
* 15. Remove a member role from a User |
|
52 |
*/ |
|
53 |
void removeMemberRole(Integer coPersonId, Integer couId, Integer id); |
|
54 |
|
|
55 |
|
|
56 |
/** |
|
57 |
* Util function to remove the datasource prefix in role Id |
|
58 |
* @param fullName |
|
59 |
* @param prefix |
|
60 |
* @return |
|
61 |
*/ |
|
62 |
String getRepoNameWithoutType(String fullName, String prefix); |
|
63 |
|
|
64 |
/** |
|
65 |
* Util function to transform repoId name to roleId name |
|
66 |
* @param repoId |
|
67 |
* @param prefix |
|
68 |
* @return |
|
69 |
*/ |
|
70 |
String getRoleIdByRepoId(String repoId, String prefix); |
|
71 |
|
|
72 |
/** |
|
73 |
* 12. Get All Users that have a specific role |
|
74 |
* @param couId |
|
75 |
* @return |
|
76 |
*/ |
|
77 |
JsonArray getUsersByCouId(Integer couId); |
|
78 |
|
|
79 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/utils/HttpUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.utils; |
|
2 |
|
|
3 |
import com.google.gson.JsonElement; |
|
4 |
import com.google.gson.JsonObject; |
|
5 |
import com.google.gson.JsonParser; |
|
6 |
import org.apache.commons.codec.binary.Base64; |
|
7 |
import org.apache.log4j.Logger; |
|
8 |
import org.springframework.beans.factory.annotation.Value; |
|
9 |
import org.springframework.http.*; |
|
10 |
import org.springframework.stereotype.Component; |
|
11 |
import org.springframework.web.client.RestTemplate; |
|
12 |
|
|
13 |
import java.nio.charset.Charset; |
|
14 |
import java.util.Map; |
|
15 |
|
|
16 |
@Component |
|
17 |
public class HttpUtils { |
|
18 |
|
|
19 |
private static final Logger logger = Logger.getLogger(HttpUtils.class); |
|
20 |
|
|
21 |
// TODO: To be changed the values |
|
22 |
// @Value("https://aai.openaire.eu/registry/") |
|
23 |
@Value("https://openaire-dev.aai-dev.grnet.gr/registry/") |
|
24 |
private String issuer; |
|
25 |
|
|
26 |
@Value("kostis30fylloy") |
|
27 |
private String user; |
|
28 |
|
|
29 |
@Value("fynhWc7F*2y4me4U") |
|
30 |
private String password; |
|
31 |
|
|
32 |
public JsonElement post(String path, JsonObject body) { |
|
33 |
RestTemplate restTemplate = new RestTemplate(); |
|
34 |
HttpHeaders headers = createHeaders(user, password); |
|
35 |
headers.setContentType(MediaType.APPLICATION_JSON); |
|
36 |
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); |
|
37 |
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class); |
|
38 |
if(responseEntity.getBody() != null) { |
|
39 |
return new JsonParser().parse(responseEntity.getBody()); |
|
40 |
} else { |
|
41 |
return null; |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
public JsonElement put(String path, JsonObject body) { |
|
46 |
RestTemplate restTemplate = new RestTemplate(); |
|
47 |
HttpHeaders headers = createHeaders(user, password); |
|
48 |
headers.setContentType(MediaType.APPLICATION_JSON); |
|
49 |
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); |
|
50 |
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class); |
|
51 |
if(responseEntity.getBody() != null) { |
|
52 |
return new JsonParser().parse(responseEntity.getBody()); |
|
53 |
} else { |
|
54 |
return null; |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
public JsonElement get(String path, Map<String, String> params) { |
|
59 |
RestTemplate restTemplate = new RestTemplate(); |
|
60 |
String url = issuer + path + ((params != null) ? createParams(params) : null); |
|
61 |
ResponseEntity<String> responseEntity = restTemplate.exchange |
|
62 |
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class); |
|
63 |
if(responseEntity.getBody() != null) { |
|
64 |
return new JsonParser().parse(responseEntity.getBody()); |
|
65 |
} else { |
|
66 |
return null; |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
public JsonElement delete(String path) { |
|
71 |
RestTemplate restTemplate = new RestTemplate(); |
|
72 |
String url = issuer + path; |
|
73 |
ResponseEntity<String> responseEntity = restTemplate.exchange |
|
74 |
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class); |
|
75 |
if(responseEntity.getBody() != null) { |
|
76 |
return new JsonParser().parse(responseEntity.getBody()); |
|
77 |
} else { |
|
78 |
return null; |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
|
|
83 |
private String createParams(Map<String, String> params) { |
|
84 |
StringBuilder ret = new StringBuilder("?"); |
|
85 |
int count = 0; |
|
86 |
for (Map.Entry<String, String> param : params.entrySet()) { |
|
87 |
ret.append(param.getKey()).append("=").append(param.getValue()); |
|
88 |
count++; |
|
89 |
if (count != params.entrySet().size()) { |
|
90 |
ret.append("&"); |
|
91 |
} |
|
92 |
} |
|
93 |
return ret.toString(); |
|
94 |
} |
|
95 |
|
|
96 |
private HttpHeaders createHeaders(String username, String password) { |
|
97 |
return new HttpHeaders() {{ |
|
98 |
String auth = username + ":" + password; |
|
99 |
byte[] encodedAuth = Base64.encodeBase64( |
|
100 |
auth.getBytes(Charset.forName("US-ASCII"))); |
|
101 |
String authHeader = "Basic " + new String(encodedAuth); |
|
102 |
set("Authorization", authHeader); |
|
103 |
}}; |
|
104 |
} |
|
105 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/utils/AuthorizationService.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.utils; |
|
2 |
|
|
3 |
import org.springframework.stereotype.Component; |
|
4 |
|
|
5 |
@Component("AuthorizationService") |
|
6 |
public class AuthorizationService { |
|
7 |
|
|
8 |
public final String ROLE_ADMIN = "ROLE_ADMIN"; |
|
9 |
public final String ROLE_PROVIDE_ADMIN = "ROLE_PROVIDE_ADMIN"; |
|
10 |
public final String ROLE_USER = "ROLE_USER"; |
|
11 |
|
|
12 |
private String mapType(String type) { |
|
13 |
if(type.equals("datasource")) { |
|
14 |
type = "datasource"; |
|
15 |
} |
|
16 |
return type; |
|
17 |
} |
|
18 |
|
|
19 |
/** |
|
20 |
* Type = DATASOURCE |
|
21 |
* |
|
22 |
* */ |
|
23 |
public String member(String type, String id) { |
|
24 |
return mapType(type).toUpperCase() + "_" + id.toUpperCase(); |
|
25 |
} |
|
26 |
|
|
27 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/utils/DatasourceManagerClient.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.utils; |
|
2 |
|
|
3 |
public class DatasourceManagerClient { |
|
4 |
// |
|
5 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/utils/JsonUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.utils; |
|
2 |
|
|
3 |
import com.google.gson.JsonArray; |
|
4 |
import com.google.gson.JsonElement; |
|
5 |
import com.google.gson.JsonObject; |
|
6 |
import eu.dnetlib.repo.manager.domain.dto.Role; |
|
7 |
import org.springframework.beans.factory.annotation.Value; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
@Component |
|
11 |
public class JsonUtils { |
|
12 |
|
|
13 |
@Value("1.0") |
|
14 |
private String version; |
|
15 |
|
|
16 |
@Value("2") |
|
17 |
private String coid; |
|
18 |
|
|
19 |
public JsonObject coPersonRoles(Integer coPersonId, Integer couId, String status) { |
|
20 |
JsonObject role = new JsonObject(); |
|
21 |
JsonArray coPersonRoles = new JsonArray(); |
|
22 |
JsonObject coPersonRole = new JsonObject(); |
|
23 |
JsonObject person = new JsonObject(); |
|
24 |
person.addProperty("Type", "CO"); |
|
25 |
person.addProperty("Id", coPersonId.toString()); |
|
26 |
coPersonRole.addProperty("Version", version); |
|
27 |
coPersonRole.add("Person", person); |
|
28 |
coPersonRole.addProperty("CouId", couId.toString()); |
|
29 |
coPersonRole.addProperty("Affiliation", "member"); |
|
30 |
coPersonRole.addProperty("Title", ""); |
|
31 |
coPersonRole.addProperty("O", "Openaire"); |
|
32 |
coPersonRole.addProperty("Status", status); |
|
33 |
coPersonRole.addProperty("ValidFrom", ""); |
|
34 |
coPersonRole.addProperty("ValidThrough", ""); |
|
35 |
coPersonRoles.add(coPersonRole); |
|
36 |
role.addProperty("RequestType", "CoPersonRoles"); |
|
37 |
role.addProperty("Version", version); |
|
38 |
role.add("CoPersonRoles", coPersonRoles); |
|
39 |
return role; |
|
40 |
} |
|
41 |
|
|
42 |
public JsonObject createNewCou(Role role) { |
|
43 |
JsonObject cou = new JsonObject(); |
|
44 |
JsonArray cous = new JsonArray(); |
|
45 |
JsonObject newCou = new JsonObject(); |
|
46 |
newCou.addProperty("Version", version); |
|
47 |
newCou.addProperty("CoId", coid); |
|
48 |
newCou.addProperty("Name", role.getName()); |
|
49 |
newCou.addProperty("Description", role.getDescription()); |
|
50 |
cous.add(newCou); |
|
51 |
cou.addProperty("RequestType", "Cous"); |
|
52 |
cou.addProperty("Version", version); |
|
53 |
cou.add("Cous", cous); |
|
54 |
return cou; |
|
55 |
} |
|
56 |
|
|
57 |
public JsonObject createResponse(JsonElement response) { |
|
58 |
JsonObject json = new JsonObject(); |
|
59 |
json.add("response", response); |
|
60 |
return json; |
|
61 |
} |
|
62 |
|
|
63 |
public JsonObject createResponse(String response) { |
|
64 |
JsonObject json = new JsonObject(); |
|
65 |
json.addProperty("response", response); |
|
66 |
return json; |
|
67 |
} |
|
68 |
|
|
69 |
public JsonObject createResponse(Number response) { |
|
70 |
JsonObject json = new JsonObject(); |
|
71 |
json.addProperty("response", response); |
|
72 |
return json; |
|
73 |
} |
|
74 |
|
|
75 |
public JsonObject createResponse(Boolean response) { |
|
76 |
JsonObject json = new JsonObject(); |
|
77 |
json.addProperty("response", response); |
|
78 |
return json; |
|
79 |
} |
|
80 |
|
|
81 |
public JsonObject createResponse(Character response) { |
|
82 |
JsonObject json = new JsonObject(); |
|
83 |
json.addProperty("response", response); |
|
84 |
return json; |
|
85 |
} |
|
86 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/controllers/AaiUserRoleController.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.controllers; |
|
2 |
|
|
3 |
import com.google.gson.JsonArray; |
|
4 |
import eu.dnetlib.repo.manager.domain.dto.Role; |
|
5 |
import eu.dnetlib.repo.manager.service.AaiUserRoleService; |
|
6 |
import eu.dnetlib.repo.manager.utils.AuthorizationService; |
|
7 |
import eu.dnetlib.repo.manager.utils.JsonUtils; |
|
8 |
import org.mitre.openid.connect.model.OIDCAuthenticationToken; |
|
9 |
import org.springframework.beans.factory.annotation.Autowired; |
|
10 |
import org.springframework.http.HttpStatus; |
|
11 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
12 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
13 |
import org.springframework.web.bind.annotation.RequestBody; |
|
14 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
15 |
import org.springframework.web.bind.annotation.RestController; |
|
16 |
|
|
17 |
import javax.ws.rs.*; |
|
18 |
import javax.ws.rs.core.MediaType; |
|
19 |
import javax.ws.rs.core.Response; |
|
20 |
|
|
21 |
@RestController |
|
22 |
@RequestMapping("/aai-user-management") |
|
23 |
public class AaiUserRoleController { |
|
24 |
|
|
25 |
@Autowired |
|
26 |
private AaiUserRoleService calls; |
|
27 |
|
|
28 |
@Autowired |
|
29 |
private JsonUtils jsonUtils; |
|
30 |
|
|
31 |
// TODO: Antonis K. This should be uncommented |
|
32 |
// @Autowired |
|
33 |
// private AuthoritiesUpdater authoritiesUpdater; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private AuthorizationService authorizationService; |
|
37 |
|
|
38 |
private String sendEmail() { |
|
39 |
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
|
40 |
return authenticationToken.getUserInfo().getEmail(); |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* Create a new role with the given name and description. |
|
45 |
**/ |
|
46 |
@Path("/createRole") |
|
47 |
@POST |
|
48 |
@Produces(MediaType.APPLICATION_JSON) |
|
49 |
@Consumes(MediaType.APPLICATION_JSON) |
|
50 |
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here |
|
51 |
public Response createRole(@RequestBody Role role) { |
|
52 |
calls.createRole(role); |
|
53 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.) |
|
58 |
*/ |
|
59 |
@Path("/subscribe/{type}/{id}") |
|
60 |
@POST |
|
61 |
@Produces(MediaType.APPLICATION_JSON) |
|
62 |
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here |
|
63 |
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) { |
|
64 |
Integer coPersonId = calls.getCoPersonIdByIdentifier(); |
|
65 |
Integer couId = calls.getCouId(type, id); |
|
66 |
if (couId != null) { |
|
67 |
Integer role = calls.getRoleId(coPersonId, couId); |
|
68 |
calls.assignMemberRole(coPersonId, couId, role); |
|
69 |
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo |
|
70 |
// authoritiesUpdater.update(sendEmail(), old -> { |
|
71 |
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old); |
|
72 |
// authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id))); |
|
73 |
// return authorities; |
|
74 |
// }); |
|
75 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
76 |
} else { |
|
77 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
/** |
|
83 |
* Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.) |
|
84 |
*/ |
|
85 |
@Path("/{type}/{id}/member/{email}") |
|
86 |
@DELETE |
|
87 |
@Produces(MediaType.APPLICATION_JSON) |
|
88 |
@Consumes(MediaType.APPLICATION_JSON) |
|
89 |
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here |
|
90 |
public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String |
|
91 |
id, @PathParam("email") String email) { |
|
92 |
Integer coPersonId = calls.getCoPersonIdByEmail(email); |
|
93 |
if (coPersonId != null) { |
|
94 |
Integer couId = calls.getCouId(type, id); |
|
95 |
Integer role = null; |
|
96 |
if (couId != null) { |
|
97 |
role = calls.getRoleId(coPersonId, couId); |
|
98 |
} |
|
99 |
if (couId != null && role != null) { |
|
100 |
calls.removeMemberRole(coPersonId, couId, role); |
|
101 |
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo |
|
102 |
// authoritiesUpdater.update(email, old -> { |
|
103 |
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old); |
|
104 |
// authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id))); |
|
105 |
// return authorities; |
|
106 |
// }); |
|
107 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
108 |
} else { |
|
109 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
110 |
} |
|
111 |
} else { |
|
112 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* Get all the users that have the role that is associated with repoId |
|
118 |
*/ |
|
119 |
@Path("/repo/{id}/all-users") |
|
120 |
@GET |
|
121 |
@Produces(MediaType.APPLICATION_JSON) |
|
122 |
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here |
|
123 |
public Response getAllUserOfARepo(@PathParam("id") String id) { |
|
124 |
|
|
125 |
// find roleId by repoId |
|
126 |
String roleId = calls.getRoleIdByRepoId(id, "datasource"); |
|
127 |
|
|
128 |
// find couId by role name |
|
129 |
if (roleId != null) { |
|
130 |
Integer couId = calls.getCouId("datasource", roleId); |
|
131 |
if (couId != null) { |
|
132 |
JsonArray users = calls.getUsersByCouId(couId); |
|
133 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(users).toString()).type(MediaType.APPLICATION_JSON).build(); |
|
134 |
} else { |
|
135 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
140 |
} |
|
141 |
|
|
142 |
|
|
143 |
/** |
|
144 |
* Subscribe to role-repo by his email |
|
145 |
*/ |
|
146 |
@Path("/subscribe/repo-role/{id}/email/{email}") |
|
147 |
@POST |
|
148 |
@Produces(MediaType.APPLICATION_JSON) |
|
149 |
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here |
|
150 |
public Response subscribeRoleByEmail(@PathParam("id") String id, @PathParam("email") String email) { |
|
151 |
Integer coPersonId = calls.getCoPersonIdByEmail(email); |
|
152 |
if (coPersonId != null) { |
|
153 |
String roleId = calls.getRoleIdByRepoId(id, "datasource"); |
|
154 |
if (roleId != null) { |
|
155 |
Integer couId = calls.getCouId("datasource", roleId); |
|
156 |
if (couId != null) { |
|
157 |
Integer role = calls.getRoleId(coPersonId, couId); |
|
158 |
calls.assignMemberRole(coPersonId, couId, role); |
|
159 |
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo |
|
160 |
// authoritiesUpdater.update(sendEmail(), old -> { |
|
161 |
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old); |
|
162 |
// authorities.add(new SimpleGrantedAuthority(authorizationService.member("datasource", id))); |
|
163 |
// return authorities; |
|
164 |
// }); |
|
165 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
166 |
} else { |
|
167 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
168 |
} |
|
169 |
} else { |
|
170 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
171 |
} |
|
172 |
} else { |
|
173 |
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("User with this email has not been found").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
174 |
} |
|
175 |
|
|
176 |
|
|
177 |
} |
|
178 |
|
|
179 |
} |
modules/uoa-repository-manager-service/branches/aai_roles_new/src/main/java/eu/dnetlib/repo/manager/domain/dto/Role.java | ||
---|---|---|
1 |
package eu.dnetlib.repo.manager.domain.dto; |
|
2 |
|
|
3 |
import javax.xml.bind.annotation.XmlRootElement; |
|
4 |
|
|
5 |
@XmlRootElement |
|
6 |
public class Role { |
|
7 |
String name; |
|
8 |
String description; |
|
9 |
|
|
10 |
public Role() {} |
|
11 |
|
|
12 |
public Role(String name, String description) { |
|
13 |
this.name = name; |
|
14 |
this.description = description; |
|
15 |
} |
|
16 |
|
|
17 |
public String getName() { |
|
18 |
return name; |
|
19 |
} |
|
20 |
|
|
21 |
public void setName(String name) { |
|
22 |
this.name = name; |
|
23 |
} |
|
24 |
|
|
25 |
public String getDescription() { |
|
26 |
return description; |
|
27 |
} |
|
28 |
|
|
29 |
public void setDescription(String description) { |
|
30 |
this.description = description; |
|
31 |
} |
|
32 |
} |
Also available in: Unified diff
New Add Repository Functionality