uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/controllers/AaiUserRoleController.java

180 lines
9.0 KiB
Java

package eu.dnetlib.repo.manager.controllers;
import com.google.gson.JsonArray;
import eu.dnetlib.repo.manager.domain.dto.Role;
import eu.dnetlib.repo.manager.service.AaiUserRoleService;
import eu.dnetlib.repo.manager.utils.AuthorizationService;
import eu.dnetlib.repo.manager.utils.JsonUtils;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@RestController
@RequestMapping("/aai-user-management")
public class AaiUserRoleController {
@Autowired
private AaiUserRoleService calls;
@Autowired
private JsonUtils jsonUtils;
// TODO: Antonis K. This should be uncommented
// @Autowired
// private AuthoritiesUpdater authoritiesUpdater;
@Autowired
private AuthorizationService authorizationService;
private String sendEmail() {
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
return authenticationToken.getUserInfo().getEmail();
}
/**
* Create a new role with the given name and description.
**/
@Path("/createRole")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
public Response createRole(@RequestBody Role role) {
calls.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.)
*/
@Path("/subscribe/{type}/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
public Response subscribe(@PathParam("type") String type, @PathParam("id") String id) {
Integer coPersonId = calls.getCoPersonIdByIdentifier();
Integer couId = calls.getCouId(type, id);
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
calls.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.)
*/
@Path("/{type}/{id}/member/{email}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
public Response removeMemberRole(@PathParam("type") String type, @PathParam("id") String
id, @PathParam("email") String email) {
Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (coPersonId != null) {
Integer couId = calls.getCouId(type, id);
Integer role = null;
if (couId != null) {
role = calls.getRoleId(coPersonId, couId);
}
if (couId != null && role != null) {
calls.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();
}
}
/**
* Get all the users that have the role that is associated with repoId
*/
@Path("/repo/{id}/all-users")
@GET
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
public Response getAllUserOfARepo(@PathParam("id") String id) {
// find roleId by repoId
String roleId = calls.getRoleIdByRepoId(id, "datasource");
// find couId by role name
if (roleId != null) {
Integer couId = calls.getCouId("datasource", roleId);
if (couId != null) {
JsonArray users = calls.getUsersByCouId(couId);
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse(users).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();
}
}
return Response.status(HttpStatus.NOT_FOUND.value()).entity(jsonUtils.createResponse("Role has not been found").toString()).type(MediaType.APPLICATION_JSON).build();
}
/**
* Subscribe to role-repo by his email
*/
@Path("/subscribe/repo-role/{id}/email/{email}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@PreAuthorize("hasAnyAuthority('ROLE_USER', 'ROLE_ADMIN', 'ROLE_PROVIDE_ADMIN')") // TODO: Perhaps less roles here
public Response subscribeRoleByEmail(@PathParam("id") String id, @PathParam("email") String email) {
Integer coPersonId = calls.getCoPersonIdByEmail(email);
if (coPersonId != null) {
String roleId = calls.getRoleIdByRepoId(id, "datasource");
if (roleId != null) {
Integer couId = calls.getCouId("datasource", roleId);
if (couId != null) {
Integer role = calls.getRoleId(coPersonId, couId);
calls.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("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();
}
}
}