dnet-role-management/src/main/java/eu/dnetlib/dnetrolemanagement/controllers/MemberController.java

120 lines
5.6 KiB
Java

package eu.dnetlib.dnetrolemanagement.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import eu.dnetlib.dnetrolemanagement.dto.User;
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 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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collection;
import java.util.HashSet;
@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.)
*/
@PreAuthorize("hasAuthority('REGISTERED_USER')")
@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);
for (int i = 0; i < users.size(); i++) {
users.get(i).getAsJsonObject().addProperty("email", emails.get(i).getAsJsonObject().get("email").getAsString());
users.get(i).getAsJsonObject().addProperty("name", names.get(i).getAsJsonObject().get("name").getAsString());
}
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role 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");
}
/**
* Assign member role to logged in user
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.POST)
@PreAuthorize("hasAuthority('REGISTRED_USER')")
public ResponseEntity<User[]> assignRole(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer coPersonId = registryService.getCoPersonIdByIdentifier();
if (coPersonId != null) {
Integer couId = registryService.getCouId(type, id);
if (couId != null) {
Integer role = registryService.getRoleId(coPersonId, couId);
registryService.assignMemberRole(coPersonId, couId, role);
authoritiesUpdater.update(AuthoritiesUtils.getAaiId(), old -> {
HashSet<SimpleGrantedAuthority> authorities = new HashSet<>((Collection<? extends SimpleGrantedAuthority>) old);
authorities.add(new SimpleGrantedAuthority(AuthoritiesUtils.member(type, id)));
return authorities;
});
}
throw new ResourceNotFoundException("Role has not been found");
}
throw new ResourceNotFoundException("Role has not been found");
}
}