diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilter.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilter.java index c717fc083..74ff37f40 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilter.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilter.java @@ -40,6 +40,7 @@ import org.apache.fineract.infrastructure.security.service.PlatformSecurityConte import org.apache.fineract.portfolio.loanaccount.domain.GLIMAccountInfoRepository; import org.apache.fineract.portfolio.loanaccount.domain.GroupLoanIndividualMonitoringAccount; import org.apache.fineract.portfolio.loanaccount.domain.Loan; +import org.apache.fineract.useradministration.exception.UnAuthenticatedUserException; import org.apache.http.HttpStatus; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; @@ -64,7 +65,7 @@ public class LoanCOBApiFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { - if (!isOnApiList(request) || isBypassUser()) { + if (!isOnApiList(request) || isBypassUser(response)) { proceed(filterChain, request, response); } else { Iterable split = Splitter.on('/').split(request.getPathInfo()); @@ -73,7 +74,7 @@ public class LoanCOBApiFilter extends OncePerRequestFilter { Long loanIdFromRequest = getLoanId(isGlim, streamSupplier); List loanIds = isGlim ? getGlimChildLoanIds(loanIdFromRequest) : Collections.singletonList(loanIdFromRequest); if (isLoanHardLocked(loanIds)) { - reject(loanIdFromRequest, response); + reject(loanIdFromRequest, response, HttpStatus.SC_CONFLICT); } else if (isLoanSoftLocked(loanIds)) { executeInlineCob(loanIds); proceed(filterChain, request, response); @@ -87,6 +88,15 @@ public class LoanCOBApiFilter extends OncePerRequestFilter { inlineLoanCOBExecutorService.execute(loanIds, JOB_NAME); } + private boolean isBypassUser(HttpServletResponse response) throws IOException { + try { + return context.authenticatedUser().isBypassUser(); + } catch (UnAuthenticatedUserException e) { + reject(null, response, HttpStatus.SC_UNAUTHORIZED); + } + return false; + } + private List getGlimChildLoanIds(Long loanIdFromRequest) { GroupLoanIndividualMonitoringAccount glimAccount = glimAccountInfoRepository.findOneByIsAcceptingChildAndApplicationId(true, BigDecimal.valueOf(loanIdFromRequest)); @@ -105,10 +115,6 @@ public class LoanCOBApiFilter extends OncePerRequestFilter { return isLoanLocked(loanIds, true); } - private boolean isBypassUser() { - return context.getAuthenticatedUserIfPresent().isBypassUser(); - } - private boolean isLoanLocked(List loanIds, boolean isHardLock) { return isHardLock ? loanIds.stream().anyMatch(loanAccountLockService::isLoanHardLocked) : loanIds.stream().anyMatch(loanAccountLockService::isLoanSoftLocked); @@ -119,8 +125,8 @@ public class LoanCOBApiFilter extends OncePerRequestFilter { filterChain.doFilter(request, response); } - private void reject(Long loanId, HttpServletResponse response) throws IOException { - response.setStatus(HttpStatus.SC_CONFLICT); + private void reject(Long loanId, HttpServletResponse response, int status) throws IOException { + response.setStatus(status); ApiGlobalErrorResponse errorResponse = ApiGlobalErrorResponse.loanIsLocked(loanId); response.getWriter().write(errorResponse.toJson()); } diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilterTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilterTest.java index 7176dae4c..7ecb8d128 100644 --- a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilterTest.java +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/jobs/filter/LoanCOBApiFilterTest.java @@ -85,7 +85,7 @@ class LoanCOBApiFilterTest { given(request.getPathInfo()).willReturn("/jobs/2/inline"); given(request.getMethod()).willReturn(HTTPMethods.POST.value()); - given(context.getAuthenticatedUserIfPresent()).willReturn(appUser); + given(context.authenticatedUser()).willReturn(appUser); given(appUser.isBypassUser()).willReturn(true); testObj.doFilterInternal(request, response, filterChain); @@ -103,7 +103,7 @@ class LoanCOBApiFilterTest { given(request.getMethod()).willReturn(HTTPMethods.POST.value()); given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false); given(loanAccountLockService.isLoanSoftLocked(2L)).willReturn(false); - given(context.getAuthenticatedUserIfPresent()).willReturn(appUser); + given(context.authenticatedUser()).willReturn(appUser); testObj.doFilterInternal(request, response, filterChain); verify(filterChain, times(1)).doFilter(request, response); @@ -120,7 +120,7 @@ class LoanCOBApiFilterTest { given(request.getMethod()).willReturn(HTTPMethods.POST.value()); given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false); given(loanAccountLockService.isLoanSoftLocked(2L)).willReturn(true); - given(context.getAuthenticatedUserIfPresent()).willReturn(appUser); + given(context.authenticatedUser()).willReturn(appUser); testObj.doFilterInternal(request, response, filterChain); verify(inlineLoanCOBExecutorService, times(1)).execute(Collections.singletonList(2L), "INLINE_LOAN_COB"); @@ -139,7 +139,7 @@ class LoanCOBApiFilterTest { given(request.getMethod()).willReturn(HTTPMethods.POST.value()); given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(true); given(response.getWriter()).willReturn(writer); - given(context.getAuthenticatedUserIfPresent()).willReturn(appUser); + given(context.authenticatedUser()).willReturn(appUser); testObj.doFilterInternal(request, response, filterChain); verify(response, times(1)).setStatus(HttpStatus.SC_CONFLICT); @@ -163,7 +163,7 @@ class LoanCOBApiFilterTest { given(loan.getId()).willReturn(loanId); given(loanAccountLockService.isLoanHardLocked(loanId)).willReturn(true); given(response.getWriter()).willReturn(writer); - given(context.getAuthenticatedUserIfPresent()).willReturn(appUser); + given(context.authenticatedUser()).willReturn(appUser); testObj.doFilterInternal(request, response, filterChain); verify(response, times(1)).setStatus(HttpStatus.SC_CONFLICT);