@@ -22,7 +22,9 @@ package org.eclipse.apoapsis.ortserver.components.authorization.routes
2222import io.ktor.server.application.ApplicationCall
2323
2424import org.eclipse.apoapsis.ortserver.components.authorization.rights.EffectiveRole
25+ import org.eclipse.apoapsis.ortserver.components.authorization.rights.HierarchyPermissions
2526import org.eclipse.apoapsis.ortserver.components.authorization.rights.OrganizationPermission
27+ import org.eclipse.apoapsis.ortserver.components.authorization.rights.OrganizationRole
2628import org.eclipse.apoapsis.ortserver.components.authorization.rights.ProductPermission
2729import org.eclipse.apoapsis.ortserver.components.authorization.rights.RepositoryPermission
2830import org.eclipse.apoapsis.ortserver.components.authorization.service.AuthorizationService
@@ -51,15 +53,10 @@ interface AuthorizationChecker {
5153 * Use the provided [service] to load the [EffectiveRole] of the user with the given [userId] for the current
5254 * [call]. A typical implementation will figure out the ID of an element in the hierarchy (organization, product,
5355 * or repository) based on current call parameters. Then it can invoke the [service] to query the permissions on
54- * this element.
56+ * this element. If the permission check is successful, return a properly initialized [EffectiveRole] instance.
57+ * Otherwise, return *null*, which cause the request to fail with a 403 error.
5558 */
56- suspend fun loadEffectiveRole (service : AuthorizationService , userId : String , call : ApplicationCall ): EffectiveRole
57-
58- /* *
59- * Check whether the given [effectiveRole] contains the permission(s) required by this [AuthorizationChecker].
60- * This function is called with the [EffectiveRole] that was loaded via [loadEffectiveRole].
61- */
62- fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean
59+ suspend fun loadEffectiveRole (service : AuthorizationService , userId : String , call : ApplicationCall ): EffectiveRole ?
6360}
6461
6562/* * The name of the request parameter referring to the organization ID. */
@@ -80,11 +77,12 @@ fun requirePermission(permission: OrganizationPermission): AuthorizationChecker
8077 service : AuthorizationService ,
8178 userId : String ,
8279 call : ApplicationCall
83- ): EffectiveRole =
84- service.getEffectiveRole(userId, OrganizationId (call.requireIdParameter(ORGANIZATION_ID_PARAM )))
85-
86- override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
87- effectiveRole.hasOrganizationPermission(permission)
80+ ): EffectiveRole ? =
81+ service.checkPermissions(
82+ userId,
83+ OrganizationId (call.requireIdParameter(ORGANIZATION_ID_PARAM )),
84+ HierarchyPermissions .permissions(permission)
85+ )
8886
8987 override fun toString (): String = " RequireOrganizationPermission($permission )"
9088 }
@@ -98,11 +96,12 @@ fun requirePermission(permission: ProductPermission): AuthorizationChecker =
9896 service : AuthorizationService ,
9997 userId : String ,
10098 call : ApplicationCall
101- ): EffectiveRole =
102- service.getEffectiveRole(userId, ProductId (call.requireIdParameter(PRODUCT_ID_PARAM )))
103-
104- override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
105- effectiveRole.hasProductPermission(permission)
99+ ): EffectiveRole ? =
100+ service.checkPermissions(
101+ userId,
102+ ProductId (call.requireIdParameter(PRODUCT_ID_PARAM )),
103+ HierarchyPermissions .permissions(permission)
104+ )
106105
107106 override fun toString (): String = " RequireProductPermission($permission )"
108107 }
@@ -116,11 +115,12 @@ fun requirePermission(permission: RepositoryPermission): AuthorizationChecker =
116115 service : AuthorizationService ,
117116 userId : String ,
118117 call : ApplicationCall
119- ): EffectiveRole =
120- service.getEffectiveRole(userId, RepositoryId (call.requireIdParameter(REPOSITORY_ID_PARAM )))
121-
122- override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
123- effectiveRole.hasRepositoryPermission(permission)
118+ ): EffectiveRole ? =
119+ service.checkPermissions(
120+ userId,
121+ RepositoryId (call.requireIdParameter(REPOSITORY_ID_PARAM )),
122+ HierarchyPermissions .permissions(permission)
123+ )
124124
125125 override fun toString (): String = " RequireRepositoryPermission($permission )"
126126 }
@@ -134,11 +134,12 @@ fun requireSuperuser(): AuthorizationChecker =
134134 service : AuthorizationService ,
135135 userId : String ,
136136 call : ApplicationCall
137- ): EffectiveRole =
138- service.getEffectiveRole(userId, CompoundHierarchyId .WILDCARD )
139-
140- override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
141- effectiveRole.isSuperuser
137+ ): EffectiveRole ? =
138+ service.checkPermissions(
139+ userId,
140+ CompoundHierarchyId .WILDCARD ,
141+ HierarchyPermissions .permissions(OrganizationRole .ADMIN )
142+ )?.takeIf { it.isSuperuser }
142143
143144 override fun toString (): String = " RequireSuperuser"
144145 }
0 commit comments