Skip to content

Commit

Permalink
OY-4717 cas-client-koodin karsintaa ja kopioitu OK-483 hakemukset ilm…
Browse files Browse the repository at this point in the history
…an hakua muutokset
  • Loading branch information
marjakari committed Dec 31, 2024
1 parent d57dd03 commit f8f6f7f
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 85 deletions.
8 changes: 4 additions & 4 deletions src/main/js/directives/application.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<div role="presentation" class="hakemuseditori">
<section class="application-header">
<h2>{{application.haku.name }}</h2>
<h2 ng-if="application.hasHaku">{{application.haku.name }}</h2>
<h2 ng-if="!application.hasHaku">{{application.formName }}</h2>

<application-periods haku="application.haku"></application-periods>
<application-periods ng-if="application.hasHaku" haku="application.haku"></application-periods>

<div class="timestamp-row" ng-show="application.haku.jarjestelmanHakulomake || application.hakemusSource === 'Ataru'">
<i ng-if="application.previewUrl" aria-hidden="true" class="icon-doc-text"></i>
Expand Down Expand Up @@ -32,8 +33,7 @@ <h2>{{application.haku.name }}</h2>
</div>
</div>
</section>

<section class="application-status" role="presentation">
<section ng-if="application.hasHaku" class="application-status" role="presentation">
<div class="application-status-item" ng-if="application.state.id != 'HAKUKIERROSPAATTYNYT' && !application.allResultsAvailable() && application.vastaanotettavatHakutoiveet().length == 0">
{{ localization('label.applicationPeriodEnded') }}
<span ng-if="application.haku.aikataulu.julkistus">
Expand Down
2 changes: 2 additions & 0 deletions src/main/js/models/hakemus.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default class Hakemus {
this.migriUrl = null;
this.hakemusSource = json.hakemusSource;
this.previewUrl = json.previewUrl;
this.formName = json.hakemus.formName;
this.hasHaku = !_.isEmpty(json.hakemus.haku);
}

removePreference(index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"1.2.246.562.20.14660127086",
"1.2.246.562.20.19202370003"
],
"submitted": "2000-01-02T00:00:00.000Z"
"submitted": "2000-01-02T00:00:00.000Z",
"form-name":
{
"fi": "Lomakkeen nimi FI",
"sv": "Lomakkeen nimi SV",
"en": "Lomakkeen nimi EN"
}
}
]
79 changes: 63 additions & 16 deletions src/main/scala/fi/vm/sade/ataru/AtaruServiceComponent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import org.asynchttpclient.RequestBuilder
import scala.concurrent.ExecutionContext.Implicits.global
import javax.servlet.http.HttpServletRequest
import org.joda.time.LocalDateTime
import org.json4s.DefaultFormats
import org.json4s.FieldSerializer.{renameFrom, renameTo}
import org.json4s.{DefaultFormats, FieldSerializer}
import org.json4s.jackson.JsonMethods.parse

import scala.compat.java8.FutureConverters.toScala
Expand All @@ -32,9 +33,10 @@ import scala.util.{Failure, Success, Try}
case class AtaruApplication(oid: String,
secret: String,
email: String,
haku: String,
haku: Option[String],
hakukohteet: List[String],
submitted: String)
submitted: String,
formName: Option[Map[String, String]])

