|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.limitedaccess.redactor |
| 2 | + |
| 3 | +import org.aspectj.lang.ProceedingJoinPoint |
| 4 | +import org.aspectj.lang.annotation.Around |
| 5 | +import org.aspectj.lang.annotation.Aspect |
| 6 | +import org.springframework.context.annotation.Configuration |
| 7 | +import org.springframework.context.annotation.EnableAspectJAutoProxy |
| 8 | +import org.springframework.stereotype.Component |
| 9 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.LimitedAccessException |
| 10 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters |
| 11 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.limitedaccess.AccessFor |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.limitedaccess.LaoContext |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.limitedaccess.redactor.LaoRedaction.Mode |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.DataResponse |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius.CaseAccess |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.util.PaginatedResponse |
| 17 | + |
| 18 | +@Configuration |
| 19 | +@EnableAspectJAutoProxy |
| 20 | +class AopConfig |
| 21 | + |
| 22 | +@Aspect |
| 23 | +@Component |
| 24 | +class LaoRedactorAspect( |
| 25 | + private val loaChecker: AccessFor, |
| 26 | +) { |
| 27 | + @Around("@annotation(redaction)") |
| 28 | + fun redact( |
| 29 | + joinPoint: ProceedingJoinPoint, |
| 30 | + redaction: LaoRedaction, |
| 31 | + ): Any { |
| 32 | + val hmppsId = (joinPoint.args.first() as String).decodeUrlCharacters() |
| 33 | + val laoContext = loaChecker.getAccessFor(hmppsId)?.asLaoContext() |
| 34 | + if (laoContext?.isLimitedAccess() == true && redaction.mode == Mode.REJECT) { |
| 35 | + throw LimitedAccessException() |
| 36 | + } |
| 37 | + val result = joinPoint.proceed() |
| 38 | + return if (laoContext?.isLimitedAccess() == true) { |
| 39 | + when (result) { |
| 40 | + is DataResponse<*> -> redactDataResponse(result) |
| 41 | + is PaginatedResponse<*> -> redactPaginatedResponse(result) |
| 42 | + else -> LaoRedactor.of(result)?.redact(result) ?: result |
| 43 | + } |
| 44 | + } else { |
| 45 | + result |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private fun redactDataResponse(dataResponse: DataResponse<*>): DataResponse<*> = LaoRedactor.of(dataResponse.data as Any)?.redact(dataResponse.data)?.let { DataResponse(it) } ?: dataResponse |
| 50 | + |
| 51 | + private fun redactPaginatedResponse(paginatedResponse: PaginatedResponse<*>): PaginatedResponse<*> { |
| 52 | + val redactor = paginatedResponse.data.firstOrNull()?.let { LaoRedactor.of(it) } |
| 53 | + return PaginatedResponse( |
| 54 | + paginatedResponse.data.map { redactor?.redact(it as Any) ?: it }, |
| 55 | + paginatedResponse.pagination, |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + private fun CaseAccess.asLaoContext() = LaoContext(crn, userExcluded, userRestricted, exclusionMessage, restrictionMessage) |
| 60 | +} |
| 61 | + |
| 62 | +@Target(AnnotationTarget.FUNCTION) |
| 63 | +@Retention(AnnotationRetention.RUNTIME) |
| 64 | +annotation class LaoRedaction( |
| 65 | + val mode: Mode = Mode.REDACT, |
| 66 | +) { |
| 67 | + enum class Mode { |
| 68 | + REDACT, |
| 69 | + REJECT, |
| 70 | + } |
| 71 | +} |
0 commit comments