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.RoleMappingService;
|
8
|
import eu.dnetlib.repo.manager.service.security.AuthoritiesMapper;
|
9
|
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater;
|
10
|
import eu.dnetlib.repo.manager.utils.JsonUtils;
|
11
|
import io.swagger.annotations.Api;
|
12
|
import io.swagger.annotations.ApiOperation;
|
13
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
14
|
import org.mitre.openid.connect.model.UserInfo;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.http.HttpStatus;
|
17
|
import org.springframework.http.ResponseEntity;
|
18
|
import org.springframework.security.access.prepost.PreAuthorize;
|
19
|
import org.springframework.security.core.context.SecurityContextHolder;
|
20
|
import org.springframework.web.bind.annotation.*;
|
21
|
|
22
|
import javax.ws.rs.core.MediaType;
|
23
|
import javax.ws.rs.core.Response;
|
24
|
import java.util.ArrayList;
|
25
|
import java.util.Collection;
|
26
|
import java.util.List;
|
27
|
|
28
|
@RestController
|
29
|
@RequestMapping(value = "/role-management")
|
30
|
@Api(description = "Role Management", value = "role-management")
|
31
|
public class UserRoleController {
|
32
|
|
33
|
private final AaiRegistryService aaiRegistryService;
|
34
|
private final AuthoritiesUpdater authoritiesUpdater;
|
35
|
private final RoleMappingService roleMappingService;
|
36
|
|
37
|
@Autowired
|
38
|
UserRoleController(AaiRegistryService aaiRegistryService,
|
39
|
AuthoritiesUpdater authoritiesUpdater,
|
40
|
RoleMappingService roleMappingService) {
|
41
|
this.aaiRegistryService = aaiRegistryService;
|
42
|
this.authoritiesUpdater = authoritiesUpdater;
|
43
|
this.roleMappingService = roleMappingService;
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Get the role with the given id.
|
48
|
**/
|
49
|
@RequestMapping(method = RequestMethod.GET, path = "/role/{id}")
|
50
|
// @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
51
|
public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @PathVariable("id") String id) {
|
52
|
int roleId = aaiRegistryService.getCouId(type, id);
|
53
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build();
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Create a new role with the given name and description.
|
58
|
**/
|
59
|
@RequestMapping(method = RequestMethod.POST, path = "/role")
|
60
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
|
61
|
public Response createRole(@RequestBody Role role) {
|
62
|
aaiRegistryService.createRole(role);
|
63
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
68
|
*/
|
69
|
@ApiOperation(value = "subscribe")
|
70
|
@RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}")
|
71
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
72
|
public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) {
|
73
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
|
74
|
if (coPersonId == null) {
|
75
|
coPersonId = aaiRegistryService.getCoPersonIdByEmail();
|
76
|
}
|
77
|
Integer couId = aaiRegistryService.getCouId(type, id);
|
78
|
if (couId != null) {
|
79
|
Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
|
80
|
aaiRegistryService.assignMemberRole(coPersonId, couId, role);
|
81
|
|
82
|
// Add role to current authorities
|
83
|
authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(id));
|
84
|
|
85
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
86
|
} else {
|
87
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
88
|
}
|
89
|
}
|
90
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
@RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}")
|
94
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
95
|
public ResponseEntity<String> getUsersByCouId(@PathVariable("id") Integer id) {
|
96
|
// calls.getUserByCoId()
|
97
|
return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString());
|
98
|
}
|
99
|
|
100
|
|
101
|
@RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles")
|
102
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or hasRole('ROLE_USER') and authentication.userInfo.email==#email")
|
103
|
public ResponseEntity<Collection<String>> getRolesByEmail(@PathVariable("email") String email) {
|
104
|
int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
105
|
List<Integer> list = new ArrayList<>();
|
106
|
// FIXME: getRoles returns all roles of user, requested and active
|
107
|
for (JsonElement element : aaiRegistryService.getRoles(coPersonId)) {
|
108
|
list.add(element.getAsJsonObject().get("CouId").getAsInt());
|
109
|
}
|
110
|
return ResponseEntity.ok(aaiRegistryService.getCouNames(list).values());
|
111
|
}
|
112
|
|
113
|
|
114
|
@RequestMapping(method = RequestMethod.GET, path = "/user/roles/my")
|
115
|
@PreAuthorize("hasRole('ROLE_USER')")
|
116
|
public ResponseEntity<Collection<String>> getRoleNames() {
|
117
|
List<String> roles;
|
118
|
JsonArray entitlements = null;
|
119
|
UserInfo userInfo = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo();
|
120
|
if (userInfo.getSource().getAsJsonArray("edu_person_entitlements") != null) {
|
121
|
entitlements = userInfo.getSource().getAsJsonArray("edu_person_entitlements");
|
122
|
} else if (userInfo.getSource().getAsJsonArray("eduperson_entitlement") != null) {
|
123
|
entitlements = userInfo.getSource().getAsJsonArray("eduperson_entitlement");
|
124
|
} else {
|
125
|
return ResponseEntity.ok(null);
|
126
|
}
|
127
|
roles = AuthoritiesMapper.entitlementRoles(entitlements);
|
128
|
return ResponseEntity.ok(roles);
|
129
|
}
|
130
|
|
131
|
}
|