Skip to content

Commit 10bb3c5

Browse files
scezenvsct-jburet
authored andcommitted
Updated endpoint & CASAuth
1 parent d8b7007 commit 10bb3c5

File tree

2 files changed

+104
-68
lines changed

2 files changed

+104
-68
lines changed

nlp/admin/server/src/main/kotlin/AdminVerticle.kt

Lines changed: 92 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,21 @@ open class AdminVerticle : WebVerticle() {
121121
actionType: String,
122122
dataProvider: (RoutingContext) -> Any? = { null },
123123
applicationIdProvider: (RoutingContext, Any?) -> Id<ApplicationDefinition>? = { context, _ ->
124-
context.pathParam(
125-
"applicationId",
126-
)?.toId()
124+
context
125+
.pathParam(
126+
"applicationId",
127+
)?.toId()
127128
},
128129
): RequestLogger = logger<Any>(actionType, dataProvider, applicationIdProvider)
129130

130131
inline fun <T> logger(
131132
actionType: String,
132133
noinline dataProvider: (RoutingContext) -> Any? = { null },
133134
crossinline applicationIdProvider: (RoutingContext, T?) -> Id<ApplicationDefinition>? = { context, _ ->
134-
context.pathParam(
135-
"applicationId",
136-
)?.toId()
135+
context
136+
.pathParam(
137+
"applicationId",
138+
)?.toId()
137139
},
138140
): RequestLogger =
139141
object : RequestLogger {
@@ -168,11 +170,13 @@ open class AdminVerticle : WebVerticle() {
168170

169171
// Retrieve all applications of the namespace
170172
blockingJsonGet("/applications") { context ->
171-
front.getApplications().filter {
172-
it.namespace == context.organization
173-
}.map {
174-
service.getApplicationWithIntents(it)
175-
}
173+
front
174+
.getApplications()
175+
.filter {
176+
it.namespace == context.organization
177+
}.map {
178+
service.getApplicationWithIntents(it)
179+
}
176180
}
177181

178182
// Retrieve all applications of the selected namespace
@@ -181,24 +185,28 @@ open class AdminVerticle : WebVerticle() {
181185
val user: String = context.userLogin
182186

183187
if (allowAccessToAllNamespaces || front.hasNamespace(user, namespace)) {
184-
front.getApplications().filter {
185-
it.namespace == namespace
186-
}.map {
187-
service.getApplicationWithIntents(it)
188-
}
188+
front
189+
.getApplications()
190+
.filter {
191+
it.namespace == namespace
192+
}.map {
193+
service.getApplicationWithIntents(it)
194+
}
189195
} else {
190196
badRequest("Unauthorized access to namespace '$namespace'")
191197
}
192198
}
193199

194200
// Retrieve application that matches given identifier
195201
blockingJsonGet("/application/:applicationId") { context ->
196-
service.getApplicationWithIntents(context.pathId("applicationId"))
202+
service
203+
.getApplicationWithIntents(context.pathId("applicationId"))
197204
?.takeIf { it.namespace == context.organization }
198205
}
199206

200207
blockingJsonGet("/application/:applicationId/model/:engine/configuration", admin) { context ->
201-
front.getApplicationById(context.pathId("applicationId"))
208+
front
209+
.getApplicationById(context.pathId("applicationId"))
202210
?.takeIf { it.namespace == context.organization }
203211
?.let { front.getCurrentModelConfiguration(it.qualifiedName, NlpEngineType(context.path("engine"))) }
204212
}
@@ -208,7 +216,8 @@ open class AdminVerticle : WebVerticle() {
208216
admin,
209217
simpleLogger("Model Configuration"),
210218
) { context, conf: NlpApplicationConfiguration ->
211-
front.getApplicationById(context.pathId("applicationId"))
219+
front
220+
.getApplicationById(context.pathId("applicationId"))
212221
?.takeIf { it.namespace == context.organization && it.supportedLocales.isNotEmpty() }
213222
?.let { front.updateModelConfiguration(it.qualifiedName, NlpEngineType(context.path("engine")), conf) }
214223
}
@@ -583,10 +592,10 @@ open class AdminVerticle : WebVerticle() {
583592
supportedLanguages
584593
.map {
585594
it.key to
586-
it.value.getDisplayLanguage(Locale.ENGLISH)
595+
it.value
596+
.getDisplayLanguage(Locale.ENGLISH)
587597
.replaceFirstChar { c -> if (c.isLowerCase()) c.titlecase(Locale.getDefault()) else c.toString() }
588-
}
589-
.sortedBy { it.second }
598+
}.sortedBy { it.second }
590599
}
591600

592601
blockingJsonPost("/parse", setOf(nlpUser)) { context, query: ParseQuery ->
@@ -635,14 +644,15 @@ open class AdminVerticle : WebVerticle() {
635644
val decrypt = decrypt(key)
636645
val applicationDefinition = front.getApplicationById(s.applicationId)
637646
if (applicationDefinition?.namespace == context.organization &&
638-
front.search(
639-
SentencesQuery(
640-
applicationId = applicationDefinition._id,
641-
language = s.language,
642-
search = decrypt,
643-
onlyExactMatch = true,
644-
),
645-
).total != 0L
647+
front
648+
.search(
649+
SentencesQuery(
650+
applicationId = applicationDefinition._id,
651+
language = s.language,
652+
search = decrypt,
653+
onlyExactMatch = true,
654+
),
655+
).total != 0L
646656
) {
647657
s.copy(text = decrypt, key = null)
648658
} else {
@@ -691,7 +701,8 @@ open class AdminVerticle : WebVerticle() {
691701
val sb = StringBuilder()
692702
val p = newPrinter(sb)
693703

694-
front.export(app._id, context.pathToLocale("locale"))
704+
front
705+
.export(app._id, context.pathToLocale("locale"))
695706
.forEach {
696707
p.printRecord(it.date, it.intent, it.text)
697708
}
@@ -728,10 +739,12 @@ open class AdminVerticle : WebVerticle() {
728739
if (context.organization == s.namespace) {
729740
front.search(
730741
s.toRequestLogStatQuery(
731-
front.getApplicationByNamespaceAndName(
732-
s.namespace,
733-
s.applicationName,
734-
)!!._id,
742+
front
743+
.getApplicationByNamespaceAndName(
744+
s.namespace,
745+
s.applicationName,
746+
)!!
747+
._id,
735748
),
736749
)
737750
} else {
@@ -748,11 +761,13 @@ open class AdminVerticle : WebVerticle() {
748761
}
749762

750763
blockingJsonGet("/intents") { context ->
751-
front.getApplications().filter {
752-
it.namespace == context.organization
753-
}.map {
754-
service.getApplicationWithIntents(it)
755-
}
764+
front
765+
.getApplications()
766+
.filter {
767+
it.namespace == context.organization
768+
}.map {
769+
service.getApplicationWithIntents(it)
770+
}
756771
}
757772

758773
blockingJsonGet("/entity-types") { context ->
@@ -777,7 +792,8 @@ open class AdminVerticle : WebVerticle() {
777792

778793
blockingJsonGet("/dictionary/:qualifiedName") { context ->
779794
context.path("qualifiedName").let { n ->
780-
n.takeUnless { it.namespace() != context.organization }
795+
n
796+
.takeUnless { it.namespace() != context.organization }
781797
?.let { front.getDictionaryDataByEntityName(it) }
782798
?: DictionaryData(n.namespace(), n.name())
783799
}
@@ -832,7 +848,8 @@ open class AdminVerticle : WebVerticle() {
832848
) { context, entityType: EntityTypeDefinition ->
833849
if (context.organization == entityType.name.namespace()) {
834850
val update =
835-
front.getEntityTypeByName(entityType.name)
851+
front
852+
.getEntityTypeByName(entityType.name)
836853
?.run {
837854
copy(
838855
description = entityType.description,
@@ -920,8 +937,7 @@ open class AdminVerticle : WebVerticle() {
920937
query.namespace,
921938
query.applicationName,
922939
)
923-
if (context.organization == app?.namespace
924-
) {
940+
if (context.organization == app?.namespace) {
925941
AdminService.testBuildStats(query, app)
926942
} else {
927943
unauthorized()
@@ -943,24 +959,25 @@ open class AdminVerticle : WebVerticle() {
943959
simpleLogger("Update Predefined Value"),
944960
) { context, query: PredefinedValueQuery ->
945961

946-
front.getDictionaryDataByEntityName(query.entityTypeName)
962+
front
963+
.getDictionaryDataByEntityName(query.entityTypeName)
947964
?.takeIf { it.namespace == context.organization }
948965
?.run {
949966
val value = query.oldPredefinedValue ?: query.predefinedValue
950967
copy(
951968
values =
952969
values.filter { it.value != value } +
953970
(
954-
values.find { it.value == value }
971+
values
972+
.find { it.value == value }
955973
?.copy(value = query.predefinedValue)
956974
?: PredefinedValue(
957975
query.predefinedValue,
958976
mapOf(query.locale to listOf(query.predefinedValue)),
959977
)
960978
),
961979
)
962-
}
963-
?.also {
980+
}?.also {
964981
front.save(it)
965982
}
966983
?: unauthorized()
@@ -985,14 +1002,16 @@ open class AdminVerticle : WebVerticle() {
9851002
simpleLogger("Update Predefined Labels"),
9861003
) { context, query: PredefinedLabelQuery ->
9871004

988-
front.getDictionaryDataByEntityName(query.entityTypeName)
1005+
front
1006+
.getDictionaryDataByEntityName(query.entityTypeName)
9891007
?.takeIf { it.namespace == context.organization }
9901008
?.run {
9911009
copy(
9921010
values =
9931011
values.filter { it.value != query.predefinedValue } +
9941012
(
995-
values.find { it.value == query.predefinedValue }
1013+
values
1014+
.find { it.value == query.predefinedValue }
9961015
?.run {
9971016
copy(
9981017
labels =
@@ -1014,8 +1033,7 @@ open class AdminVerticle : WebVerticle() {
10141033
)
10151034
),
10161035
)
1017-
}
1018-
?.also {
1036+
}?.also {
10191037
front.save(it)
10201038
}
10211039
?: unauthorized()
@@ -1134,7 +1152,12 @@ open class AdminVerticle : WebVerticle() {
11341152
val n = context.path("namespace").trim()
11351153
if (front.hasNamespace(context.userLogin, n)) {
11361154
front.setCurrentNamespace(context.userLogin, n)
1137-
(context as UserContextInternal).setUser(context.user!!.copy(namespace = n))
1155+
1156+
val updated = context.user!!.copy(namespace = n)
1157+
1158+
// Update the user through userContext() and persist in session so next calls see the change.
1159+
(context.userContext() as UserContextInternal).setUser(updated)
1160+
context.session()?.put("tockUser", updated)
11381161
} else {
11391162
unauthorized()
11401163
}
@@ -1196,7 +1219,10 @@ open class AdminVerticle : WebVerticle() {
11961219
val webRoot = verticleProperty("content_path", "/maven/dist")
11971220
// swagger yaml
11981221
router.get("${baseHref}doc/nlp.yaml").handler { context ->
1199-
context.vertx().fileSystem().readFile("$webRoot/doc/nlp.yaml")
1222+
context
1223+
.vertx()
1224+
.fileSystem()
1225+
.readFile("$webRoot/doc/nlp.yaml")
12001226
.onComplete {
12011227
if (it.succeeded()) {
12021228
context.response().end(
@@ -1223,28 +1249,32 @@ open class AdminVerticle : WebVerticle() {
12231249
val indexContentHandler =
12241250
Handler<RoutingContext> { context ->
12251251
if (indexContent != null) {
1226-
context.response()
1252+
context
1253+
.response()
12271254
.putHeader(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8")
12281255
.end(indexContent)
12291256
} else {
12301257
context.vertx().fileSystem().readFile("$webRoot/index.html").onComplete {
12311258
if (it.succeeded()) {
12321259
logger.info { "base href: $baseHref" }
12331260
val content =
1234-
it.result()
1261+
it
1262+
.result()
12351263
.toString(UTF_8)
12361264
.replace("<base href=\"/\"", "<base href=\"$baseHref\"")
12371265
logger.debug { "content: $content" }
12381266
val result = Buffer.buffer(content)
12391267
indexContent = result
12401268

1241-
context.response()
1269+
context
1270+
.response()
12421271
.putHeader(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8")
12431272
.end(result)
12441273
} else {
12451274
logger.warn { "Can't find $webRoot/index.html" }
12461275
context.response().statusCode = 404
1247-
context.response()
1276+
context
1277+
.response()
12481278
.putHeader(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8")
12491279
.end("<html><body><h1>Resource not found</h1></body></html>")
12501280
}
@@ -1258,7 +1288,8 @@ open class AdminVerticle : WebVerticle() {
12581288
router.route(GET, baseHref).handler(indexContentHandler)
12591289
router.route(GET, "${baseHref}index.html").handler(indexContentHandler)
12601290

1261-
router.route(GET, "$baseHref*")
1291+
router
1292+
.route(GET, "$baseHref*")
12621293
.handler(StaticHandler.create(FileSystemAccess.ROOT, webRoot))
12631294
.handler(indexContentHandler)
12641295
} else {
@@ -1273,18 +1304,14 @@ open class AdminVerticle : WebVerticle() {
12731304
protected open fun saveApplication(
12741305
existingApp: ApplicationDefinition?,
12751306
app: ApplicationDefinition,
1276-
): ApplicationDefinition {
1277-
return front.save(app)
1278-
}
1307+
): ApplicationDefinition = front.save(app)
12791308

12801309
override fun configure() {
12811310
configureServices()
12821311
configureStaticHandling()
12831312
}
12841313

1285-
override fun defaultHealthcheck(): (RoutingContext) -> Unit {
1286-
return { it.response().end() }
1287-
}
1314+
override fun defaultHealthcheck(): (RoutingContext) -> Unit = { it.response().end() }
12881315

12891316
override fun detailedHealthcheck(): (RoutingContext) -> Unit =
12901317
detailedHealthcheck(

shared/src/main/kotlin/security/auth/CASAuthProvider.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ import org.pac4j.vertx.handler.impl.CallbackHandlerOptions
5050
import org.pac4j.vertx.handler.impl.SecurityHandler
5151
import org.pac4j.vertx.handler.impl.SecurityHandlerOptions
5252

53-
abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
53+
abstract class CASAuthProvider(
54+
vertx: Vertx,
55+
) : SSOTockAuthProvider(vertx) {
5456
protected val sessionStore: VertxSessionStore
5557
private val executor: Executor get() = injector.provide()
5658

@@ -122,7 +124,8 @@ abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
122124
for ((namespace, roles) in rolesByNamespace) {
123125
user =
124126
injector.provide<TockUserListener>().registerUser(
125-
TockUser(username, namespace, roles), isJoinNamespace,
127+
TockUser(username, namespace, roles),
128+
isJoinNamespace,
126129
)
127130
}
128131
return user!! // !! is because user is already guaranteed to be not null
@@ -193,7 +196,13 @@ abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
193196
verticle.router.route("/*").handler(
194197
WithExcludedPathHandler(excluded) { rc ->
195198
val user = rc.user()
196-
if (user != null && user !is TockUser) { // user is Pac4jUser
199+
val sessionTockUser = rc.session()?.get("tockUser") as? TockUser
200+
201+
if (sessionTockUser != null) {
202+
// If a TockUser is already in session (ex: after a namespace switch), reuse it and don't overwrite it.
203+
(rc.userContext() as UserContextInternal).setUser(sessionTockUser)
204+
rc.next()
205+
} else if (user != null && user !is TockUser) { // user is Pac4jUser
197206
executor.executeBlocking {
198207
upgradeToTockUser(getUserCasProfile(rc)) { hr ->
199208
if (hr.succeeded()) {

0 commit comments

Comments
 (0)