152 lines
7.5 KiB
Java
152 lines
7.5 KiB
Java
package eu.dnetlib.dnetrolemanagement.controllers;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonArray;
|
|
import eu.dnetlib.dnetrolemanagement.dto.Response;
|
|
import eu.dnetlib.dnetrolemanagement.dto.User;
|
|
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
|
|
import eu.dnetlib.dnetrolemanagement.exception.UnprocessableException;
|
|
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.access.prepost.PreAuthorize;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@RestController
|
|
@RequestMapping("/member")
|
|
public class MemberController {
|
|
|
|
private final RegistryService registryService;
|
|
private final AuthoritiesUpdater authoritiesUpdater;
|
|
private final Gson gson;
|
|
|
|
@Autowired
|
|
public MemberController(RegistryService registryService, AuthoritiesUpdater authoritiesUpdater) {
|
|
this.registryService = registryService;
|
|
this.authoritiesUpdater = authoritiesUpdater;
|
|
this.gson = new Gson();
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the user info of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET)
|
|
public ResponseEntity<User[]> getInfos(@PathVariable("type") String type, @PathVariable("id") String id) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
JsonArray users = registryService.getUserIdByCouId(couId, false);
|
|
JsonArray emails = registryService.getUserEmailByCouId(couId, false);
|
|
JsonArray names = registryService.getUserNamesByCouId(couId, false);
|
|
return ResponseEntity.ok(JsonUtils.mergeUserInfo(users, emails, names, gson));
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
|
|
/**
|
|
* Assign member role to logged in user or user with @email
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
|
|
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
|
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
|
if (coPersonIds.size() > 0) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
coPersonIds.forEach(coPersonId -> {
|
|
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
|
Integer role = registryService.getRoleId(coPersonId, couId);
|
|
registryService.assignMemberRole(coPersonId, couId, role);
|
|
authoritiesUpdater.update(identifier, old -> {
|
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
|
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
|
return authorities;
|
|
});
|
|
});
|
|
return ResponseEntity.ok(new Response("Role has been assigned successfully"));
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
throw new ResourceNotFoundException("User has not been found");
|
|
}
|
|
|
|
/**
|
|
* Remove member role from logged in user or user with @email
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.DELETE)
|
|
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @PathVariable("id") String id, @RequestParam(required = false) String email) {
|
|
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
|
if (coPersonIds.size() > 0) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
// If none of the accounts has admin role for this group remove member role
|
|
if (coPersonIds.stream().filter(coPersonId -> registryService.getUserAdminGroup(coPersonId, couId) != null).collect(Collectors.toList()).size() == 0) {
|
|
coPersonIds.forEach(coPersonId -> {
|
|
String identifier = registryService.getIdentifierByCoPersonId(coPersonId);
|
|
Integer role = registryService.getRoleId(coPersonId, couId);
|
|
registryService.removeMemberRole(coPersonId, couId, role);
|
|
authoritiesUpdater.update(identifier, old -> {
|
|
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
|
|
authorities.remove(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
|
|
return authorities;
|
|
});
|
|
});
|
|
return ResponseEntity.ok(new Response("Role has been revoked successfully"));
|
|
}
|
|
throw new UnprocessableException("User is admin of this group, so you must remove admin role first instead of member role");
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
throw new ResourceNotFoundException("User has not been found");
|
|
}
|
|
|
|
|
|
/**
|
|
* Get the names of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}/name", method = RequestMethod.GET)
|
|
public ResponseEntity<User[]> getNames(@PathVariable("type") String type, @PathVariable("id") String id) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
JsonArray users = registryService.getUserNamesByCouId(couId, false);
|
|
return ResponseEntity.ok(gson.fromJson(users, User[].class));
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
|
|
/**
|
|
* Get the Identifiers of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}/id", method = RequestMethod.GET)
|
|
public ResponseEntity<User[]> getIds(@PathVariable("type") String type, @PathVariable("id") String id) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
JsonArray users = registryService.getUserIdByCouId(couId, false);
|
|
return ResponseEntity.ok(gson.fromJson(users, User[].class));
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
|
|
/**
|
|
* Get the emails of the members of a type(Community, etc.) with id(ee, egi, etc.)
|
|
*/
|
|
@RequestMapping(value = "/{type}/{id}/email", method = RequestMethod.GET)
|
|
public ResponseEntity<User[]> getEmails(@PathVariable("type") String type, @PathVariable("id") String id) {
|
|
Integer couId = registryService.getCouId(type, id);
|
|
if (couId != null) {
|
|
JsonArray users = registryService.getUserEmailByCouId(couId, false);
|
|
return ResponseEntity.ok(gson.fromJson(users, User[].class));
|
|
}
|
|
throw new ResourceNotFoundException("Role has not been found");
|
|
}
|
|
}
|