Third Party User Creation and Transaction Permission Management

Wasi-Third_Party_UserAndPermission_Management
Wasiullah Khan Jadoon 1 month ago
parent 7c64d1b5dc
commit 220e0be4eb

@ -0,0 +1,26 @@
package com.mfsys.aconnect.client.controller;
import com.mfsys.aconnect.client.dto.ThirdPartyUserDTO;
import com.mfsys.aconnect.client.service.ThirdPartyUserService;
import com.mfsys.common.configuration.constant.AconnectURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class ThirdPartyUserController {
@Autowired
private ThirdPartyUserService thirdPartyUserService;
@Autowired
public ThirdPartyUserController(ThirdPartyUserService thirdPartyUserService){
this.thirdPartyUserService = thirdPartyUserService;
}
@PostMapping(AconnectURI.CREATE_THIRD_PARTY_USER)
public Object createThirdPartyUser(@RequestBody ThirdPartyUserDTO request,
@RequestHeader("Authorization") String token){
return thirdPartyUserService.createThirdPartyUser(request, token);
}
}

@ -0,0 +1,43 @@
package com.mfsys.aconnect.client.controller;
import com.mfsys.aconnect.client.dto.TransactionPermissionDTO;
import com.mfsys.aconnect.client.service.TransactionPermissionService;
import com.mfsys.aconnect.client.model.TransactionEndpoint;
import com.mfsys.common.configuration.constant.AconnectURI;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class TransactionPermissionController {
private final TransactionPermissionService permissionService;
public TransactionPermissionController(TransactionPermissionService permissionService) {
this.permissionService = permissionService;
}
@PostMapping(AconnectURI.ASSIGN_PERMISSIONS)
public ResponseEntity<List<TransactionPermissionDTO.PermissionResponse>> assignPermissions(
@RequestBody TransactionPermissionDTO.AssignPermissionsRequest request, @RequestHeader("Authorization") String token) {
return new ResponseEntity<>(permissionService.assignPermissions(request, token), HttpStatus.CREATED);
}
@GetMapping(AconnectURI.GET_TRANSACTION_PERMISSIONS)
public ResponseEntity<List<TransactionPermissionDTO.PermissionResponse>> getUserPermissions(
@PathVariable String userId, @RequestHeader("Authorization") String token) {
return ResponseEntity.ok(permissionService.getUserPermissions(userId, token));
}
@GetMapping(AconnectURI.GET_TRANSACTION_ENDPOINTS)
public ResponseEntity<List<String>> getAllEndpoints(@RequestHeader("Authorization") String token) {
List<String> endpoints = Arrays.stream(TransactionEndpoint.values())
.map(TransactionEndpoint::getCode)
.collect(Collectors.toList());
return ResponseEntity.ok(endpoints);
}
}

@ -0,0 +1,20 @@
package com.mfsys.aconnect.client.dto;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class ThirdPartyUserDTO {
private String formId;
private String postProcessFormId;
private String workFlowId;
private String operation;
private String porOrgacode;
private String usercode;
private Map<String, String> filesMap;
private List<Object> autoIncrementFields;
private List<List<String>> uniqueConstraints;
private List<Object> formCounters;
private String payload;
}

@ -0,0 +1,23 @@
package com.mfsys.aconnect.client.dto;
import lombok.Data;
import java.util.List;
public class TransactionPermissionDTO {
@Data
public static class AssignPermissionsRequest {
private String userId;
private String porOrgacode;
private List<String> transactionEndpoints;
}
@Data
public static class PermissionResponse {
private Long id;
private String userId;
private String transactionEndpoint;
private boolean isAllowed;
}
}

@ -0,0 +1,10 @@
package com.mfsys.aconnect.client.exception;
import com.mfsys.common.configuration.constant.ERRCode;
import com.mfsys.common.configuration.exception.ApplicationException;
public class TransactionNotAllowedException extends ApplicationException {
public TransactionNotAllowedException(String porOrgacode){
super(porOrgacode, ERRCode.TRANSACTION_NOT_ALLOWED_EXCEPTION);
}
}