trait AtaruServiceComponent {
this: TarjontaComponent
Expand All @@ -54,22 +56,28 @@ trait AtaruServiceComponent {
val now = new LocalDateTime().toDate.getTime
val henkilo = oppijanumerorekisteriService.henkilo(personOid)

def translate(name: Map[String,String]): Option[String] =
List(language.toString, "fi", "en", "sv")
.map(name.get)
.find(_.isDefined)
.flatten

getApplications(personOid)
.map(a => (
a,
tarjontaService.haku(a.haku, language),
a.haku.flatMap(tarjontaService.haku(_, language)),
getHakukohteet(a.hakukohteet, language),
tuloskirjeService.getTuloskirjeInfo(request, a.haku, a.oid, AccessibleHtml)
a.haku.flatMap(tuloskirjeService.getTuloskirjeInfo(request, _, a.oid, AccessibleHtml))
))
.collect {
case (a, Some(haku), Some(hakukohteet), tuloskirje) =>
val valintatulos = Try(if (valintatulosFetchStrategy.ataru(haku, henkilo, a)) {
valintatulosService.getValintatulos(a.oid, a.haku)
valintatulosService.getValintatulos(a.oid, a.haku.get)
} else {
None
})
val ohjeetUudelleOpiskelijalleMap: Map[String, String] = hakukohteet
.filter(h => !h.ohjeetUudelleOpiskelijalle.isEmpty)
.filter(h => h.ohjeetUudelleOpiskelijalle.isDefined)
.map(h => h.oid -> h.ohjeetUudelleOpiskelijalle.get)
.toMap
val hakutoiveet = hakukohteet.map(toHakutoive)
Expand All @@ -78,11 +86,11 @@ trait AtaruServiceComponent {
personOid = personOid,
received = Option.apply(Instant.parse(a.submitted).toEpochMilli),
updated = None,
state = state(now, haku, hakukohteet, a, valintatulos.getOrElse(None)),
state = state(now, Some(haku), hakukohteet, a, valintatulos.getOrElse(None)),
tuloskirje = tuloskirje,
ohjeetUudelleOpiskelijalle = ohjeetUudelleOpiskelijalleMap,
hakutoiveet = hakutoiveet,
haku = haku,
haku = Some(haku),
educationBackground = EducationBackground("base_education", false),
answers = Map(),
postOffice = None,
Expand All @@ -91,7 +99,8 @@ trait AtaruServiceComponent {
hasForm = true,
requiredPaymentState = None,
notifications = Map(),
oppijanumero = henkilo.oppijanumero.getOrElse(personOid)
oppijanumero = henkilo.oppijanumero.getOrElse(personOid),
formName = translate(a.formName.getOrElse(Map()))
)
Audit.oppija.log(ShowHakemus(request, hakemus.personOid, hakemus.oid, haku.oid))
HakemusInfo(
Expand All @@ -103,18 +112,50 @@ trait AtaruServiceComponent {
hakemusSource = "Ataru",
previewUrl = Some(OphUrlProperties.url("ataru.applications.modify", a.secret))
)
case (a, None, _, tuloskirje) =>
val hakemus = Hakemus(
oid = a.oid,
personOid = personOid,
received = Option.apply(Instant.parse(a.submitted).toEpochMilli),
updated = None,
state = state(now, None, List.empty, a, None),
tuloskirje = tuloskirje,
ohjeetUudelleOpiskelijalle = Map(),
hakutoiveet = List.empty,
haku = None,
educationBackground = EducationBackground("base_education", false),
answers = Map(),
postOffice = None,
email = Some(a.email),
requiresAdditionalInfo = false,
hasForm = true,
formName = translate(a.formName.getOrElse(Map())),
requiredPaymentState = None,
notifications = Map(),
oppijanumero = henkilo.oppijanumero.getOrElse(personOid)
)
Audit.oppija.log(ShowHakemus(request, hakemus.personOid, hakemus.oid, ""))
HakemusInfo(
hakemus = hakemus,
errors = List(),
questions = List(),
tulosOk = true,
paymentInfo = None,
hakemusSource = "Ataru",
previewUrl = Some(OphUrlProperties.url("ataru.applications.modify", a.secret))
)
}
}

private def state(now: Long,
haku: Haku,
haku: Option[Haku],
hakukohteet: List[Hakukohde],
application: AtaruApplication,
valintatulos: Option[Hakemus.Valintatulos]): HakemuksenTila = {
if (hakukohteet.exists(KohteenHakuaika.hakuaikaEnded(haku, _, now))) {
if (haku.aikataulu.exists(_.hakukierrosPaattyy.exists(_ < now))) {
if (haku.isDefined && hakukohteet.exists(KohteenHakuaika.hakuaikaEnded(haku.get, _, now))) {
if (haku.get.aikataulu.exists(_.hakukierrosPaattyy.exists(_ < now))) {
HakukierrosPaattynyt(valintatulos = valintatulos)
} else if (hakukohteet.forall(KohteenHakuaika.hakuaikaEnded(haku, _, now))) {
} else if (hakukohteet.forall(KohteenHakuaika.hakuaikaEnded(haku.get, _, now))) {
HakukausiPaattynyt(valintatulos = valintatulos)
} else {
Active(valintatulos = valintatulos)
Expand Down Expand Up @@ -152,7 +193,10 @@ trait AtaruServiceComponent {
// feels a bit strange to have the testing code mixed with the production code,
// but I am only following the pattern used in the rest of this application
class StubbedAtaruService extends AtaruServiceCommons {
implicit private val formats = DefaultFormats
implicit private val formats = DefaultFormats +
FieldSerializer[AtaruApplication](
renameTo("formName", "form-name"), renameFrom("form-name", "formName")
)

def getApplications(personOid: String): List[AtaruApplication] = {
personOid match {
Expand All @@ -179,7 +223,10 @@ trait AtaruServiceComponent {

private val casClient: CasClient = CasClientBuilder.build(casConfig)

implicit private val formats = DefaultFormats
implicit private val formats = DefaultFormats +
FieldSerializer[AtaruApplication](
renameTo("formName", "form-name"), renameFrom("form-name", "formName")
)

def getApplications(personOid: String): List[AtaruApplication] = {
val req = new RequestBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ trait HakemusEditoriComponent extends AtaruServiceComponent

def fetchTuloskirje(request: HttpServletRequest, personOid: String, hakuOid: String, tuloskirjeKind: TuloskirjeKind): Option[Array[Byte]] = {
val hakemukset = fetchByPersonOid(request, personOid, DontFetch) match {
case FullSuccess(hs) => hs.find(_.hakemus.haku.oid == hakuOid)
case FullSuccess(hs) => hs.find((h: HakemusInfo) => h.hakemus.haku.isDefined && h.hakemus.haku.get.oid == hakuOid)
case PartialSuccess(_, ts) => throw ts.head
case FullFailure(ts) => throw ts.head
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ trait HakemusConverterComponent {
tuloskirje,
ohjeetUudelleOpiskelijalleMap,
hakutoiveet,
haku,
Some(haku),
EducationBackground(koulutusTaustaAnswers.get(baseEducationKey), !Try {koulutusTaustaAnswers.get("ammatillinenTutkintoSuoritettu").toBoolean}.getOrElse(false)),
answers,
None,
Expand All @@ -68,7 +68,8 @@ trait HakemusConverterComponent {
false,
None,
Map.empty,
henkilo.oppijanumero.getOrElse(application.personOid)
henkilo.oppijanumero.getOrElse(application.personOid),
None
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import fi.vm.sade.omatsivut.util.Logging
import fi.vm.sade.omatsivut.util.Timer.timed
import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods.parse
import org.json4s.jackson.Serialization.read

import javax.servlet.http.HttpServletRequest
import scala.util.{Failure, Success, Try}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ object ImmutableLegacyApplicationWrapper extends Logging {

def wrap(application: Application) = {
logger.info("wrapataan")
logger.info(application.getOid)
logger.info(application.getAnswers.size().toString)
logger.info(s"application oid: ${application.getOid}")
logger.info(s"application answers: ${application.getAnswers.size()}")
val answers: LegacyApplicationAnswers = {
application.getAnswers.toMap.mapValues { phaseAnswers => phaseAnswers.toMap }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ case class Hakemus(oid: String,
tuloskirje: Option[Tuloskirje] = None,
ohjeetUudelleOpiskelijalle: Map[String, String], // hakukohdeOid -> linkki
hakutoiveet: List[Hakutoive] = Nil,
haku: Haku,
haku: Option[Haku],
educationBackground: EducationBackground,
answers: Answers,
postOffice: Option[String],
Expand All @@ -47,8 +47,9 @@ case class Hakemus(oid: String,
hasForm: Boolean,
requiredPaymentState: Option[String],
notifications: Map[String, Map[String, Boolean]],
oppijanumero: String) extends HakemusLike {
def preferences = hakutoiveet.map(_.hakemusData.getOrElse(Map.empty))
oppijanumero: String,
formName: Option[String]) extends HakemusLike {
def preferences: List[HakutoiveData] = hakutoiveet.map(_.hakemusData.getOrElse(Map.empty))

def withoutKelaUrl: Hakemus = copy(state = state.withoutKelaUrl)

Expand Down
35 changes: 0 additions & 35 deletions src/main/scala/fi/vm/sade/omatsivut/cas/CasClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,9 @@ class CasClient(casBaseUrl: Uri, client: Client[IO], callerId: String) extends L
def this(casServer: String, client: Client[IO], callerId: String) = this(Uri.fromString(casServer).right.get, client, callerId)

def validateServiceTicketWithOppijaAttributes(service: String)(serviceTicket: ServiceTicket): IO[OppijaAttributes] = {
logger.info(s"validateServiceTicketWithOppijaAttributes")
validateServiceTicket[OppijaAttributes](casBaseUrl, client, service, decodeOppijaAttributes)(serviceTicket)
}

def validateServiceTicketWithVirkailijaUsername(service: String)(serviceTicket: ServiceTicket): IO[Username] = {
validateServiceTicket[Username](casBaseUrl, client, service, decodeVirkailijaUsername)(serviceTicket)
}

def validateServiceTicket[R](service: String)(serviceTicket: ServiceTicket, responseHandler: Response[IO] => IO[R]): IO[R] = {
validateServiceTicket[R](casBaseUrl, client, service, responseHandler)(serviceTicket)
}

private def validateServiceTicket[R](casBaseUrl: Uri, client: Client[IO], service: String, responseHandler: Response[IO] => IO[R])(serviceTicket: ServiceTicket): IO[R] = {
val pUri: Uri = casBaseUrl.addPath("serviceValidate")
.withQueryParam("ticket", serviceTicket)
Expand All @@ -58,10 +49,6 @@ class CasClient(casBaseUrl: Uri, client: Client[IO], callerId: String) extends L
FetchHelper.fetch[R](client, callerId, request, responseHandler)
}

def authenticateVirkailija(user: CasUser): IO[Boolean] = {
TicketGrantingTicketClient.getTicketGrantingTicket(casBaseUrl, client, user, callerId)
.map(_tgtUrl => true) // Authentication succeeded if we received a tgtUrl
}

/**
* Establishes session with the requested service by
Expand Down Expand Up @@ -134,21 +121,6 @@ class CasClient(casBaseUrl: Uri, client: Client[IO], callerId: String) extends L
}
}

private val virkailijaServiceTicketDecoder: EntityDecoder[IO, Username] =
textOrXmlDecoder
.map(s => Utility.trim(scala.xml.XML.loadString(s)))
.flatMapR[Username] { serviceResponse => {
val user = (serviceResponse \ "authenticationSuccess" \ "user")
user.length match {
case 1 => DecodeResult.successT(user.text)
case _ =>
DecodeResult.failureT(InvalidMessageBodyFailure(
s"Virkailija Service Ticket validation response decoding failed: response body is of wrong form ($serviceResponse)"
))
}
}
}

private def casFailure[R](debugLabel: String, resp: Response[IO]): EitherT[IO, DecodeFailure, R] = {
textOrXmlDecoder
.decode(resp, strict = false)
Expand All @@ -164,16 +136,9 @@ class CasClient(casBaseUrl: Uri, client: Client[IO], callerId: String) extends L
* Decode CAS Oppija's service ticket validation response to various oppija attributes.
*/
def decodeOppijaAttributes: Response[IO] => IO[OppijaAttributes] = { response =>
logger.info("decodeOppijaAttributes")
decodeCASResponse[OppijaAttributes](response, "oppija attributes", oppijaServiceTicketDecoder)
}

/**
* Decode CAS Virkailija's service ticket validation response to username.
*/
def decodeVirkailijaUsername: Response[IO] => IO[Username] = { response =>
decodeCASResponse[Username](response, "username", virkailijaServiceTicketDecoder)
}

private def decodeCASResponse[R](response: Response[IO], debugLabel: String, decoder: EntityDecoder[IO, R]): IO[R] = {
val decodeResult = if (response.status.isSuccess) {
Expand Down
13 changes: 0 additions & 13 deletions src/main/scala/fi/vm/sade/omatsivut/cas/CasLogout.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class FakeCasClient(casBaseUrl: String, client: Client[IO], callerId: String, au
}
}

override def decodeVirkailijaUsername: Response[IO] => IO[Username] = response => IO.pure("frank-virkailija")
override def fetchCasSession(params: CasParams, sessionCookieName: String): IO[SessionCookie] = {
IO.pure("keksi")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ trait SecuredSessionServletContainer {
.validateServiceTicketWithOppijaAttributes(initsessionPath(request.getContextPath()))(ticket)
.timeout(Duration(10, TimeUnit.SECONDS))
.attempt
logger.info(s"got result $result")
result.unsafeRunSync() match { // Execute IO synchronously, required for servlets
case Right(attrs) =>
logger.info(s"User logging in: $attrs")
if (isUsingValtuudet(attrs)) {
logger.info(s"User ${attrs.getOrElse("impersonatorDisplayName", "NOT_FOUND")} is using valtuudet; Will not init session and should redirect to ${valtuudetRedirectUri}")
redirect(valtuudetRedirectUri)
} else {
logger.info(s"Parsing attributes")
logger.info(s"Parsing user attributes")
val hetu = attrs("nationalIdentificationNumber")
val personOid = attrs.getOrElse("personOid", "")
val displayName = attrs.getOrElse("displayName", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait VastaanottoComponent {
class VastaanottoService()(implicit language: Language) extends JsonFormats {

def vastaanota(request: HttpServletRequest, hakemusOid: String, hakukohdeOid: String, henkiloOid: String, vastaanotto: Vastaanotto, hakemus: HakemusInfo): ActionResult = {
val hakuOid: String = hakemus.hakemus.haku.oid
val hakuOid: String = hakemus.hakemus.haku.map(_.oid).getOrElse("")
val email: Option[String] = hakemus.hakemus.email
val henkiloOidHakemukselta = hakemus.hakemus.personOid
if (!henkiloOid.equals(henkiloOidHakemukselta)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
////
//// "use application system's application period when application type is not 'LISÄHAKU'" in {
//// withHakemusWithEmptyAnswers(hakemusYhteishakuKevat2014WithForeignBaseEducationId) { hakemusInfo =>
//// hakemusInfo.hakemus.haku.applicationPeriods.head must_== TestFixture.hakemus2_hakuaika
//// hakemusInfo.hakemus.haku.get.applicationPeriods.head must_== TestFixture.hakemus2_hakuaika
//// }
//// }
//// "provide additional application period for application with athlete questions" in {
Expand Down
Loading

0 comments on commit f8f6f7f

Please sign in to comment.