package eu.eudat.controllers; import eu.eudat.data.query.items.table.userinfo.UserInfoTableRequestItem; import eu.eudat.logic.managers.UserManager; import eu.eudat.logic.security.claims.ClaimedAuthorities; import eu.eudat.logic.services.ApiContext; 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 { @Autowired public Users(ApiContext apiContext) { super(apiContext); } @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(this.getApiContext(), 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(this.getApiContext(), 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(this.getApiContext(), 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(this.getApiContext(), settings, principal); return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); } }