@ -0,0 +1,21 @@
package com.mfsys.aconnect.client.model;
import com.mfsys.common.configuration.constant.AconnectURI;
public enum TransactionEndpoint {
ACCOUNT_TO_GL(AconnectURI.TRANSACTION_ACCOUNT_GL_URI),
GL_TO_GL(AconnectURI.TRANSACTION_GL_GL_URI),
ACCOUNT_TO_ACCOUNT(AconnectURI.ACCOUNT_TO_ACCOUNT_TRANSACTION_URI),
GL_TO_ACCOUNT(AconnectURI.GL_TO_ACCOUNT_TRANSACTION_URI);
private final String code;
TransactionEndpoint(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}

@ -0,0 +1,25 @@
package com.mfsys.aconnect.client.model;
import jakarta.persistence.*;
import lombok.*;
@Entity(name = "transaction_permissions")
@Table(name = "transaction_permissions")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TransactionPermission {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private String userId;
@Column(name = "transaction_endpoint", nullable = false)
private String transactionEndpoint;
@Column(name = "is_allowed", nullable = false, columnDefinition = "TINYINT(1)")
private boolean isAllowed;
}

@ -0,0 +1,17 @@
package com.mfsys.aconnect.client.repository;
import com.mfsys.aconnect.client.model.TransactionPermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface TransactionPermissionRepository extends JpaRepository<TransactionPermission, Long> {
List<TransactionPermission> findByUserId(String userId);
Optional<TransactionPermission> findByUserIdAndTransactionEndpoint(String userId, String transactionEndpoint);
@Modifying
void deleteByUserId(String userId);
}

@ -33,7 +33,7 @@ public class AuthService {
HttpEntity<Map<String, String>> request = new HttpEntity<>(requestPayload, headers); HttpEntity<Map<String, String>> request = new HttpEntity<>(requestPayload, headers);
try { try {
ResponseEntity<String> response = restTemplate.postForEntity(securityURI, request, String.class); ResponseEntity<String> response = restTemplate.postForEntity(securityURI+"/security/auth/user", request, String.class);
JsonNode jsonNode = objectMapper.readTree(response.getBody()); JsonNode jsonNode = objectMapper.readTree(response.getBody());
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();

@ -0,0 +1,71 @@
package com.mfsys.aconnect.client.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mfsys.aconnect.client.dto.ThirdPartyUserDTO;
import com.mfsys.aconnect.configuration.config.WebClientConfig;
import com.mfsys.aconnect.usermanagement.dto.UserDTOs;
import com.mfsys.aconnect.usermanagement.model.Role;
import com.mfsys.aconnect.usermanagement.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.http.*;
import java.util.Map;
@Service
public class ThirdPartyUserService {
@Value("${app.security.uri}")
private String securityURI;
private final WebClientConfig webClientConfig;
private final UserService userService;
private final ObjectMapper objectMapper;
public ThirdPartyUserService(WebClientConfig webClientConfig, UserService userService, ObjectMapper objectMapper){
this.webClientConfig = webClientConfig;
this.userService = userService;
this.objectMapper = objectMapper;
}
public Object createThirdPartyUser(ThirdPartyUserDTO request, String token){
String porOrgacode = request.getPorOrgacode();
String url = securityURI + "/security/user";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", token);
headers.set("POR_ORGACODE", porOrgacode);
headers.set("SUS_USERCODE", request.getUsercode());
headers.setContentType(MediaType.APPLICATION_JSON);
ResponseEntity<Object> response = webClientConfig.post(url, request, headers);
if (response.getStatusCode().is2xxSuccessful()) {
saveToAconnect(request);
}
return response.getBody();
}
private void saveToAconnect(ThirdPartyUserDTO request) {
try {
Map<String, Object> payloadMap = objectMapper.readValue(request.getPayload(), Map.class);
UserDTOs.UserRequest userRequest = new UserDTOs.UserRequest();
userRequest.setUserId(getStr(payloadMap, "SUS_USERCODE"));
userRequest.setPorOrgacode(getStr(payloadMap, "POR_ORGACODE"));
userRequest.setUserFullname(getStr(payloadMap, "SUS_NAME"));
userRequest.setPassword(getStr(payloadMap, "SUS_PASSWORD"));
userRequest.setEmail(getStr(payloadMap, "SUS_EMAIL"));
userRequest.setRole(Role.USER);
userService.createUser(userRequest);
} catch (Exception e) {
System.err.println("Failed to save third party user to Aconnect: " + e.getMessage());
}
}
private String getStr(Map<String, Object> map, String key) {
Object val = map.get(key);
return val != null ? val.toString() : null;
}
}

@ -0,0 +1,59 @@
package com.mfsys.aconnect.client.service;
import com.mfsys.aconnect.client.dto.TransactionPermissionDTO;
import com.mfsys.aconnect.client.model.TransactionPermission;
import com.mfsys.aconnect.client.repository.TransactionPermissionRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class TransactionPermissionService {
private final TransactionPermissionRepository permissionRepository;
public TransactionPermissionService(TransactionPermissionRepository permissionRepository) {
this.permissionRepository = permissionRepository;
}
@Transactional
public List<TransactionPermissionDTO.PermissionResponse> assignPermissions(TransactionPermissionDTO.AssignPermissionsRequest request, String token) {
String porOrgacode = request.getPorOrgacode();
permissionRepository.deleteByUserId(request.getUserId());
List<TransactionPermission> permissions = request.getTransactionEndpoints().stream()
.map(endpoint -> {
TransactionPermission p = new TransactionPermission();
p.setUserId(request.getUserId());
p.setTransactionEndpoint(endpoint);
p.setAllowed(true);
return p;
})
.collect(Collectors.toList());
List<TransactionPermission> saved = permissionRepository.saveAll(permissions);
return saved.stream().map(this::mapToResponse).collect(Collectors.toList());
}
public List<TransactionPermissionDTO.PermissionResponse> getUserPermissions(String userId, String token) {
return permissionRepository.findByUserId(userId)
.stream()
.map(this::mapToResponse)
.collect(Collectors.toList());
}
public boolean isAllowed(String userId, String transactionEndpoint) {
return permissionRepository
.findByUserIdAndTransactionEndpoint(userId, transactionEndpoint)
.map(TransactionPermission::isAllowed)
.orElse(false);
}
private TransactionPermissionDTO.PermissionResponse mapToResponse(TransactionPermission p) {
TransactionPermissionDTO.PermissionResponse response = new TransactionPermissionDTO.PermissionResponse();
response.setId(p.getId());
response.setUserId(p.getUserId());
response.setTransactionEndpoint(p.getTransactionEndpoint());
response.setAllowed(p.isAllowed());
return response;
}
}

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.mfsys.aconnect.client.model.TransactionEndpoint;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -29,9 +30,11 @@ public class TransactionService {
private final WebClientConfig webClientService; private final WebClientConfig webClientService;
private final TransactionLogService transactionLogService; private final TransactionLogService transactionLogService;
public TransactionService(WebClientConfig webClientService, TransactionLogService transactionLogService) { private final TransactionPermissionService permissionService;
public TransactionService(WebClientConfig webClientService, TransactionLogService transactionLogService, TransactionPermissionService permissionService) {
this.webClientService = webClientService; this.webClientService = webClientService;
this.transactionLogService = transactionLogService; this.transactionLogService = transactionLogService;
this.permissionService = permissionService;
} }
public ResponseEntity getActiveAccountDetails(String porOrgacode, String mbmBkmsnumber, LocalDate sgtGntrvaluedate, String tokenHeader, String userCode) { public ResponseEntity getActiveAccountDetails(String porOrgacode, String mbmBkmsnumber, LocalDate sgtGntrvaluedate, String tokenHeader, String userCode) {
@ -54,6 +57,9 @@ public class TransactionService {
public ResponseEntity processAccountTransaction(AccountGLTransactionRequest accountGLTransactionRequest, String tokenHeader) { public ResponseEntity processAccountTransaction(AccountGLTransactionRequest accountGLTransactionRequest, String tokenHeader) {
if (!permissionService.isAllowed(accountGLTransactionRequest.getSgtGntrcreateusr(), TransactionEndpoint.ACCOUNT_TO_GL.getCode())) {
throw new TransactionNotAllowedException(accountGLTransactionRequest.getPorOrgacode());
}
Double creditAmount = accountGLTransactionRequest.getCreditGl().getSgtGntramtfc(); Double creditAmount = accountGLTransactionRequest.getCreditGl().getSgtGntramtfc();
Double debitAmount = accountGLTransactionRequest.getDebitAcc().getSgtGntramtfc(); Double debitAmount = accountGLTransactionRequest.getDebitAcc().getSgtGntramtfc();
@ -93,6 +99,9 @@ public class TransactionService {
} }
public Object processGLTransaction(GLtoGLRequest gLtoGLRequest, String tokenHeader) { public Object processGLTransaction(GLtoGLRequest gLtoGLRequest, String tokenHeader) {
if (!permissionService.isAllowed(gLtoGLRequest.getSgtGntrcreateusr(), TransactionEndpoint.GL_TO_GL.getCode())) {
throw new TransactionNotAllowedException(gLtoGLRequest.getPorOrgacode());
}
Double creditAmount = gLtoGLRequest.getCreditGl().getSgtGntramtfc(); Double creditAmount = gLtoGLRequest.getCreditGl().getSgtGntramtfc();
Double debitAmount = gLtoGLRequest.getDebitGl().getSgtGntramtfc(); Double debitAmount = gLtoGLRequest.getDebitGl().getSgtGntramtfc();
@ -145,6 +154,9 @@ public class TransactionService {
} }
public Object processAccToAccTransaction(AccountToAccountDTO accountToAccountDTO, String tokenHeader) { public Object processAccToAccTransaction(AccountToAccountDTO accountToAccountDTO, String tokenHeader) {
if (!permissionService.isAllowed(accountToAccountDTO.getSgtGntrcreateusr(), TransactionEndpoint.ACCOUNT_TO_ACCOUNT.getCode())) {
throw new TransactionNotAllowedException(accountToAccountDTO.getPorOrgacode());
}
BigDecimal creditAmount = accountToAccountDTO.getCreditAcc().getSgtGntramtfc(); BigDecimal creditAmount = accountToAccountDTO.getCreditAcc().getSgtGntramtfc();
BigDecimal debitAmount = accountToAccountDTO.getDebitAcc().getSgtGntramtfc(); BigDecimal debitAmount = accountToAccountDTO.getDebitAcc().getSgtGntramtfc();
@ -196,6 +208,9 @@ public class TransactionService {
} }
public Object processGLtoAccTransaction(GlToAccountDTO glToAccountDTO, String tokenHeader) { public Object processGLtoAccTransaction(GlToAccountDTO glToAccountDTO, String tokenHeader) {
if (!permissionService.isAllowed(glToAccountDTO.getSgtGntrcreateusr(), TransactionEndpoint.GL_TO_ACCOUNT.getCode())) {
throw new TransactionNotAllowedException(glToAccountDTO.getPorOrgacode());
}
BigDecimal creditAmount = glToAccountDTO.getCreditAcc().getSgtGntramtfc(); BigDecimal creditAmount = glToAccountDTO.getCreditAcc().getSgtGntramtfc();
BigDecimal debitAmount = glToAccountDTO.getDebitGl().getSgtGntramtfc(); BigDecimal debitAmount = glToAccountDTO.getDebitGl().getSgtGntramtfc();

@ -1,4 +1,4 @@
app.security.uri=http://localhost:9090/security/auth/user app.security.uri=http://localhost:9090
app.deposit.uri=http://localhost:9095 app.deposit.uri=http://localhost:9095
app.generalledger.uri=http://localhost:9093 app.generalledger.uri=http://localhost:9093
app.onlinebanking.uri=http://localhost:9099 app.onlinebanking.uri=http://localhost:9099

@ -6,6 +6,7 @@ public interface AconnectURI {
String DEPOSIT = "/deposit"; String DEPOSIT = "/deposit";
String CRM = "/crm"; String CRM = "/crm";
String LOAN = "/loan"; String LOAN = "/loan";
String CREATE_THIRD_PARTY_USER = "/createThirdPartyUser";
String GENERALLEDGER = "/generalledger"; String GENERALLEDGER = "/generalledger";
String SIGNIN = "/signin"; String SIGNIN = "/signin";
String TRANSACTION_URI = "/transactions"; String TRANSACTION_URI = "/transactions";
@ -17,8 +18,12 @@ public interface AconnectURI {
String BUSINESS = "/business"; String BUSINESS = "/business";
String REVERSE_URI = "/reversal"; String REVERSE_URI = "/reversal";
String AUTHORIZATION_URI = "/authorizations"; String AUTHORIZATION_URI = "/authorizations";
String TRANSACTION_PERMISSIONS = "/transaction-permissions";
String DEPOSIT_AUTHORIZATION_URI = DEPOSIT + AUTHORIZATION_URI; String DEPOSIT_AUTHORIZATION_URI = DEPOSIT + AUTHORIZATION_URI;
String GENERALLEDGER_AUTHORIZATION_URI = GENERALLEDGER + AUTHORIZATION_URI; String GENERALLEDGER_AUTHORIZATION_URI = GENERALLEDGER + AUTHORIZATION_URI;
String ASSIGN_PERMISSIONS = TRANSACTION_PERMISSIONS + "/assign";
String GET_TRANSACTION_PERMISSIONS = TRANSACTION_PERMISSIONS + "/{userId}";
String GET_TRANSACTION_ENDPOINTS = TRANSACTION_PERMISSIONS + "/endpoints";
// Cancellation // Cancellation
String DEPOSIT_CANCELLATION_URI = DEPOSIT + TRANSACTION_URI + CANCEL_URI; String DEPOSIT_CANCELLATION_URI = DEPOSIT + TRANSACTION_URI + CANCEL_URI;

@ -17,7 +17,8 @@ public enum ERRCode implements ErrorMessage {
MISSING_ACCOUNT_NUMBER("ERR_ACCT_0001","Account number is required"), MISSING_ACCOUNT_NUMBER("ERR_ACCT_0001","Account number is required"),
SAMEACCOUNTNUMBER("ERR_ACCT_0002","Account number must be different"), SAMEACCOUNTNUMBER("ERR_ACCT_0002","Account number must be different"),
PREVIOUS_DAY_CANCELLATION("ERR_TRX_0004","Previous day transactions cannot be cancelled"), PREVIOUS_DAY_CANCELLATION("ERR_TRX_0004","Previous day transactions cannot be cancelled"),
TRANSACTION_NOT_FOUND_EXCEPTION("ERR_TRX_0005","Previous day transaction not found"); TRANSACTION_NOT_FOUND_EXCEPTION("ERR_TRX_0005","Previous day transaction not found"),
TRANSACTION_NOT_ALLOWED_EXCEPTION("ERR_TRX_0006","User not allowed to perform this transaction");

Loading…
Cancel
Save