Remove all field gets. Add force assign and remove role from users.
parent
de9fa97e72
commit
9a7d55694c
@ -0,0 +1,123 @@
|
||||
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.Role;
|
||||
import eu.dnetlib.dnetrolemanagement.entities.User;
|
||||
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
|
||||
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.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/curator")
|
||||
public class CuratorController {
|
||||
|
||||
private final RegistryService registryService;
|
||||
private final AuthoritiesUpdater authoritiesUpdater;
|
||||
private final Gson gson;
|
||||
|
||||
@Autowired
|
||||
public CuratorController(RegistryService registryService, AuthoritiesUpdater authoritiesUpdater) {
|
||||
this.registryService = registryService;
|
||||
this.authoritiesUpdater = authoritiesUpdater;
|
||||
this.gson = new Gson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new role (only for admins)
|
||||
*/
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public ResponseEntity<Response> createRole(@RequestBody Role role) {
|
||||
if (registryService.getCouId(role.getName()) == null) {
|
||||
if (registryService.createRole(role.getName(), role.getDescription()) != null) {
|
||||
return ResponseEntity.ok(new Response("Role has been created successfully"));
|
||||
} else {
|
||||
throw new UnprocessableException("Role creation failed. Try again later");
|
||||
}
|
||||
} else {
|
||||
throw new ConflictException("This role already exists");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user info of the curators of a type(Community, etc.)
|
||||
*/
|
||||
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
|
||||
public ResponseEntity<User[]> getAll(@PathVariable("type") String type) {
|
||||
Integer couId = registryService.getCouId(AuthoritiesUtils.curatorRole(type));
|
||||
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 curator role to logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}", method = RequestMethod.POST)
|
||||
public ResponseEntity<Response> assignRole(@PathVariable("type") String type, @RequestParam(required = false) String email,
|
||||
@RequestParam(value = "force", defaultValue = "false") boolean force) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer temp = registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase());
|
||||
if (temp != null || force) {
|
||||
Integer couId = (temp != null)?temp:registryService.createRole(AuthoritiesUtils.curatorRole(type), "");
|
||||
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.curator(type)));
|
||||
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 curator role from logged in user or user with @email
|
||||
*/
|
||||
@RequestMapping(value = "/{type}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity<Response> removeRole(@PathVariable("type") String type, @RequestParam(required = false) String email) {
|
||||
List<Integer> coPersonIds = registryService.getCoPersonIdsByEmail(email);
|
||||
if (coPersonIds.size() > 0) {
|
||||
Integer couId = registryService.getCouId(AuthoritiesUtils.curatorRole(type).toLowerCase());
|
||||
if (couId != null) {
|
||||
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.curator(type)));
|
||||
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");
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package eu.dnetlib.dnetrolemanagement.dto;
|
||||
package eu.dnetlib.dnetrolemanagement.entities;
|
||||
|
||||
public class Response {
|
||||
|
@ -0,0 +1,29 @@
|
||||
package eu.dnetlib.dnetrolemanagement.entities;
|
||||
|
||||
public class Role {
|
||||
String name;
|
||||
String description;
|
||||
|
||||
public Role() {}
|
||||
|
||||
public Role(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package eu.dnetlib.dnetrolemanagement.dto;
|
||||
package eu.dnetlib.dnetrolemanagement.entities;
|
||||
|
||||
public class User {
|
||||
|
Loading…
Reference in New Issue