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
|
}
|