Skip to content

Commit

Permalink
GFORMS-3014 - Add detail.FormTemplateId to gform-frontend requestRece…
Browse files Browse the repository at this point in the history
…ived events (#2308)
  • Loading branch information
josef-vlach authored Nov 1, 2024
1 parent 90a94ef commit cf1c10d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
29 changes: 28 additions & 1 deletion app/uk/gov/hmrc/gform/playcomponents/FrontendFiltersModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package uk.gov.hmrc.gform.playcomponents

import org.apache.pekko.stream.Materializer
import play.api.mvc.{ CookieHeaderEncoding, DefaultCookieHeaderEncoding, EssentialFilter, SessionCookieBaker }
import play.api.libs.json.{ JsObject, Json }
import play.api.mvc.{ CookieHeaderEncoding, DefaultCookieHeaderEncoding, EssentialFilter, RequestHeader, SessionCookieBaker }
import play.filters.cors.{ CORSConfig, CORSFilter }
import play.filters.csrf.CSRFComponents
import play.filters.headers.SecurityHeadersFilter
Expand All @@ -27,6 +28,8 @@ import uk.gov.hmrc.gform.config.ConfigModule
import uk.gov.hmrc.gform.controllers.{ ControllersModule, ErrorHandler }
import uk.gov.hmrc.gform.gformbackend.GformBackendModule
import uk.gov.hmrc.crypto.ApplicationCrypto
import uk.gov.hmrc.http.HeaderCarrier
import uk.gov.hmrc.play.audit.model.{ ExtendedDataEvent, RedactionLog, TruncationLog }
import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.DefaultSessionCookieCryptoFilter
import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.SessionCookieCrypto
import uk.gov.hmrc.play.bootstrap.frontend.filters.crypto.SessionCookieCryptoFilter
Expand Down Expand Up @@ -96,6 +99,30 @@ class FrontendFiltersModule(
materializer
) {
override val maskedFormFields = Seq("password")

override def extendedDataEvent(
eventType: String,
transactionName: String,
request: RequestHeader,
detail: JsObject,
truncationLog: TruncationLog,
redactionLog: RedactionLog
)(implicit hc: HeaderCarrier): ExtendedDataEvent = {

val updatedDetail = RequestFormTemplateId.formTemplateId(request) match {
case Some(formTemplateId) => detail ++ Json.obj("formTemplateId" -> formTemplateId.value)
case None => detail
}

super.extendedDataEvent(
eventType,
transactionName,
request,
updatedDetail,
truncationLog,
redactionLog
)
}
}

private val hmrcSessionCookieCryptoFilter: SessionCookieCryptoFilter = {
Expand Down
40 changes: 40 additions & 0 deletions app/uk/gov/hmrc/gform/playcomponents/RequestFormTemplateId.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.gform.playcomponents

import cats.syntax.eq._
import play.api.mvc.RequestHeader
import play.api.routing.Router.RequestImplicits._
import uk.gov.hmrc.gform.sharedmodel.formtemplate.FormTemplateId

object RequestFormTemplateId {
def formTemplateId(rh: RequestHeader): Option[FormTemplateId] = {
val formTemplateIdParamIndex: Option[Int] = {
val mayContainsFormTemplateId: Option[Array[Boolean]] =
rh.handlerDef.map(_.path.split("/")).map(_.map(_.containsSlice("$formTemplateId")))
mayContainsFormTemplateId.map(_.indexOf(true))
}

formTemplateIdParamIndex match {
case Some(i) if i =!= -1 =>
val templateId = rh.uri.split("\\?")(0).split("/")(i)
Some(FormTemplateId(templateId))
case _ =>
None
}
}
}
21 changes: 5 additions & 16 deletions app/uk/gov/hmrc/gform/playcomponents/RequestHeaderService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package uk.gov.hmrc.gform.playcomponents

import cats.syntax.eq._
import play.api.mvc.RequestHeader
import play.api.routing.Router.RequestImplicits._
import scala.concurrent.{ ExecutionContext, Future }
import uk.gov.hmrc.gform.gformbackend.GformConnector
import uk.gov.hmrc.gform.sharedmodel.formtemplate.{ FormTemplateContext, FormTemplateId }
Expand All @@ -29,20 +27,11 @@ final class RequestHeaderService(
gformConnector: GformConnector
)(implicit ec: ExecutionContext) {

def formTemplateContext(rh: RequestHeader): Future[Option[FormTemplateContext]] = {
val formTemplateIdParamIndex: Option[Int] = {
val mayContainsFormTemplateId: Option[Array[Boolean]] =
rh.handlerDef.map(_.path.split("/")).map(_.map(_.containsSlice("$formTemplateId")))
mayContainsFormTemplateId.map(_.indexOf(true))
}

formTemplateIdParamIndex match {
case Some(i) if i =!= -1 =>
val templateId = rh.uri.split("\\?")(0).split("/")(i)
def formTemplateContext(rh: RequestHeader): Future[Option[FormTemplateContext]] =
RequestFormTemplateId.formTemplateId(rh) match {
case Some(FormTemplateId(formTemplateId)) =>
implicit val hc: HeaderCarrier = HeaderCarrierConverter.fromRequest(rh)
gformConnector.getFormTemplateContext(FormTemplateId(templateId.toLowerCase)).map(Some(_))
case _ =>
Future.successful(None)
gformConnector.getFormTemplateContext(FormTemplateId(formTemplateId.toLowerCase)).map(Some(_))
case None => Future.successful(None)
}
}
}

0 comments on commit cf1c10d

Please sign in to comment.