Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 14, 2024
1 parent 6fc4e77 commit 3aff3da
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ final case class PermissionsResponder(
.mapBoth(_.getMessage, PermissionIri.unsafeFrom)

groupIri <- ZIO.foreach(req.forGroup)(checkGroupExists).mapError(_.getMessage)
resourceClassIri <- ZIO.foreach(req.forResourceClass)(checkResourceClassExists).mapError(_.getMessage)
propertyIri <- ZIO.foreach(req.forProperty)(checkPropertyExists).mapError(_.getMessage)
resourceClassIri <- ZIO.foreach(req.forResourceClass)(checkResourceClassIri).mapError(_.getMessage)
propertyIri <- ZIO.foreach(req.forProperty)(checkPropertyIri).mapError(_.getMessage)
forWhat <- ZIO.fromEither(ForWhat.fromIris(groupIri, resourceClassIri, propertyIri))
_ <- ZIO.fail("Permissions needs to be supplied.").when(req.hasPermissions.isEmpty)
doap <- ZIO.fromEither(DefaultObjectAccessPermission.from(permissionIri, projectIri, forWhat, req.hasPermissions))
Expand Down Expand Up @@ -697,8 +697,8 @@ final case class PermissionsResponder(
doap <-
doapService.findById(permissionIri).someOrFail(NotFoundException(s"DOAP ${permissionIri.value} not found."))
group <- ZIO.foreach(req.forGroup)(checkGroupExists)
resourceClass <- ZIO.foreach(req.forResourceClass)(checkResourceClassExists)
property <- ZIO.foreach(req.forProperty)(checkPropertyExists)
resourceClass <- ZIO.foreach(req.forResourceClass)(checkResourceClassIri)
property <- ZIO.foreach(req.forProperty)(checkPropertyIri)
newForWhat <- ZIO
.fromEither(ForWhat.fromIris(group, resourceClass, property))
.when(req.hasForWhat)
Expand All @@ -722,16 +722,14 @@ final case class PermissionsResponder(
_ <- groupService.findById(gIri).someOrFail(BadRequestException(s"Group ${groupIri} not found."))
} yield gIri

private def checkPropertyExists(propertyIri: IRI): Task[PropertyIri] = for {
private def checkPropertyIri(propertyIri: IRI): Task[PropertyIri] = for {
smartIri <- iriConverter.asSmartIri(propertyIri).mapError(BadRequestException.apply)
pIri <- ZIO.fromEither(PropertyIri.from(smartIri)).mapError(BadRequestException.apply)
_ <- ontologyRepo.findProperty(pIri).someOrFail(BadRequestException(s"Property ${pIri} not found."))
} yield pIri

private def checkResourceClassExists(resourceClassIri: IRI): Task[ResourceClassIri] = for {
private def checkResourceClassIri(resourceClassIri: IRI): Task[ResourceClassIri] = for {
smartIri <- iriConverter.asSmartIri(resourceClassIri).mapError(BadRequestException.apply)
rcIri <- ZIO.fromEither(ResourceClassIri.from(smartIri)).mapError(BadRequestException.apply)
_ <- ontologyRepo.findClassBy(rcIri).someOrFail(BadRequestException(s"Resource class ${rcIri} not found."))
} yield rcIri

private def asDefaultObjectAccessPermissionParts(
Expand Down Expand Up @@ -918,7 +916,7 @@ final case class PermissionsResponder(
val task: Task[DefaultObjectAccessPermissionGetResponseADM] =
for {
doap <- doapService.findById(permission).someOrFail(NotFoundException(s"DOAP ${permission.value} not found."))
resourceClass <- checkResourceClassExists(changeRequest.forResourceClass)
resourceClass <- checkResourceClassIri(changeRequest.forResourceClass)
newDoap <- doapService.save(doap.copy(forWhat = ResourceClass(resourceClass.toInternal)))
} yield DefaultObjectAccessPermissionGetResponseADM(doapService.asDefaultObjectAccessPermissionADM(newDoap))

Expand All @@ -933,7 +931,7 @@ final case class PermissionsResponder(
val task: Task[DefaultObjectAccessPermissionGetResponseADM] =
for {
doap <- doapService.findById(permission).someOrFail(NotFoundException(s"DOAP ${permission.value} not found."))
property <- checkPropertyExists(changeRequest.forProperty)
property <- checkPropertyIri(changeRequest.forProperty)
newDoap <- doapService.save(doap.copy(forWhat = Property(property.toInternal)))
} yield DefaultObjectAccessPermissionGetResponseADM(doapService.asDefaultObjectAccessPermissionADM(newDoap))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ package org.knora.webapi.slice.admin.domain.service
import zio.Chunk
import zio.IO
import zio.Task
import zio.ZIO
import zio.ZLayer

import dsp.errors.DuplicateValueException
import org.knora.webapi.messages.admin.responder.permissionsmessages.DefaultObjectAccessPermissionADM
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM
import org.knora.webapi.slice.admin.domain.model.DefaultObjectAccessPermission
Expand Down Expand Up @@ -38,18 +36,7 @@ final case class DefaultObjectAccessPermissionService(
): Task[DefaultObjectAccessPermission] =
repo.save(DefaultObjectAccessPermission(PermissionIri.makeNew(project.shortcode), project.id, forWhat, permission))

def save(
doap: DefaultObjectAccessPermission,
): IO[DuplicateValueException, DefaultObjectAccessPermission] =
repo.findByProjectAndForWhat(doap.forProject, doap.forWhat).orDie.flatMap {
case Some(doapExisting) if doapExisting.id != doap.id =>
ZIO.fail(
DuplicateValueException(
s"Permission already exists for project ${doapExisting.forProject.value} and ${doapExisting.forWhat}",
),
)
case _ => repo.save(doap).orDie
}
def save(doap: DefaultObjectAccessPermission): Task[DefaultObjectAccessPermission] = repo.save(doap)

def findByProjectAndForWhat(projectIri: ProjectIri, forWhat: ForWhat): Task[Option[DefaultObjectAccessPermission]] =
repo.findByProjectAndForWhat(projectIri, forWhat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ trait OntologyRepo extends Repository[ReadOntologyV2, InternalIri] {

def findClassBy(classIri: InternalIri): Task[Option[ReadClassInfoV2]]

final def findClassBy(resourceClassIri: ResourceClassIri): Task[Option[ReadClassInfoV2]] =
findClassBy(resourceClassIri.toInternal)

def findDirectSuperClassesBy(classIri: InternalIri): Task[List[ReadClassInfoV2]]

def findAllSuperClassesBy(classIri: InternalIri): Task[List[ReadClassInfoV2]]
Expand Down

0 comments on commit 3aff3da

Please sign in to comment.