1
|
package eu.dnetlib.repo.manager.controllers;
|
2
|
|
3
|
import com.google.gson.JsonArray;
|
4
|
import com.google.gson.JsonElement;
|
5
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
6
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
7
|
import eu.dnetlib.repo.manager.service.security.AaiUserRoleService;
|
8
|
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
|
9
|
import eu.dnetlib.repo.manager.utils.JsonUtils;
|
10
|
import io.swagger.annotations.Api;
|
11
|
import io.swagger.annotations.ApiOperation;
|
12
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.http.HttpStatus;
|
15
|
import org.springframework.http.ResponseEntity;
|
16
|
import org.springframework.security.access.prepost.PreAuthorize;
|
17
|
import org.springframework.security.core.context.SecurityContextHolder;
|
18
|
import org.springframework.web.bind.annotation.*;
|
19
|
|
20
|
import javax.ws.rs.core.MediaType;
|
21
|
import javax.ws.rs.core.Response;
|
22
|
import java.util.ArrayList;
|
23
|
import java.util.List;
|
24
|
|
25
|
@RestController
|
26
|
@RequestMapping(value = "/aai-user-management")
|
27
|
@Api(description = "AAI User Management", value = "aai-user-management")
|
28
|
public class AaiUserRoleController {
|
29
|
|
30
|
private final AaiRegistryService aaiRegistryService;
|
31
|
private final AuthoritiesUpdater authoritiesUpdater;
|
32
|
private final AaiUserRoleService aaiUserRoleService;
|
33
|
|
34
|
@Autowired
|
35
|
AaiUserRoleController(AaiRegistryService aaiRegistryService,
|
36
|
AuthoritiesUpdater authoritiesUpdater,
|
37
|
AaiUserRoleService aaiUserRoleService) {
|
38
|
this.aaiRegistryService = aaiRegistryService;
|
39
|
this.authoritiesUpdater = authoritiesUpdater;
|
40
|
this.aaiUserRoleService = aaiUserRoleService;
|
41
|
}
|
42
|
|
43
|
private String sendEmail() {
|
44
|
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
45
|
return authenticationToken.getUserInfo().getEmail();
|
46
|
}
|
47
|
|
48
|
/**
|
49
|
* Get the role with the given name and description.
|
50
|
**/
|
51
|
@RequestMapping(method = RequestMethod.GET, path = "/role/id/get")
|
52
|
// @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
53
|
public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @RequestParam("id") String id) {
|
54
|
int roleId = aaiRegistryService.getCouId(type, id);
|
55
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build();
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Create a new role with the given name and description.
|
60
|
**/
|
61
|
@RequestMapping(method = RequestMethod.POST, path = "/createRole")
|
62
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
|
63
|
public Response createRole(@RequestBody Role role) {
|
64
|
aaiRegistryService.createRole(role);
|
65
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
70
|
*/
|
71
|
@ApiOperation(value = "subscribe")
|
72
|
@RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}")
|
73
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
74
|
public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) {
|
75
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
|
76
|
if (coPersonId == null) {
|
77
|
coPersonId = aaiRegistryService.getCoPersonIdByEmail();
|
78
|
}
|
79
|
Integer couId = aaiRegistryService.getCouId(type, id);
|
80
|
if (couId != null) {
|
81
|
Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
|
82
|
aaiRegistryService.assignMemberRole(coPersonId, couId, role);
|
83
|
|
84
|
// Add role to current user authorities
|
85
|
authoritiesUpdater.addRole(aaiUserRoleService.convertRepoIdToAuthority(id));
|
86
|
|
87
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
88
|
} else {
|
89
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
90
|
}
|
91
|
}
|
92
|
|
93
|
|
94
|
/**
|
95
|
* Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
96
|
*/
|
97
|
@ApiOperation(value = "Remove role from member")
|
98
|
@RequestMapping(method = RequestMethod.DELETE, path = "/{type}/{id}/member/{email}")
|
99
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // FIXME: ??
|
100
|
public Response removeMemberRole(@PathVariable("type") String type, @PathVariable("id") String
|
101
|
id, @PathVariable("email") String email) {
|
102
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
103
|
if (coPersonId != null) {
|
104
|
Integer couId = aaiRegistryService.getCouId(type, id);
|
105
|
Integer role = null;
|
106
|
if (couId != null) {
|
107
|
role = aaiRegistryService.getRoleId(coPersonId, couId);
|
108
|
}
|
109
|
if (couId != null && role != null) {
|
110
|
aaiRegistryService.removeMemberRole(coPersonId, couId, role);
|
111
|
|
112
|
// Remove role from current user authorities
|
113
|
authoritiesUpdater.removeRole(aaiUserRoleService.convertRepoIdToAuthority(id));
|
114
|
|
115
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
116
|
} else {
|
117
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
118
|
}
|
119
|
} else {
|
120
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
121
|
}
|
122
|
}
|
123
|
|
124
|
|
125
|
/**
|
126
|
* Subscribe to role-repo by his email
|
127
|
*/
|
128
|
@RequestMapping(method = RequestMethod.POST, path = "/subscribe/repo-role/{id}")
|
129
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or @aaiUserRoleService.isMemberOf(#id)")
|
130
|
public Response subscribeRoleByEmail(@PathVariable("id") String id, @RequestParam("email") String email) {
|
131
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
132
|
if (coPersonId != null) {
|
133
|
Integer couId = aaiRegistryService.getCouId("datasource", id);
|
134
|
if (couId != null) {
|
135
|
Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
|
136
|
aaiRegistryService.assignMemberRole(coPersonId, couId, role);
|
137
|
|
138
|
// Add role to current user authorities
|
139
|
authoritiesUpdater.addRole(aaiUserRoleService.convertRepoIdToAuthority(id));
|
140
|
|
141
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
142
|
} else {
|
143
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
144
|
}
|
145
|
} else {
|
146
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User with this email has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
147
|
}
|
148
|
|
149
|
|
150
|
}
|
151
|
|
152
|
|
153
|
/**
|
154
|
* Get all the users that have the role that is associated with repoId
|
155
|
*/
|
156
|
@RequestMapping(method = RequestMethod.GET, path = "/repo/{id}/all-users")
|
157
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // FIXME: ??
|
158
|
public ResponseEntity<List<String>> getAllUsersOfARepo(@PathVariable("id") String id) {
|
159
|
|
160
|
List<String> userList = new ArrayList<>();
|
161
|
|
162
|
// find couId by role name
|
163
|
Integer couId = aaiRegistryService.getCouId("datasource", id);
|
164
|
if (couId != null) {
|
165
|
JsonArray users = aaiRegistryService.getUsersByCouId(couId);
|
166
|
for (JsonElement jsonElement : users) {
|
167
|
userList.add(jsonElement.toString());
|
168
|
}
|
169
|
return ResponseEntity.ok(userList);
|
170
|
}
|
171
|
|
172
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
173
|
}
|
174
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
|
177
|
@RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}")
|
178
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
179
|
public ResponseEntity<String> getUsersByCouId(@PathVariable("id") Integer id) {
|
180
|
// calls.getUserByCoId()
|
181
|
return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString());
|
182
|
}
|
183
|
|
184
|
|
185
|
@RequestMapping(method = RequestMethod.GET, path = "/user/roles")
|
186
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or hasRole('ROLE_USER') and authentication.userInfo.email==#email")
|
187
|
public ResponseEntity<List<String>> getRolesByEmail(@RequestParam("email") String email) {
|
188
|
int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
189
|
List<String> list = new ArrayList<>();
|
190
|
for (JsonElement element : aaiRegistryService.getRoles(coPersonId)) {
|
191
|
list.add(element.toString());
|
192
|
}
|
193
|
return ResponseEntity.ok(list);
|
194
|
}
|
195
|
|
196
|
}
|