Skip to content

Commit

Permalink
Merge branch 'main' into update/minor
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone authored Mar 1, 2024
2 parents b018cbb + 320a4a8 commit 557e604
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ object LayersTest {
KnoraResponseRenderer.layer,
KnoraUserGroupRepoLive.layer,
KnoraUserRepoLive.layer,
KnoraUserToUserConverter.layer,
ListRestService.layer,
ListsEndpoints.layer,
ListsEndpointsHandlers.layer,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ object LayersLive {
KnoraProjectRepoLive.layer,
KnoraResponseRenderer.layer,
KnoraUserRepoLive.layer,
KnoraUserToUserConverter.layer,
KnoraUserGroupRepoLive.layer,
ListRestService.layer,
ListsEndpoints.layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package org.knora.webapi.messages.admin.responder.usersmessages
import org.apache.pekko.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.*

import java.util.UUID

import org.knora.webapi.core.RelayedMessage
import org.knora.webapi.messages.ResponderRequest.KnoraRequestADM
import org.knora.webapi.messages.admin.responder.AdminKnoraResponseADM
Expand Down Expand Up @@ -44,13 +42,10 @@ case class UserGetByIriADM(
/**
* Requests removing the user from a group.
*
* @param userIri the IRI of the user to be updated.
* @param groupIri the IRI of the group.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
* @param user the user to be updated.
* @param group the group.
*/
case class UserGroupMembershipRemoveRequestADM(userIri: UserIri, groupIri: GroupIri, apiRequestID: UUID)
extends UsersResponderRequestADM
case class UserGroupMembershipRemoveRequestADM(user: User, group: GroupADM) extends UsersResponderRequestADM

// Responses

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import zio.*
import dsp.errors.ForbiddenException
import dsp.errors.NotFoundException
import org.knora.webapi.IRI
import org.knora.webapi.messages.admin.responder.usersmessages.UserInformationType.Full
import org.knora.webapi.messages.util.KnoraSystemInstances.Users.SystemUser
import org.knora.webapi.responders.admin.UsersResponder
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.model.UserIri
import org.knora.webapi.slice.admin.domain.service.UserService

/**
* Utility functions for working with users.
Expand All @@ -36,7 +34,7 @@ object UserUtilADM {
requestingUser: User,
requestedUserIri: IRI,
projectIri: IRI
): ZIO[UsersResponder, Throwable, User] = {
): ZIO[UserService, Throwable, User] = {
val userIri = UserIri.unsafeFrom(requestedUserIri)
requestingUser match {
case _ if requestingUser.id == userIri.value => ZIO.succeed(requestingUser)
Expand All @@ -45,9 +43,9 @@ object UserUtilADM {
s"You are logged in as ${requestingUser.username}, but only a system administrator or project administrator can perform an operation as another user"
ZIO.fail(ForbiddenException(msg))
case _ =>
UsersResponder
.findUserByIri(userIri, Full, SystemUser)
.someOrFail(NotFoundException(s"User '${userIri.value}' not found"))
ZIO.serviceWithZIO[UserService](
_.findUserByIri(userIri).someOrFail(NotFoundException(s"User '${userIri.value}' not found"))
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import org.knora.webapi.messages.v2.responder.resourcemessages.CreateResourceReq
import org.knora.webapi.messages.v2.responder.resourcemessages.CreateResourceRequestV2.AssetIngestState.AssetInTemp
import org.knora.webapi.messages.v2.responder.standoffmessages.MappingXMLtoStandoff
import org.knora.webapi.messages.v2.responder.valuemessages.*
import org.knora.webapi.responders.admin.UsersResponder
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.admin.domain.service.UserService
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.util.*
Expand Down Expand Up @@ -685,7 +685,7 @@ object CreateResourceRequestV2 {
requestingUser: User,
ingestState: AssetIngestState = AssetInTemp
): ZIO[
IriConverter & MessageRelay & SipiService & StringFormatter & UsersResponder,
IriConverter & MessageRelay & SipiService & StringFormatter & UserService,
Throwable,
CreateResourceRequestV2
] =
Expand Down
16 changes: 14 additions & 2 deletions webapi/src/main/scala/org/knora/webapi/responders/IriService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.StringFormatter.MAX_IRI_ATTEMPTS
import org.knora.webapi.messages.twirl.queries.sparql
import org.knora.webapi.slice.admin.domain.model.UserIri
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Ask
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Select
Expand All @@ -29,6 +31,7 @@ import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Select
* will be subject to further refactoring once we extract more services.
*/
final case class IriService(
private val iriConverter: IriConverter,
private val triplestore: TriplestoreService,
private val stringFormatter: StringFormatter
) extends LazyLogging {
Expand Down Expand Up @@ -58,6 +61,16 @@ final case class IriService(
def isClassUsedInData(classIri: SmartIri): Task[Boolean] =
triplestore.query(Select(sparql.v2.txt.isClassUsedInData(classIri))).map(_.results.bindings.nonEmpty)

def checkOrCreateNewUserIri(entityIri: Option[UserIri]): Task[UserIri] =
for {
// check the custom IRI; if not given, create an unused IRI
customUserIri <- ZIO.foreach(entityIri.map(_.value))(iriConverter.asSmartIri)
userIriStr <- checkOrCreateEntityIri(customUserIri, UserIri.makeNew.value)
userIri <- ZIO
.fromEither(UserIri.from(userIriStr))
.orElseFail(BadRequestException(s"Invalid User IRI: $userIriStr"))
} yield userIri

/**
* Checks whether an entity with the provided custom IRI exists in the triplestore. If yes, throws an exception.
* If no custom IRI was given, creates a random unused IRI.
Expand Down Expand Up @@ -108,6 +121,5 @@ final case class IriService(
}

object IriService {
val layer: ZLayer[TriplestoreService & StringFormatter, Nothing, IriService] =
ZLayer.fromFunction(IriService.apply _)
val layer = ZLayer.derive[IriService]
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,7 @@ final case class GroupsResponderADMLive(

// remove all members from group if status is false
operationResponse <-
removeGroupMembersIfNecessary(
changedGroup = updateGroupResult.group,
apiRequestID = apiRequestID
)
removeGroupMembersIfNecessary(changedGroup = updateGroupResult.group)

} yield operationResponse

Expand Down Expand Up @@ -524,13 +521,9 @@ final case class GroupsResponderADMLive(
* group members need to be removed from the group.
*
* @param changedGroup the group with the new status.
* @param apiRequestID the unique request ID.
* @return a [[GroupGetResponseADM]]
*/
private def removeGroupMembersIfNecessary(
changedGroup: GroupADM,
apiRequestID: UUID
): Task[GroupGetResponseADM] =
private def removeGroupMembersIfNecessary(changedGroup: GroupADM) =
if (changedGroup.status) {
// group active. no need to remove members.
logger.debug("removeGroupMembersIfNecessary - group active. no need to remove members.")
Expand All @@ -539,22 +532,10 @@ final case class GroupsResponderADMLive(
// group deactivated. need to remove members.
logger.debug("removeGroupMembersIfNecessary - group deactivated. need to remove members.")
for {
members <-
groupMembersGetADM(
groupIri = changedGroup.id,
requestingUser = KnoraSystemInstances.Users.SystemUser
)

seqOfFutures: Seq[Task[UserResponseADM]] =
members.map { (user: User) =>
messageRelay
.ask[UserResponseADM](
UserGroupMembershipRemoveRequestADM(user.userIri, changedGroup.groupIri, apiRequestID)
)
}

_ <- ZioHelper.sequence(seqOfFutures)

members <- groupMembersGetADM(changedGroup.id, KnoraSystemInstances.Users.SystemUser)
_ <- ZIO.foreachDiscard(members)(user =>
messageRelay.ask[UserResponseADM](UserGroupMembershipRemoveRequestADM(user, changedGroup))
)
} yield GroupGetResponseADM(group = changedGroup)
}
}
Expand Down
Loading

0 comments on commit 557e604

Please sign in to comment.