2021-09-09 13:32:59 +02:00
|
|
|
package eu.dnetlib.dnetrolemanagement.controllers;
|
|
|
|
|
|
|
|
import eu.dnetlib.dnetrolemanagement.entities.Response;
|
|
|
|
import eu.dnetlib.dnetrolemanagement.exception.ConflictException;
|
|
|
|
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/super")
|
|
|
|
public class SuperAdminController {
|
|
|
|
|
|
|
|
private final RegistryService registryService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public SuperAdminController(RegistryService registryService) {
|
|
|
|
this.registryService = registryService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new role (only for admins)
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
|
|
|
public ResponseEntity<Response> createRole(@RequestParam("name") String name, @RequestParam(value = "description", required = false) String description) {
|
|
|
|
try {
|
2021-10-04 15:35:27 +02:00
|
|
|
if (registryService.getCouId(name) == null) {
|
|
|
|
registryService.createRole(name, description != null ? description : "");
|
2021-09-09 13:32:59 +02:00
|
|
|
return ResponseEntity.ok(new Response("Role has been created successfully"));
|
|
|
|
} else {
|
|
|
|
throw new ConflictException("This role already exists");
|
|
|
|
}
|
|
|
|
} catch (HttpClientErrorException e) {
|
|
|
|
throw new ConflictException("This role already exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|