Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Apr 10, 2024
1 parent f52c822 commit 3c62dc9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3c62dc9

Please sign in to comment.