Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/main/kotlin/hu/kirdev/schpincer/model/SchPincerOidcUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUser

enum class CardType { DO, KB, AB }
data class CircleMembership(val id: Long, val name: String, val title: List<String>)
data class ExecutiveAt(val id: Long, val name: String)
data class Entrant(val groupId: Long, val groupName: String, val entrantType: String)

class SchPincerOidcUser(private val oidcUser: OidcUser) : OidcUser by oidcUser {
val internalId get() = subject
var extraAuthorities: List<GrantedAuthority> = listOf()
val memberships = parseCircleMemberships()
val executiveAtCircles = parseExecutiveAt()
val entrants = parseEntrants()
val cardType = getCardType(entrants)

Expand All @@ -28,22 +30,41 @@ class SchPincerOidcUser(private val oidcUser: OidcUser) : OidcUser by oidcUser {
return card
}

private fun parseExecutiveAt(): List<ExecutiveAt> {
val executiveAt = oidcUser.getClaim<List<Map<String, Any>>>("pek.sch.bme.hu:executiveAt/v1") ?: listOf()
return executiveAt.mapNotNull {
runCatching {
ExecutiveAt(
(it["id"] as Number).toLong(),
it["name"].toString(),
)
}.getOrNull()
}
}

private fun parseCircleMemberships(): List<CircleMembership> {
val memberships = oidcUser.getClaim<List<Map<String, Any>>>("pek.sch.bme.hu:activeMemberships/v1") ?: listOf()
return memberships.mapNotNull {
runCatching {
CircleMembership(
it["id"] as Long,
(it["id"] as Number).toLong(),
it["name"].toString(),
(it["title"] as List<*>).map { it.toString() })
(it["title"] as List<*>).map { it.toString() },
)
}.getOrNull()
}
}

private fun parseEntrants(): List<Entrant> {
val entrants = oidcUser.getClaim<List<Map<String, Any>>>("pek.sch.bme.hu:entrants/v1") ?: listOf()
return entrants.mapNotNull {
runCatching { Entrant(it["groupId"] as Long, it["groupName"].toString(), it["entrantType"].toString()) }.getOrNull()
runCatching {
Entrant(
(it["id"] as Number).toLong(),
it["groupName"].toString(),
it["entrantType"].toString(),
)
}.getOrNull()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class SchPincerOidcUserService(
val authschUser = super.loadUser(userRequest) ?: return null

val schPincerUser = SchPincerOidcUser(authschUser)
val ownedCircles = getOwnedCircleIds(schPincerUser.memberships, circleService)
val ownedCircles = getOwnedCircleIds(schPincerUser.executiveAtCircles, circleService)
if (userService.exists(schPincerUser.internalId)) {
val user = userService.getById(schPincerUser.internalId)
user.email = schPincerUser.email
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/hu/kirdev/schpincer/web/Utility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hu.kirdev.schpincer.web

import hu.kirdev.schpincer.config.Role
import hu.kirdev.schpincer.dto.CircleMemberRole
import hu.kirdev.schpincer.model.CircleMembership
import hu.kirdev.schpincer.model.ExecutiveAt
import hu.kirdev.schpincer.model.SchPincerOidcUser
import hu.kirdev.schpincer.service.CircleService
import hu.kirdev.schpincer.service.UserService
Expand Down Expand Up @@ -70,12 +70,10 @@ fun Authentication?.getUserIfPresent() = if (hasUser()) getUser() else null
fun Authentication?.getUserId() = (this?.principal as? SchPincerOidcUser)?.internalId

fun Authentication?.getOwnedCircles(circleService: CircleService) =
getOwnedCircleIds((this?.principal!! as SchPincerOidcUser).memberships, circleService)
getOwnedCircleIds((this?.principal!! as SchPincerOidcUser).executiveAtCircles, circleService)

fun getOwnedCircleIds(memberships: List<CircleMembership>, circleService: CircleService): List<Long> {
return memberships
.filter { it.title.any { it.lowercase().matches("^k[oö]rvezet[oöő]$".toRegex()) } }
.mapNotNull { circleService.findByVirGroupId(it.id)?.id }
fun getOwnedCircleIds(executiveAt: List<ExecutiveAt>, circleService: CircleService): List<Long> {
return executiveAt.mapNotNull { circleService.findByVirGroupId(it.id)?.id }
}


Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
/ __|/ __| || | | _ \_ _| \| |/ __| __| _ \
\__ \ (__| __ | [] | _/| || .` | (__| _|| /
|___/\___|_||_| |_| |___|_|\_|\___|___|_|_\
:: SCH-PINCÉR :: Startup completed
:: SCH-PINCÉR ::
Project lead: Szabo Gergely
Contributors: Albi, Beni, Berci, Isti, Schámi, Szabó Beni, Trisz
Github: https://github.com/kir-dev/sch-pincer
Profiles: ${spring.profiles.active}
Spring Boot: ${spring-boot.formatted-version}