Skip to content

Commit

Permalink
tooling: add script for comparing wiki list against fabX
Browse files Browse the repository at this point in the history
  • Loading branch information
sschaeffner committed Oct 11, 2024
1 parent eef2408 commit 1941b6c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ build

# macOS
.DS_Store

# tooling
tooling/wikiusers.html
114 changes: 114 additions & 0 deletions tooling/checkUsersAgainstWiki.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@file:DependsOn("io.ktor:ktor-client-core-jvm:2.3.12")
@file:DependsOn("io.ktor:ktor-client-cio-jvm:2.3.12")
@file:DependsOn("io.ktor:ktor-client-content-negotiation-jvm:2.3.12")
@file:DependsOn("io.ktor:ktor-client-auth-jvm:2.3.12")
@file:DependsOn("io.ktor:ktor-serialization-kotlinx-json-jvm:2.3.12")

@file:DependsOn("io.ktor:ktor-client-okhttp:2.3.12")
@file:DependsOn("io.ktor:ktor-client-gson:2.3.12")
@file:DependsOn("../web/build/libs/web-0.0.0-SNAPSHOT.jar")

import cloud.fabX.fabXaccess.user.rest.User
import io.ktor.client.HttpClient
import io.ktor.client.call.*
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.auth.Auth
import io.ktor.client.plugins.auth.providers.BasicAuthCredentials
import io.ktor.client.plugins.auth.providers.basic
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import java.io.File
import kotlin.text.Regex
import kotlinx.coroutines.runBlocking
import cloud.fabX.fabXaccess.user.rest.CardIdentity


val fabxUsername = System.getenv("USERNAME") ?: "admin"
val fabxPassword = System.getenv("PASSWORD") ?: "password"
val fabxBaseUrl = System.getenv("BASE_URL") ?: "http://localhost:8080"

val client = HttpClient(CIO) {
install(ContentNegotiation) {
json()
}
install(Auth) {
basic {
credentials {
BasicAuthCredentials(username = fabxUsername, password = fabxPassword)
}
}
}
}

fun getFabxUsersFromBackend(): List<User> {
val fabxUsers = runBlocking {
println("getting fabX users...")
val r: HttpResponse = client.get("$fabxBaseUrl/api/v1/user")
println("\t...${r.status}")
if (r.status != HttpStatusCode.OK) {
System.exit(42)
}
r.body<List<User>>()
}

return fabxUsers
}

fun readWikiUsers(): Set<String> {
val regex = Regex("""data-username="(?<wikiname>[\w]+)"""")

val wikinames = File("./wikiusers.html").bufferedReader().readLines()
.flatMap { line ->
regex.findAll(line).map { matchResult -> matchResult.groups["wikiname"]!!.value }
}
.distinct()
.toSet()

println("Found ${wikinames.size} wikinames in wiki table")

return wikinames
}

val fabxUsers: List<User> = getFabxUsersFromBackend()
val wikinames: Set<String> = readWikiUsers()

val fabxWikiNames: Set<String> = fabxUsers.map { it.wikiName }.toSet()

// WITHOUT FABX
val usersWithoutFabx: Set<String> = wikinames - fabxWikiNames

println()
println("USERS WITHOUT fabX ACCOUNT (${usersWithoutFabx.size})")
usersWithoutFabx.sorted().forEach { println(it) }

// WITH FABX
val usersWithFabx: Set<User> = fabxUsers
.filter { u -> wikinames.contains(u.wikiName) }
.toSet()

// WITH FABX BUT WITHOUT CARD
val usersWithoutCard: Set<String> = usersWithFabx
.filter { user ->
!user.identities.any { it is CardIdentity }
}
.map { user -> user.wikiName }
.toSet()

println()
println("USERS WITHOUT fabX CARD (${usersWithoutCard.size})")
usersWithoutCard.sorted().forEach { println(it) }

// WITH FABX AND WITH CARD
val usersWithCard: Set<String> = usersWithFabx
.filter { user ->
user.identities.any { it is CardIdentity }
}
.map { user -> user.wikiName }
.toSet()

println()
println("USERS WITH fabX CARD (${usersWithCard.size})")
usersWithCard.sorted().forEach { println(it) }

0 comments on commit 1941b6c

Please sign in to comment.