Finished account page users tab
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
package com.mattrixwv.raidbuilder.controller;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.mattrixwv.raidbuilder.annotation.AccountAuthorization;
|
||||
import com.mattrixwv.raidbuilder.entity.Account;
|
||||
import com.mattrixwv.raidbuilder.service.AccountService;
|
||||
import com.mattrixwv.raidbuilder.util.DatabaseTypeUtil.AccountPermissionType;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/account")
|
||||
@RequiredArgsConstructor
|
||||
public class AccountController{
|
||||
private final ObjectMapper mapper;
|
||||
private final AccountService accountService;
|
||||
|
||||
|
||||
@GetMapping
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public List<Account> getAccounts(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "searchTerm", required = false) String searchTerm){
|
||||
log.info("Getting accounts page {} of size {} with search term {}", page, pageSize, searchTerm);
|
||||
|
||||
|
||||
List<Account> accounts;
|
||||
if((searchTerm == null) || (searchTerm.isEmpty())){
|
||||
accounts = accountService.getAccounts(page, pageSize);
|
||||
}
|
||||
else{
|
||||
accounts = accountService.getAccounts(page, pageSize, searchTerm);
|
||||
}
|
||||
|
||||
|
||||
return accounts;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode createAccount(@RequestBody Account account){
|
||||
log.info("Creating account {}", account.getUsername());
|
||||
|
||||
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
Account existingAccount = accountService.getByUsername(account.getUsername());
|
||||
if(existingAccount != null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Username already exists");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
account = accountService.createAccount(account);
|
||||
returnNode.put("status", "success");
|
||||
returnNode.put("accountId", account.getAccountId().toString());
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@GetMapping("/count")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode getAccountsCount(@RequestParam(value = "searchTerm", required = false) String searchTerm){
|
||||
log.info("Getting accounts count");
|
||||
|
||||
|
||||
Long accountsCount;
|
||||
if((searchTerm == null) || (searchTerm.isBlank())){
|
||||
accountsCount = accountService.getAccountsCount();
|
||||
}
|
||||
else{
|
||||
accountsCount = accountService.getAccountsCount(searchTerm);
|
||||
}
|
||||
|
||||
ObjectNode countNode = mapper.createObjectNode();
|
||||
countNode.put("count", accountsCount);
|
||||
countNode.put("status", "success");
|
||||
|
||||
|
||||
return countNode;
|
||||
}
|
||||
|
||||
@PutMapping("/{accountId}/forcePasswordReset")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode forcePasswordReset(@PathVariable("accountId") UUID accountId){
|
||||
log.info("Forcing password reset for account {}", accountId);
|
||||
|
||||
|
||||
Account account = accountService.getByAccountId(accountId);
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
if(account == null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Account not found");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
account.setRefreshToken(null);
|
||||
account.setRefreshTokenExpiration(null);
|
||||
account.setForceReset(true);
|
||||
accountService.updateAccount(account);
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@PutMapping("/{accountId}/resetPassword")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode resetPassword(@PathVariable("accountId") UUID accountId, @RequestBody ObjectNode passwordNode){
|
||||
log.info("Resetting password for account {}", accountId);
|
||||
|
||||
|
||||
Account account = accountService.getByAccountId(accountId);
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
if(account == null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Account not found");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
account.setRefreshToken(null);
|
||||
account.setRefreshTokenExpiration(null);
|
||||
account.setForceReset(false);
|
||||
accountService.updateAccount(account);
|
||||
accountService.updatePassword(accountId, passwordNode.get("password").asText());
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@PutMapping("/{accountId}/revokeRefreshToken")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode revokeRefreshToken(@PathVariable("accountId") UUID accountId){
|
||||
log.info("Revoking refresh token for account {}", accountId);
|
||||
|
||||
|
||||
Account account = accountService.getByAccountId(accountId);
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
if(account == null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Account not found");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
account.setRefreshToken(null);
|
||||
account.setRefreshTokenExpiration(null);
|
||||
accountService.updateAccount(account);
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@PutMapping("/{accountId}")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode updateAccount(@PathVariable("accountId") UUID accountId, @RequestBody Account account){
|
||||
log.info("Updating account {}", accountId);
|
||||
|
||||
|
||||
Account oldAccount = accountService.getByAccountId(accountId);
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
if(oldAccount == null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Account not found");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
oldAccount.setUsername(account.getUsername());
|
||||
oldAccount.setEmail(account.getEmail());
|
||||
oldAccount.setAccountStatus(account.getAccountStatus());
|
||||
accountService.updateAccount(oldAccount);
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{accountId}")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN})
|
||||
public ObjectNode deleteAccount(@PathVariable("accountId") UUID accountId){
|
||||
log.info("Deleting account {}", accountId);
|
||||
|
||||
|
||||
Account account = accountService.getByAccountId(accountId);
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
if(account == null){
|
||||
ArrayNode errorsNode = mapper.createArrayNode();
|
||||
errorsNode.add("Account not found");
|
||||
returnNode.set("errors", errorsNode);
|
||||
returnNode.put("status", "error");
|
||||
}
|
||||
else{
|
||||
accountService.deleteAccount(accountId);
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user