Skip to content

Commit

Permalink
Extract AdministrativePermission to admin slice
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Mar 28, 2024
1 parent 062b369 commit 965651f
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import dsp.errors.BadRequestException
import dsp.errors.ForbiddenException
import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.OntologyConstants.KnoraAdmin.AdministrativePermissionAbbreviations
import org.knora.webapi.responders.admin.PermissionsResponderADM
import org.knora.webapi.routing.UnsafeZioRun
import org.knora.webapi.sharedtestdata.SharedOntologyTestDataADM._
import org.knora.webapi.sharedtestdata.SharedTestDataADM2._
import org.knora.webapi.sharedtestdata._
import org.knora.webapi.slice.admin.api.service.PermissionsRestService
import org.knora.webapi.slice.admin.domain.model.AdministrativePermission
import org.knora.webapi.slice.admin.domain.model.AdministrativePermissions
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermission
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermissions
import org.knora.webapi.util.ZioScalaTestUtil.assertFailsWithA
Expand Down Expand Up @@ -74,7 +75,7 @@ class PermissionsMessagesADMSpec extends CoreSpec {
CreateAdministrativePermissionAPIRequestADM(
forProject = "invalid-project-IRI",
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectAdminAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectAdminAll)),
),
SharedTestDataADM.imagesUser01,
),
Expand All @@ -89,7 +90,7 @@ class PermissionsMessagesADMSpec extends CoreSpec {
CreateAdministrativePermissionAPIRequestADM(
forProject = SharedTestDataADM.imagesProjectIri,
forGroup = groupIri,
hasPermissions = Set(PermissionADM.ProjectAdminAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectAdminAll)),
),
SharedTestDataADM.imagesUser01,
),
Expand All @@ -105,7 +106,7 @@ class PermissionsMessagesADMSpec extends CoreSpec {
id = Some(permissionIri),
forProject = SharedTestDataADM.imagesProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectAdminAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectAdminAll)),
),
SharedTestDataADM.imagesUser01,
),
Expand Down Expand Up @@ -135,7 +136,7 @@ class PermissionsMessagesADMSpec extends CoreSpec {
assertFailsWithA[BadRequestException](
exit,
s"Invalid value for name parameter of hasPermissions: $invalidName, it should be one of " +
s"${AdministrativePermissionAbbreviations.toString}",
s"${AdministrativePermissions.allTokens.mkString(", ")}",
)
}

Expand All @@ -159,7 +160,7 @@ class PermissionsMessagesADMSpec extends CoreSpec {
CreateAdministrativePermissionAPIRequestADM(
forProject = SharedTestDataADM.imagesProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectAdminAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectAdminAll)),
),
SharedTestDataADM.imagesReviewerUser,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionT
import org.knora.webapi.messages.util.PermissionUtilADM
import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.sharedtestdata.SharedTestDataADM2
import org.knora.webapi.slice.admin.domain.model.AdministrativePermission
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermission

import pekko.testkit.ImplicitSender
Expand Down Expand Up @@ -133,10 +134,16 @@ class PermissionUtilADMSpec extends CoreSpec with ImplicitSender {
"ProjectResourceCreateAllPermission|ProjectAdminAllPermission|ProjectResourceCreateRestrictedPermission <http://www.knora.org/ontology/00FF/images#bild>,<http://www.knora.org/ontology/00FF/images#bildformat>"

val permissionsSet = Set(
PermissionADM.ProjectResourceCreateAllPermission,
PermissionADM.ProjectAdminAllPermission,
PermissionADM.projectResourceCreateRestrictedPermission("http://www.knora.org/ontology/00FF/images#bild"),
PermissionADM.projectResourceCreateRestrictedPermission("http://www.knora.org/ontology/00FF/images#bildformat"),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
PermissionADM.from(AdministrativePermission.ProjectAdminAll),
PermissionADM.from(
AdministrativePermission.ProjectResourceCreateRestricted,
"http://www.knora.org/ontology/00FF/images#bild",
),
PermissionADM.from(
AdministrativePermission.ProjectResourceCreateRestricted,
"http://www.knora.org/ontology/00FF/images#bildformat",
),
)

PermissionUtilADM.parsePermissionsWithType(
Expand All @@ -147,13 +154,13 @@ class PermissionUtilADMSpec extends CoreSpec with ImplicitSender {

"build a 'PermissionADM' object" in {
PermissionUtilADM.buildPermissionObject(
name = OntologyConstants.KnoraAdmin.ProjectResourceCreateRestrictedPermission,
name = AdministrativePermission.ProjectResourceCreateRestricted.token,
iris = Set("1", "2", "3"),
) should equal(
Set(
PermissionADM.projectResourceCreateRestrictedPermission("1"),
PermissionADM.projectResourceCreateRestrictedPermission("2"),
PermissionADM.projectResourceCreateRestrictedPermission("3"),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateRestricted, "1"),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateRestricted, "2"),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateRestricted, "3"),
),
)
}
Expand Down Expand Up @@ -181,25 +188,6 @@ class PermissionUtilADMSpec extends CoreSpec with ImplicitSender {
result should contain allElementsOf deduplicatedPermissions
}

