From 3c62dc9d17a461ca10aaabe9b6cb508fec3b5fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kleinb=C3=B6lting?= Date: Wed, 10 Apr 2024 09:42:30 +0200 Subject: [PATCH] cleanup --- .../slice/infrastructure/JwtService.scala | 56 +++++++++---------- .../webapi/slice/infrastructure/Scope.scala | 4 +- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/JwtService.scala b/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/JwtService.scala index ecba2949e2..85af74cc63 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/JwtService.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/JwtService.scala @@ -6,21 +6,6 @@ package org.knora.webapi.routing import com.typesafe.scalalogging.Logger -import dsp.valueobjects.Iri -import dsp.valueobjects.UuidUtil -import org.knora.webapi.IRI -import org.knora.webapi.config.DspIngestConfig -import org.knora.webapi.config.JwtConfig -import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM -import org.knora.webapi.routing.Authenticator.AUTHENTICATION_INVALIDATION_CACHE_NAME -import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri -import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode -import org.knora.webapi.slice.admin.domain.model.Permission.Administrative -import org.knora.webapi.slice.admin.domain.model.User -import org.knora.webapi.slice.admin.domain.service.KnoraProjectService -import org.knora.webapi.slice.infrastructure.Scope -import org.knora.webapi.slice.infrastructure.ScopeValue -import org.knora.webapi.util.cache.CacheUtil import org.slf4j.LoggerFactory import pdi.jwt.JwtAlgorithm import pdi.jwt.JwtClaim @@ -40,6 +25,22 @@ import zio.durationInt import scala.util.Failure import scala.util.Success +import dsp.valueobjects.Iri +import dsp.valueobjects.UuidUtil +import org.knora.webapi.IRI +import org.knora.webapi.config.DspIngestConfig +import org.knora.webapi.config.JwtConfig +import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM +import org.knora.webapi.routing.Authenticator.AUTHENTICATION_INVALIDATION_CACHE_NAME +import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri +import org.knora.webapi.slice.admin.domain.model.KnoraProject.Shortcode +import org.knora.webapi.slice.admin.domain.model.Permission.Administrative +import org.knora.webapi.slice.admin.domain.model.User +import org.knora.webapi.slice.admin.domain.service.KnoraProjectService +import org.knora.webapi.slice.infrastructure.Scope +import org.knora.webapi.slice.infrastructure.ScopeValue +import org.knora.webapi.util.cache.CacheUtil + case class Jwt(jwtString: String, expiration: Long) /** @@ -96,19 +97,18 @@ final case class JwtServiceLive( } private def calculateScope(user: User) = - if (user.isSystemAdmin || user.isSystemUser) { - ZIO.succeed(Scope.admin) - } else { - ZIO - .foreach(user.permissions.administrativePermissionsPerProject.toSeq) { case (iriStr, permission) => - knoraProjectService - .findById(ProjectIri.unsafeFrom(iriStr)) - .orDie - .map(_.map(prj => mapPermissionToScope(permission, prj.shortcode)).getOrElse(Seq.empty)) - } - .map(_.flatten) - .map(_.foldLeft(Scope.empty)(_ + _)) - } + if (user.isSystemAdmin || user.isSystemUser) { ZIO.succeed(Scope.admin) } + else { mapUserPermissionsToScope(user) } + + private def mapUserPermissionsToScope(user: User): UIO[Scope] = + ZIO + .foreach(user.permissions.administrativePermissionsPerProject.toSeq) { case (iriStr, permission) => + knoraProjectService + .findById(ProjectIri.unsafeFrom(iriStr)) + .orDie + .map(_.map(prj => mapPermissionToScope(permission, prj.shortcode)).getOrElse(Seq.empty)) + } + .map(scopeValues => Scope.from(scopeValues.flatten)) private def mapPermissionToScope(permission: Set[PermissionADM], shortcode: Shortcode): Seq[ScopeValue.Write] = permission.toSeq diff --git a/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/Scope.scala b/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/Scope.scala index 6abc7f5cd3..9fca30d725 100644 --- a/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/Scope.scala +++ b/webapi/src/main/scala/org/knora/webapi/slice/infrastructure/Scope.scala @@ -26,8 +26,8 @@ object Scope { val empty: Scope = Scope(Set.empty) val admin: Scope = Scope(Set(ScopeValue.Admin)) - def from(scopeValue: ScopeValue) = Scope(Set(scopeValue)) - def from(scopeValues: Seq[ScopeValue]) = scopeValues.foldLeft(Scope.empty)(_ + _) + def from(scopeValue: ScopeValue): Scope = Scope(Set(scopeValue)) + def from(scopeValues: Seq[ScopeValue]): Scope = scopeValues.foldLeft(Scope.empty)(_ + _) } sealed trait ScopeValue {