@@ -23,8 +23,13 @@ import io.ktor.server.application.ApplicationCall
2323
2424import org.eclipse.apoapsis.ortserver.components.authorization.rights.EffectiveRole
2525import org.eclipse.apoapsis.ortserver.components.authorization.rights.OrganizationPermission
26+ import org.eclipse.apoapsis.ortserver.components.authorization.rights.ProductPermission
27+ import org.eclipse.apoapsis.ortserver.components.authorization.rights.RepositoryPermission
2628import org.eclipse.apoapsis.ortserver.components.authorization.service.AuthorizationService
29+ import org.eclipse.apoapsis.ortserver.model.CompoundHierarchyId
2730import org.eclipse.apoapsis.ortserver.model.OrganizationId
31+ import org.eclipse.apoapsis.ortserver.model.ProductId
32+ import org.eclipse.apoapsis.ortserver.model.RepositoryId
2833import org.eclipse.apoapsis.ortserver.shared.ktorutils.requireIdParameter
2934
3035/* *
@@ -57,6 +62,15 @@ interface AuthorizationChecker {
5762 fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean
5863}
5964
65+ /* * The name of the request parameter referring to the organization ID. */
66+ private const val ORGANIZATION_ID_PARAM = " organizationId"
67+
68+ /* * The name of the request parameter referring to the product ID. */
69+ private const val PRODUCT_ID_PARAM = " productId"
70+
71+ /* * The name of the request parameter referring to the repository ID. */
72+ private const val REPOSITORY_ID_PARAM = " repositoryId"
73+
6074/* *
6175 * Create an [AuthorizationChecker] that checks for the presence of the given organization-level [permission].
6276 */
@@ -67,10 +81,64 @@ fun requirePermission(permission: OrganizationPermission): AuthorizationChecker
6781 userId : String ,
6882 call : ApplicationCall
6983 ): EffectiveRole =
70- service.getEffectiveRole(userId, OrganizationId (call.requireIdParameter(" organizationId " )))
84+ service.getEffectiveRole(userId, OrganizationId (call.requireIdParameter(ORGANIZATION_ID_PARAM )))
7185
7286 override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
7387 effectiveRole.hasOrganizationPermission(permission)
7488
7589 override fun toString (): String = " RequireOrganizationPermission($permission )"
7690 }
91+
92+ /* *
93+ * Create an [AuthorizationChecker] that checks for the presence of the given product-level [permission].
94+ */
95+ fun requirePermission (permission : ProductPermission ): AuthorizationChecker =
96+ object : AuthorizationChecker {
97+ override suspend fun loadEffectiveRole (
98+ service : AuthorizationService ,
99+ userId : String ,
100+ 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)
106+
107+ override fun toString (): String = " RequireProductPermission($permission )"
108+ }
109+
110+ /* *
111+ * Create an [AuthorizationChecker] that checks for the presence of the given repository-level [permission].
112+ */
113+ fun requirePermission (permission : RepositoryPermission ): AuthorizationChecker =
114+ object : AuthorizationChecker {
115+ override suspend fun loadEffectiveRole (
116+ service : AuthorizationService ,
117+ userId : String ,
118+ 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)
124+
125+ override fun toString (): String = " RequireRepositoryPermission($permission )"
126+ }
127+
128+ /* *
129+ * Create an [AuthorizationChecker] that checks whether the user is a superuser.
130+ */
131+ fun requireSuperuser (): AuthorizationChecker =
132+ object : AuthorizationChecker {
133+ override suspend fun loadEffectiveRole (
134+ service : AuthorizationService ,
135+ userId : String ,
136+ call : ApplicationCall
137+ ): EffectiveRole =
138+ service.getEffectiveRole(userId, CompoundHierarchyId .WILDCARD )
139+
140+ override fun checkAuthorization (effectiveRole : EffectiveRole ): Boolean =
141+ effectiveRole.isSuperuser
142+
143+ override fun toString (): String = " RequireSuperuser"
144+ }
0 commit comments