package eu.eudat.controllers; import eu.eudat.data.query.items.table.userinfo.UserInfoTableRequestItem; import eu.eudat.exceptions.security.ExpiredTokenException; import eu.eudat.exceptions.security.NonValidTokenException; import eu.eudat.exceptions.security.NullEmailException; import eu.eudat.logic.managers.UserManager; import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.services.ApiContext; import eu.eudat.models.data.doi.DOIRequest; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.helpers.responses.ResponseItem; import eu.eudat.models.data.security.Principal; import eu.eudat.models.data.userinfo.UserListingModel; import eu.eudat.models.data.userinfo.UserProfile; import eu.eudat.types.ApiMessageCode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.io.IOException; import java.util.Map; import java.util.UUID; import static eu.eudat.types.Authorities.ADMIN; @RestController @CrossOrigin @RequestMapping(value = "api/user") public class Users extends BaseController { private UserManager userManager; @Autowired public Users(ApiContext apiContext, UserManager userManager) { super(apiContext); this.userManager = userManager; } @RequestMapping(method = RequestMethod.POST, value = {"/getPaged"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity>> getPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception { DataTableData dataTable = userManager.getPaged(userInfoTableRequestItem); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE)); } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/updateRoles"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> updateRoles(@Valid @RequestBody UserListingModel userListingModel, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) { userManager.editRoles(userListingModel); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } @RequestMapping(method = RequestMethod.GET, value = {"/{id}"}, produces = "application/json") public @ResponseBody ResponseEntity> get(@PathVariable String id, Principal principal) throws Exception { UUID userId = id.equals("me") ? principal.getId() : UUID.fromString(id); UserProfile user = userManager.getSingle(userId); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(user).status(ApiMessageCode.NO_MESSAGE)); } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/settings"}, produces = "application/json") public @ResponseBody ResponseEntity> saveSettings(@RequestBody Map settings, Principal principal) throws IOException { userManager.updateSettings(settings, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } @RequestMapping(method = RequestMethod.POST, value = {"/getCollaboratorsPaged"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity>> getCollaboratorsPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, Principal principal) throws Exception { DataTableData dataTable = userManager.getCollaboratorsPaged(userInfoTableRequestItem, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE)); } @RequestMapping(method = RequestMethod.GET, value = {"/hasDOIToken"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> hasDOIToken(Principal principal) throws NullEmailException { try { return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().payload(this.userManager.isDOITokenValid(principal)).status(ApiMessageCode.NO_MESSAGE)); } catch (NonValidTokenException | ExpiredTokenException | IOException e) { return ResponseEntity.status(460).body(new ResponseItem().payload(false).status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage())); } } @Transactional @RequestMapping(method = RequestMethod.POST, value = {"/registerDOIToken"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> registerDOIToken(@RequestBody DOIRequest doiRequest, Principal principal) throws NullEmailException, IOException { userManager.registerDOIToken(doiRequest, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } @Transactional @RequestMapping(method = RequestMethod.DELETE, value = {"/deleteDOIToken"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> deleteDOIToken(Principal principal) throws NullEmailException, IOException { userManager.deleteDOIToken(principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } }