package eu.dnetlib.dnetrolemanagement.controllers; import com.google.gson.Gson; import com.google.gson.JsonArray; import eu.dnetlib.dnetrolemanagement.entities.Response; import eu.dnetlib.dnetrolemanagement.entities.User; import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException; import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException; import eu.dnetlib.dnetrolemanagement.services.RegistryService; import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUpdater; import eu.dnetlib.dnetrolemanagement.utils.AuthoritiesUtils; import eu.dnetlib.dnetrolemanagement.utils.JsonUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.web.bind.annotation.*; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @RestController @RequestMapping("/admin") public class AdminController { private final RegistryService registryService; private final AuthoritiesUpdater authoritiesUpdater; private final Gson gson; @Autowired public AdminController(RegistryService registryService, AuthoritiesUpdater authoritiesUpdater) { this.registryService = registryService; this.authoritiesUpdater = authoritiesUpdater; this.gson = new Gson(); } /** * Get the user info of the managers of a type(Community, etc.) with id(ee, egi, etc.) */ @RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET) public ResponseEntity getAll(@PathVariable("type") String type, @PathVariable("id") String id) { Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (couId != null) { JsonArray users = registryService.getUserIdByCouId(couId, true); JsonArray emails = registryService.getUserEmailByCouId(couId, true); JsonArray names = registryService.getUserNamesByCouId(couId, true); return ResponseEntity.ok(JsonUtils.mergeUserInfo(users, emails, names, gson)); } throw new ResourceNotFoundException("Role has not been found"); } /** * Assign admin role to logged in user or user with @email */ @RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST) public ResponseEntity assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email, @RequestParam(value = "force", defaultValue = "false") boolean force) { List coPersonIds = registryService.getCoPersonIdsByEmail(email); if (coPersonIds.size() > 0) { Integer temp = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (temp != null || force) { Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.memberRole(type, id), ""); AtomicBoolean assigned = new AtomicBoolean(false); coPersonIds.forEach(coPersonId -> { if (assignRoleToAccount(coPersonId, couId, type, id, force)) { assigned.set(true); } }); if (assigned.get()) { return ResponseEntity.ok(new Response("Role has been assigned successfully")); } else { throw new UnprocessableException("User must be a member of this group and not already admin"); } } throw new ResourceNotFoundException("Role has not been found"); } throw new ResourceNotFoundException("User has not been found"); } private boolean assignRoleToAccount(Integer coPersonId, Integer couId, String type, String id, boolean force) { String identifier = registryService.getIdentifierByCoPersonId(coPersonId); Integer role = registryService.getRoleId(coPersonId, couId); if (role != null || force) { if(role == null) { registryService.assignMemberRole(coPersonId,couId, role); } if (registryService.getUserAdminGroup(coPersonId, couId) == null) { registryService.assignAdminRole(coPersonId, couId); authoritiesUpdater.update(identifier, old -> { HashSet authorities = new HashSet<>((Collection) old); if(role == null) { authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id))); } authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id))); return authorities; }); return true; } return false; } return false; } /** * Remove admin role from logged in user or user with @email */ @RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE) public ResponseEntity removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) { List coPersonIds = registryService.getCoPersonIdsByEmail(email); if (coPersonIds.size() > 0) { Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (couId != null) { coPersonIds.forEach(coPersonId -> { registryService.removeAdminRole(coPersonId, couId); String identifier = registryService.getIdentifierByCoPersonId(coPersonId); authoritiesUpdater.update(identifier, old -> { HashSet authorities = new HashSet<>((Collection) old); authorities.remove(new SimpleGrantedAuthority(AuthoritiesUtils.manager(type, id))); return authorities; }); }); return ResponseEntity.ok(new Response("Role has been revoked successfully")); } throw new ResourceNotFoundException("Role has not been found"); } throw new ResourceNotFoundException("User has not been found"); } /* *//** * Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.) *//* @RequestMapping(value = "/{type}/{id}/name", method = RequestMethod.GET) public ResponseEntity getNames(@PathVariable("type") String type, @PathVariable("id") String id) { Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (couId != null) { JsonArray users = registryService.getUserNamesByCouId(couId, true); return ResponseEntity.ok(gson.fromJson(users, User[].class)); } throw new ResourceNotFoundException("Role has not been found"); } *//** * Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.) *//* @RequestMapping(value = "/{type}/{id}/id", method = RequestMethod.GET) public ResponseEntity getIds(@PathVariable("type") String type, @PathVariable("id") String id) { Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (couId != null) { JsonArray users = registryService.getUserIdByCouId(couId, true); return ResponseEntity.ok(gson.fromJson(users, User[].class)); } throw new ResourceNotFoundException("Role has not been found"); } *//** * Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.) *//* @RequestMapping(value = "/{type}/{id}/email", method = RequestMethod.GET) public ResponseEntity getEmails(@PathVariable("type") String type, @PathVariable("id") String id) { Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id)); if (couId != null) { JsonArray users = registryService.getUserEmailByCouId(couId, true); return ResponseEntity.ok(gson.fromJson(users, User[].class)); } throw new ResourceNotFoundException("Role has not been found"); }*/ }