202 lines
11 KiB
Java
202 lines
11 KiB
Java
package eu.dnetlib.repo.manager.controllers;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
|
import eu.dnetlib.repo.manager.utils.JsonUtils;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping(value = "/aai-user-management")
|
|
@Api(description = "AAI User Management", value = "aai-user-management")
|
|
public class AaiUserRoleController {
|
|
|
|
private final AaiRegistryService aaiRegistryService;
|
|
|
|
// TODO: Antonis K. This should be uncommented
|
|
// @Autowired
|
|
// private AuthoritiesUpdater authoritiesUpdater;
|
|
//
|
|
// @Autowired
|
|
// private AuthorizationService authorizationService;
|
|
|
|
@Autowired
|
|
AaiUserRoleController(AaiRegistryService aaiRegistryService) {
|
|
this.aaiRegistryService = aaiRegistryService;
|
|
}
|
|
|
|
private String sendEmail() {
|
|
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
|
return authenticationToken.getUserInfo().getEmail();
|
|
}
|
|
|
|
/**
|
|
* Create a new role with the given name and description.
|
|
**/
|
|
@RequestMapping(method = RequestMethod.GET, path = "/role/id/get")
|
|
// @PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
|
public Response getRole(@RequestParam(value = "type", defaultValue = "datasource") String type, @RequestParam("id") String id) {
|
|
int roleId = aaiRegistryService.getCouId(type, id);
|
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role id is: " + roleId).toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
|
|
/**
|
|
* Create a new role with the given name and description.
|
|
**/
|
|
@RequestMapping(method = RequestMethod.POST, path = "/createRole")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
|
public Response createRole(@RequestBody Role role) {
|
|
aaiRegistryService.createRole(role);
|
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
|
|
/**
|
|
* Subscribe to a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@ApiOperation(value = "subscribe")
|
|
@RequestMapping(method = RequestMethod.POST, path = "/subscribe/{type}/{id}")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN') or (hasRole(@Converter.convertRepoIdToRoleId(#repository.id)) or hasRole(@Converter.convertRepoIdToRoleId(returnObject.id)))")
|
|
// TODO: Perhaps less roles here
|
|
public Response subscribe(@PathVariable("type") String type, @PathVariable("id") String id) {
|
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
|
|
if (coPersonId == null) {
|
|
coPersonId = aaiRegistryService.getCoPersonIdByEmail();
|
|
}
|
|
Integer couId = aaiRegistryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
|
|
aaiRegistryService.assignMemberRole(coPersonId, couId, role);
|
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
|
// authoritiesUpdater.update(sendEmail(), old -> {
|
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
|
// authorities.add(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
|
// return authorities;
|
|
// });
|
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
} else {
|
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Remove the member role from user with email for a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@ApiOperation(value = "Remove role from member")
|
|
@RequestMapping(method = RequestMethod.DELETE, path = "/{type}/{id}/member/{email}")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
|
public Response removeMemberRole(@PathVariable("type") String type, @PathVariable("id") String
|
|
id, @PathVariable("email") String email) {
|
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
|
if (coPersonId != null) {
|
|
Integer couId = aaiRegistryService.getCouId(type, id);
|
|
Integer role = null;
|
|
if (couId != null) {
|
|
role = aaiRegistryService.getRoleId(coPersonId, couId);
|
|
}
|
|
if (couId != null && role != null) {
|
|
aaiRegistryService.removeMemberRole(coPersonId, couId, role);
|
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
|
// authoritiesUpdater.update(email, old -> {
|
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
|
// authorities.remove(new SimpleGrantedAuthority(authorizationService.member(type, id)));
|
|
// return authorities;
|
|
// });
|
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been removed").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
} else {
|
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
} else {
|
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Subscribe to role-repo by his email
|
|
*/
|
|
@RequestMapping(method = RequestMethod.POST, path = "/subscribe/repo-role/{id}")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
|
public Response subscribeRoleByEmail(@PathVariable("id") String id, @RequestParam("email") String email) {
|
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
|
if (coPersonId != null) {
|
|
Integer couId = aaiRegistryService.getCouId("datasource", id);
|
|
if (couId != null) {
|
|
Integer role = aaiRegistryService.getRoleId(coPersonId, couId);
|
|
aaiRegistryService.assignMemberRole(coPersonId, couId, role);
|
|
// TODO: Antonis K. This should be uncommented to make a role DATASOURCE.OP... for every new repo
|
|
// authoritiesUpdater.update(sendEmail(), old -> {
|
|
// HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
|
// authorities.add(new SimpleGrantedAuthority(authorizationService.member("datasource", id)));
|
|
// return authorities;
|
|
// });
|
|
return Response.status(HttpStatus.OK.value()).entity(JsonUtils.createResponse("Role has been assigned").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
} else {
|
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
} else {
|
|
return Response.status(HttpStatus.NOT_FOUND.value()).entity(JsonUtils.createResponse("User with this email has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Get all the users that have the role that is associated with repoId
|
|
*/
|
|
@RequestMapping(method = RequestMethod.GET, path = "/repo/{id}/all-users")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
|
|
public ResponseEntity<List<String>> getAllUsersOfARepo(@PathVariable("id") String id) {
|
|
|
|
List<String> userList = new ArrayList<>();
|
|
|
|
// find couId by role name
|
|
Integer couId = aaiRegistryService.getCouId("datasource", id);
|
|
if (couId != null) {
|
|
JsonArray users = aaiRegistryService.getUsersByCouId(couId);
|
|
for (JsonElement jsonElement : users) {
|
|
userList.add(jsonElement.toString());
|
|
}
|
|
return ResponseEntity.ok(userList);
|
|
}
|
|
|
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
}
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
@RequestMapping(method = RequestMethod.GET, path = "/users/couid/{id}")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
|
public ResponseEntity<String> getUsersByCouId(@PathVariable("id") Integer id) {
|
|
// calls.getUserByCoId()
|
|
return ResponseEntity.ok(aaiRegistryService.getUsersByCouId(id).toString());
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, path = "/user/roles")
|
|
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')")
|
|
public ResponseEntity<List<String>> getRolesByEmail(@RequestParam("email") String email) {
|
|
int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email);
|
|
List<String> list = new ArrayList<>();
|
|
for (JsonElement element : aaiRegistryService.getRoles(coPersonId)) {
|
|
list.add(element.toString());
|
|
}
|
|
return ResponseEntity.ok(list);
|
|
}
|
|
|
|
} |