Skip to content

Commit

Permalink
refactor: Remove user auth from ontology endpoints ignoring the user …
Browse files Browse the repository at this point in the history
…anyway (DEV-4292) (#3408)
  • Loading branch information
seakayone authored Oct 30, 2024
1 parent af0388e commit 2071909
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

// Request the metadata of all ontologies to check that 'foo' isn't listed.

appActor ! OntologyMetadataGetByProjectRequestV2(
requestingUser = imagesUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2()

val cachedMetadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(!cachedMetadataResponse.ontologies.exists(_.ontologyIri == fooIri.get.toSmartIri))
Expand All @@ -458,19 +456,14 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {
ZIO.serviceWithZIO[OntologyCache](_.loadOntologies(KnoraSystemInstances.Users.SystemUser)),
)

appActor ! OntologyMetadataGetByProjectRequestV2(
requestingUser = imagesUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2()

val loadedMetadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(!loadedMetadataResponse.ontologies.exists(_.ontologyIri == fooIri.get.toSmartIri))
}

"not delete the 'anything' ontology, because it is used in data and in the 'something' ontology" in {
appActor ! OntologyMetadataGetByProjectRequestV2(
projectIris = Set(anythingProjectIri),
requestingUser = anythingAdminUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2(projectIris = Set(anythingProjectIri))

val metadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(metadataResponse.ontologies.size == 3)
Expand Down Expand Up @@ -676,10 +669,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

"not allow a user to create a property if they are not a sysadmin or an admin in the ontology's project" in {

appActor ! OntologyMetadataGetByProjectRequestV2(
projectIris = Set(anythingProjectIri),
requestingUser = anythingNonAdminUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2(projectIris = Set(anythingProjectIri))

val metadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(metadataResponse.ontologies.size == 3)
Expand Down Expand Up @@ -738,10 +728,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

"create a property anything:hasName as a subproperty of knora-api:hasValue and schema:name" in {

appActor ! OntologyMetadataGetByProjectRequestV2(
projectIris = Set(anythingProjectIri),
requestingUser = anythingAdminUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2(projectIris = Set(anythingProjectIri))

val metadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(metadataResponse.ontologies.size == 3)
Expand Down Expand Up @@ -827,10 +814,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {

"create a link property in the 'anything' ontology, and automatically create the corresponding link value property" in {

appActor ! OntologyMetadataGetByProjectRequestV2(
projectIris = Set(anythingProjectIri),
requestingUser = anythingAdminUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2(projectIris = Set(anythingProjectIri))

val metadataResponse = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(metadataResponse.ontologies.size == 3)
Expand Down Expand Up @@ -6209,10 +6193,7 @@ class OntologyResponderV2Spec extends CoreSpec with ImplicitSender {
val classIri: SmartIri = AnythingOntologyIri.makeEntityIri("FoafPerson")

// create the property anything:hasFoafName
appActor ! OntologyMetadataGetByProjectRequestV2(
projectIris = Set(anythingProjectIri),
requestingUser = anythingAdminUser,
)
appActor ! OntologyMetadataGetByProjectRequestV2(projectIris = Set(anythingProjectIri))

val metadataResponse: ReadOntologyMetadataV2 = expectMsgType[ReadOntologyMetadataV2](timeout)
assert(metadataResponse.ontologies.size == 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AddCardinalitiesToClassSpec extends CoreSpec {
val newPropertyIri = "http://www.knora.org/ontology/0001/freetest#hasName".toSmartIri

// check ontology first
appActor ! OntologyMetadataGetByIriRequestV2(ontologyIris = Set(freetestOntologyIri), requestingUser = user)
appActor ! OntologyMetadataGetByIriRequestV2(ontologyIris = Set(freetestOntologyIri))
val ontologyMetadata = expectMsgType[ReadOntologyMetadataV2]
val ontologyLastModificationDate = ontologyMetadata.ontologies.head.lastModificationDate
.getOrElse(throw new AssertionError("Ontology does not have a LastModificationDate"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ import org.knora.webapi.slice.ontology.domain.model.Cardinality
/**
* An abstract trait for messages that can be sent to `ResourcesResponderV2`.
*/
sealed trait OntologiesResponderRequestV2 extends KnoraRequestV2 with RelayedMessage {

def requestingUser: User
}
sealed trait OntologiesResponderRequestV2 extends KnoraRequestV2 with RelayedMessage

/**
* Requests the creation of an empty ontology. A successful response will be a [[ReadOntologyV2]].
Expand Down Expand Up @@ -1211,36 +1208,22 @@ case class SubClassesGetRequestV2(resourceClassIri: SmartIri, requestingUser: Us
*/
case class SubClassesGetResponseV2(subClasses: Seq[SubClassInfoV2])

/**
* Request information about the Knora entities (Knora resource classes, standoff class, resource properties, and standoff properties) of a named graph.
* A successful response will be a [[OntologyKnoraEntitiesIriInfoV2]].
*
* @param ontologyIri the IRI of the named graph.
* @param requestingUser the user making the request.
*/
case class OntologyKnoraEntityIrisGetRequestV2(ontologyIri: SmartIri, requestingUser: User)
extends OntologiesResponderRequestV2

/**
* Requests metadata about ontologies by project.
*
* @param projectIris the IRIs of the projects for which ontologies should be returned. If this set is empty, information
* about all ontologies is returned.
* @param requestingUser the user making the request.
*/
case class OntologyMetadataGetByProjectRequestV2(
projectIris: Set[SmartIri] = Set.empty[SmartIri],
requestingUser: User,
) extends OntologiesResponderRequestV2
case class OntologyMetadataGetByProjectRequestV2(projectIris: Set[SmartIri] = Set.empty[SmartIri])
extends OntologiesResponderRequestV2

/**
* Requests metadata about ontologies by ontology IRI.
*
* @param ontologyIris the IRIs of the ontologies to be queried. If this set is empty, information
* about all ontologies is returned.
* @param requestingUser the user making the request.
*/
case class OntologyMetadataGetByIriRequestV2(ontologyIris: Set[SmartIri] = Set.empty[SmartIri], requestingUser: User)
case class OntologyMetadataGetByIriRequestV2(ontologyIris: Set[SmartIri] = Set.empty[SmartIri])
extends OntologiesResponderRequestV2

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,17 @@ final case class OntologyResponderV2(
case StandoffAllPropertyEntitiesGetRequestV2(_) => getAllStandoffPropertyEntitiesV2
case CheckSubClassRequestV2(subClassIri, superClassIri, _) =>
checkSubClassV2(subClassIri, superClassIri)
case SubClassesGetRequestV2(resourceClassIri, requestingUser) => getSubClassesV2(resourceClassIri, requestingUser)
case OntologyKnoraEntityIrisGetRequestV2(namedGraphIri, _) =>
getKnoraEntityIrisInNamedGraphV2(namedGraphIri)
case SubClassesGetRequestV2(resourceClassIri, requestingUser) =>
getSubClassesV2(resourceClassIri, requestingUser)
case OntologyEntitiesGetRequestV2(ontologyIri, allLanguages, requestingUser) =>
getOntologyEntitiesV2(ontologyIri, allLanguages, requestingUser)
case ClassesGetRequestV2(resourceClassIris, allLanguages, requestingUser) =>
ontologyCacheHelpers.getClassDefinitionsFromOntologyV2(resourceClassIris, allLanguages, requestingUser)
case PropertiesGetRequestV2(propertyIris, allLanguages, requestingUser) =>
getPropertyDefinitionsFromOntologyV2(propertyIris, allLanguages, requestingUser)
case OntologyMetadataGetByProjectRequestV2(projectIris, _) =>
case OntologyMetadataGetByProjectRequestV2(projectIris) =>
getOntologyMetadataForProjectsV2(projectIris)
case OntologyMetadataGetByIriRequestV2(ontologyIris, _) =>
case OntologyMetadataGetByIriRequestV2(ontologyIris) =>
getOntologyMetadataByIriV2(ontologyIris)
case createOntologyRequest: CreateOntologyRequestV2 => createOntology(createOntologyRequest)
case changeOntologyMetadataRequest: ChangeOntologyMetadataRequestV2 =>
Expand Down Expand Up @@ -296,30 +295,6 @@ final case class OntologyResponderV2(
}
} yield SubClassesGetResponseV2(subClasses)

/**
* Gets the [[OntologyKnoraEntitiesIriInfoV2]] for an ontology.
*
* @param ontologyIri the IRI of the ontology to query
* @return an [[OntologyKnoraEntitiesIriInfoV2]].
*/
private def getKnoraEntityIrisInNamedGraphV2(ontologyIri: SmartIri): Task[OntologyKnoraEntitiesIriInfoV2] =
for {
cacheData <- ontologyCache.getCacheData
ontology = cacheData.ontologies(ontologyIri)
} yield OntologyKnoraEntitiesIriInfoV2(
ontologyIri = ontologyIri,
propertyIris = ontology.properties.keySet.filter { propertyIri =>
OntologyHelpers.isKnoraResourceProperty(propertyIri, cacheData)
},
classIris = ontology.classes.filter { case (_, classDef) =>
classDef.isResourceClass
}.keySet,
standoffClassIris = ontology.classes.filter { case (_, classDef) =>
classDef.isStandoffClass
}.keySet,
standoffPropertyIris = ontology.properties.keySet.filter(cacheData.standoffProperties),
)

/**
* Gets the metadata describing the ontologies that belong to selected projects, or to all projects.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ final case class OntologiesRouteV2()(
private def getOntologyMetadata(): Route =
path(ontologiesBasePath / "metadata") {
get { requestContext =>
val requestTask = for {
maybeProjectIri <- RouteUtilV2.getProjectIri(requestContext)
requestingUser <- ZIO.serviceWithZIO[Authenticator](_.getUserADM(requestContext))
} yield OntologyMetadataGetByProjectRequestV2(maybeProjectIri.toSet, requestingUser)
val requestTask = RouteUtilV2
.getProjectIri(requestContext)
.map(_.toSet)
.map(OntologyMetadataGetByProjectRequestV2(_))
RouteUtilV2.runRdfRouteZ(requestTask, requestContext)
}
}
Expand All @@ -166,12 +166,10 @@ final case class OntologiesRouteV2()(
private def getOntologyMetadataForProjects(): Route =
path(ontologiesBasePath / "metadata" / Segments) { (projectIris: List[IRI]) =>
get { requestContext =>
val requestTask = for {
requestingUser <- ZIO.serviceWithZIO[Authenticator](_.getUserADM(requestContext))
validatedProjectIris <- ZIO.foreach(projectIris) { iri =>
RouteUtilZ.toSmartIri(iri, s"Invalid project IRI: $iri")
}
} yield OntologyMetadataGetByProjectRequestV2(validatedProjectIris.toSet, requestingUser)
val requestTask = ZIO
.foreach(projectIris)(iri => RouteUtilZ.toSmartIri(iri, s"Invalid project IRI: $iri"))
.map(_.toSet)
.map(OntologyMetadataGetByProjectRequestV2(_))
RouteUtilV2.runRdfRouteZ(requestTask, requestContext)
}
}
Expand Down

0 comments on commit 2071909

Please sign in to comment.