argos/dmp-backend/web/src/main/java/eu/eudat/controllers/Users.java

118 lines
6.0 KiB
Java
Raw Normal View History

2017-12-15 11:26:25 +01:00
package eu.eudat.controllers;
2018-08-30 13:09:36 +02:00
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;
2018-06-27 12:29:21 +02:00
import eu.eudat.logic.managers.UserManager;
2018-08-30 13:09:36 +02:00
import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.models.data.doi.DOIRequest;
2018-06-27 12:29:21 +02:00
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;
2018-08-24 17:21:02 +02:00
import eu.eudat.models.data.userinfo.UserProfile;
2018-01-31 16:39:16 +01:00
import eu.eudat.types.ApiMessageCode;
2017-12-15 11:26:25 +01:00
import org.springframework.beans.factory.annotation.Autowired;
2018-01-31 16:39:16 +01:00
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;
2018-08-31 16:12:31 +02:00
import java.io.IOException;
2018-08-30 13:09:36 +02:00
import java.util.Map;
2018-08-24 17:21:02 +02:00
import java.util.UUID;
2018-01-31 16:39:16 +01:00
import static eu.eudat.types.Authorities.ADMIN;
2017-12-15 11:26:25 +01:00
@RestController
@CrossOrigin
2018-02-09 16:54:41 +01:00
@RequestMapping(value = "api/user")
2018-01-31 16:39:16 +01:00
public class Users extends BaseController {
2018-01-04 10:32:39 +01:00
private UserManager userManager;
2018-01-31 16:39:16 +01:00
@Autowired
public Users(ApiContext apiContext, UserManager userManager) {
2018-01-31 16:39:16 +01:00
super(apiContext);
this.userManager = userManager;
2018-01-31 16:39:16 +01:00
}
2018-02-01 15:04:36 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/getPaged"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
2018-08-31 16:12:31 +02:00
ResponseEntity<ResponseItem<DataTableData<UserListingModel>>> getPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
DataTableData<UserListingModel> dataTable = userManager.getPaged(userInfoTableRequestItem);
2018-08-31 16:12:31 +02:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<UserListingModel>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
2018-01-31 16:39:16 +01:00
}
@Transactional
2018-02-01 15:04:36 +01:00
@RequestMapping(method = RequestMethod.POST, value = {"/updateRoles"}, consumes = "application/json", produces = "application/json")
2018-01-31 16:39:16 +01:00
public @ResponseBody
2018-02-01 15:04:36 +01:00
ResponseEntity<ResponseItem<UserListingModel>> updateRoles(@Valid @RequestBody UserListingModel userListingModel, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) {
userManager.editRoles(userListingModel);
2018-08-31 16:12:31 +02:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserListingModel>().status(ApiMessageCode.NO_MESSAGE));
2018-01-31 16:39:16 +01:00
}
2018-08-24 17:21:02 +02:00
@RequestMapping(method = RequestMethod.GET, value = {"/{id}"}, produces = "application/json")
public @ResponseBody
2018-08-31 16:12:31 +02:00
ResponseEntity<ResponseItem<UserProfile>> get(@PathVariable String id, Principal principal) throws Exception {
UUID userId = id.equals("me") ? principal.getId() : UUID.fromString(id);
UserProfile user = userManager.getSingle(userId);
2018-08-31 16:12:31 +02:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserProfile>().payload(user).status(ApiMessageCode.NO_MESSAGE));
2018-08-24 17:21:02 +02:00
}
2018-08-30 13:09:36 +02:00
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"/settings"}, produces = "application/json")
public @ResponseBody
2018-08-31 16:12:31 +02:00
ResponseEntity<ResponseItem<UserProfile>> saveSettings(@RequestBody Map<String, Object> settings, Principal principal) throws IOException {
userManager.updateSettings(settings, principal);
2018-08-31 16:12:31 +02:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserProfile>().status(ApiMessageCode.NO_MESSAGE));
2018-08-30 13:09:36 +02:00
}
@RequestMapping(method = RequestMethod.POST, value = {"/getCollaboratorsPaged"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
2019-05-20 12:15:28 +02:00
ResponseEntity<ResponseItem<DataTableData<UserListingModel>>> getCollaboratorsPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, Principal principal) throws Exception {
DataTableData<UserListingModel> dataTable = userManager.getCollaboratorsPaged(userInfoTableRequestItem, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<UserListingModel>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
}
@RequestMapping(method = RequestMethod.GET, value = {"/hasDOIToken"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Boolean>> hasDOIToken(Principal principal) throws NullEmailException {
try {
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Boolean>().payload(this.userManager.isDOITokenValid(principal)).status(ApiMessageCode.NO_MESSAGE));
} catch (NonValidTokenException | ExpiredTokenException | IOException e) {
return ResponseEntity.status(460).body(new ResponseItem<Boolean>().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<ResponseItem<UserProfile>> registerDOIToken(@RequestBody DOIRequest doiRequest, Principal principal) throws NullEmailException, IOException {
userManager.registerDOIToken(doiRequest, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserProfile>().status(ApiMessageCode.NO_MESSAGE));
}
2020-04-10 16:16:37 +02:00
@Transactional
@RequestMapping(method = RequestMethod.DELETE, value = {"/deleteDOIToken"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<UserProfile>> deleteDOIToken(Principal principal) throws NullEmailException, IOException {
userManager.deleteDOIToken(principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserProfile>().status(ApiMessageCode.NO_MESSAGE));
}
2018-01-31 16:39:16 +01:00
}
2017-12-15 11:26:25 +01:00
2018-01-17 13:03:51 +01:00
2017-12-15 11:26:25 +01:00