"remove lesser permissions" in {
val withLesserPermissions = Set(
PermissionADM.from(ObjectAccessPermission.View, "1"),
PermissionADM.from(ObjectAccessPermission.View, "1"),
PermissionADM.from(ObjectAccessPermission.Modify, "2"),
PermissionADM.from(ObjectAccessPermission.ChangeRights, "1"),
PermissionADM.from(ObjectAccessPermission.Delete, "2"),
)

val withoutLesserPermissions = Set(
PermissionADM.from(ObjectAccessPermission.ChangeRights, "1"),
PermissionADM.from(ObjectAccessPermission.Delete, "2"),
)

val result = PermissionUtilADM.removeLesserPermissions(withLesserPermissions, PermissionType.OAP)
result.size should equal(withoutLesserPermissions.size)
result should contain allElementsOf withoutLesserPermissions
}

"create permissions string" in {
val permissions = Set(
PermissionADM.from(ObjectAccessPermission.ChangeRights, "1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.knora.webapi.sharedtestdata.SharedTestDataADM.imagesUser02
import org.knora.webapi.sharedtestdata.SharedTestDataADM.incunabulaMemberUser
import org.knora.webapi.sharedtestdata.SharedTestDataADM.normalUser
import org.knora.webapi.sharedtestdata.SharedTestDataADM2
import org.knora.webapi.slice.admin.domain.model.AdministrativePermission
import org.knora.webapi.slice.admin.domain.model.GroupIri
import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermission
Expand Down Expand Up @@ -226,7 +227,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
CreateAdministrativePermissionAPIRequestADM(
forProject = imagesProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectResourceCreateAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll)),
),
rootUser,
UUID.randomUUID(),
Expand All @@ -251,7 +252,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
id = Some(customIri),
forProject = SharedTestDataADM.anythingProjectIri,
forGroup = SharedTestDataADM.thingSearcherGroup.id,
hasPermissions = Set(PermissionADM.ProjectResourceCreateAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll)),
),
rootUser,
UUID.randomUUID(),
Expand All @@ -267,18 +268,12 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
val customIri = "http://rdfh.ch/permissions/0001/0pd-VUDeShWNJ2Nq3fGGGQ"
val hasPermissions = Set(
PermissionADM(
name = OntologyConstants.KnoraAdmin.ProjectResourceCreateAllPermission,
name = AdministrativePermission.ProjectResourceCreateAll.token,
additionalInformation = Some("blabla"),
permissionCode = Some(8),
),
)
val expectedHasPermissions = Set(
PermissionADM(
name = OntologyConstants.KnoraAdmin.ProjectResourceCreateAllPermission,
additionalInformation = None,
permissionCode = None,
),
)
val expectedHasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll))
val actual = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[PermissionsResponderADM](
_.createAdministrativePermission(
Expand Down Expand Up @@ -1001,7 +996,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
"ask to update hasPermissions of a permission" should {
"throw ForbiddenException for PermissionChangeHasPermissionsRequestADM if requesting user is not system or project Admin" in {
val permissionIri = "http://rdfh.ch/permissions/00FF/buxHAlz8SHuu0FuiLN_tKQ"
val hasPermissions = NonEmptyChunk(PermissionADM.ProjectResourceCreateAllPermission)
val hasPermissions = NonEmptyChunk(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll))

val exit = UnsafeZioRun.run(
ZIO.serviceWithZIO[PermissionsResponderADM](
Expand All @@ -1022,7 +1017,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {

"update hasPermissions of an administrative permission" in {
val permissionIri = "http://rdfh.ch/permissions/00FF/buxHAlz8SHuu0FuiLN_tKQ"
val hasPermissions = NonEmptyChunk(PermissionADM.ProjectResourceCreateAllPermission)
val hasPermissions = NonEmptyChunk(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll))
val actual = UnsafeZioRun.runOrThrow(
ZIO.serviceWithZIO[PermissionsResponderADM](
_.updatePermissionHasPermissions(
Expand All @@ -1044,7 +1039,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
val permissionIri = "http://rdfh.ch/permissions/00FF/buxHAlz8SHuu0FuiLN_tKQ"
val hasPermissions = NonEmptyChunk(
PermissionADM(
name = OntologyConstants.KnoraAdmin.ProjectAdminAllPermission,
name = AdministrativePermission.ProjectAdminAll.token,
additionalInformation = Some("aIRI"),
permissionCode = Some(1),
),
Expand All @@ -1062,7 +1057,7 @@ class PermissionsResponderADMSpec extends CoreSpec with ImplicitSender {
val ap = actual.asInstanceOf[AdministrativePermissionGetResponseADM].administrativePermission
assert(ap.iri == permissionIri)
ap.hasPermissions.size should be(1)
val expectedSetOfPermissions = Set(PermissionADM.ProjectAdminAllPermission)
val expectedSetOfPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectAdminAll))
assert(ap.hasPermissions.equals(expectedSetOfPermissions))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.knora.webapi.sharedtestdata.SharedTestDataADM
import org.knora.webapi.slice.admin.api.model.ProjectsEndpointsRequestsAndResponses.ProjectCreateRequest
import org.knora.webapi.slice.admin.api.model.ProjectsEndpointsRequestsAndResponses.ProjectUpdateRequest
import org.knora.webapi.slice.admin.api.service.ProjectRestService
import org.knora.webapi.slice.admin.domain.model.AdministrativePermission
import org.knora.webapi.slice.admin.domain.model.KnoraProject._
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermission
import org.knora.webapi.slice.admin.domain.model.RestrictedView
Expand Down Expand Up @@ -205,7 +206,10 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
(ap: AdministrativePermissionADM) =>
ap.forProject == received.project.id && ap.forGroup == OntologyConstants.KnoraAdmin.ProjectAdmin &&
ap.hasPermissions.equals(
Set(PermissionADM.ProjectAdminAllPermission, PermissionADM.ProjectResourceCreateAllPermission),
Set(
PermissionADM.from(AdministrativePermission.ProjectAdminAll),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
),
)
}

Expand All @@ -215,7 +219,7 @@ class ProjectRestServiceSpec extends CoreSpec with ImplicitSender {
val hasAPForProjectMember = receivedApAdmin.administrativePermissions.filter {
(ap: AdministrativePermissionADM) =>
ap.forProject == received.project.id && ap.forGroup == OntologyConstants.KnoraAdmin.ProjectMember &&
ap.hasPermissions.equals(Set(PermissionADM.ProjectResourceCreateAllPermission))
ap.hasPermissions.equals(Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll)))
}
hasAPForProjectMember.size shouldBe 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.knora.webapi.messages.admin.responder.permissionsmessages.ObjectAcces
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM
import org.knora.webapi.sharedtestdata.SharedOntologyTestDataADM._
import org.knora.webapi.sharedtestdata.SharedTestDataADM2._
import org.knora.webapi.slice.admin.domain.model.AdministrativePermission
import org.knora.webapi.slice.admin.domain.model.ObjectAccessPermission

/* Helper case classes */
Expand Down Expand Up @@ -92,7 +93,7 @@ object SharedPermissionsTestData {
forProject = imagesProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(
PermissionADM.ProjectResourceCreateAllPermission,
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
),
),
)
Expand All @@ -105,8 +106,8 @@ object SharedPermissionsTestData {
forProject = imagesProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectAdmin,
hasPermissions = Set(
PermissionADM.ProjectResourceCreateAllPermission,
PermissionADM.ProjectAdminAllPermission,
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
PermissionADM.from(AdministrativePermission.ProjectAdminAll),
),
),
)
Expand All @@ -119,8 +120,11 @@ object SharedPermissionsTestData {
forProject = imagesProjectIri,
forGroup = "http://rdfh.ch/groups/00FF/images-reviewer",
hasPermissions = Set(
PermissionADM.projectResourceCreateRestrictedPermission(s"$IMAGES_ONTOLOGY_IRI#bild"),
PermissionADM.projectResourceCreateRestrictedPermission(s"$IMAGES_ONTOLOGY_IRI#bildformat"),
PermissionADM.from(AdministrativePermission.ProjectResourceCreateRestricted, s"$IMAGES_ONTOLOGY_IRI#bild"),
PermissionADM.from(
AdministrativePermission.ProjectResourceCreateRestricted,
s"$IMAGES_ONTOLOGY_IRI#bildformat",
),
),
),
)
Expand Down Expand Up @@ -179,7 +183,7 @@ object SharedPermissionsTestData {
iri = "http://rdfh.ch/permissions/003-a1",
forProject = SharedTestDataADM2.incunabulaProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectResourceCreateAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll)),
),
)

Expand All @@ -191,8 +195,8 @@ object SharedPermissionsTestData {
forProject = SharedTestDataADM2.incunabulaProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectAdmin,
hasPermissions = Set(
PermissionADM.ProjectResourceCreateAllPermission,
PermissionADM.ProjectAdminAllPermission,
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
PermissionADM.from(AdministrativePermission.ProjectAdminAll),
),
),
)
Expand Down Expand Up @@ -314,7 +318,7 @@ object SharedPermissionsTestData {
iri = "http://rdfh.ch/permissions/00FF/XFozeICsTE2gHSOsm4ZMIw",
forProject = SharedTestDataADM2.anythingProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectMember,
hasPermissions = Set(PermissionADM.ProjectResourceCreateAllPermission),
hasPermissions = Set(PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll)),
),
)

Expand All @@ -326,8 +330,8 @@ object SharedPermissionsTestData {
forProject = SharedTestDataADM2.anythingProjectIri,
forGroup = OntologyConstants.KnoraAdmin.ProjectAdmin,
hasPermissions = Set(
PermissionADM.ProjectResourceCreateAllPermission,
PermissionADM.ProjectAdminAllPermission,
PermissionADM.from(AdministrativePermission.ProjectResourceCreateAll),
PermissionADM.from(AdministrativePermission.ProjectAdminAll),
),
),
)
Expand Down
Loading

0 comments on commit 965651f

Please sign in to comment.