|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +import { QueryClient } from '@tanstack/react-query'; |
| 21 | + |
| 22 | +import { |
| 23 | + getSuperuserOptions, |
| 24 | + getUserInfoOptions, |
| 25 | +} from '@/api/@tanstack/react-query.gen.ts'; |
| 26 | + |
| 27 | +class BasePermissions { |
| 28 | + constructor( |
| 29 | + public readonly isSuperuser: boolean, |
| 30 | + public readonly permissions: string[] |
| 31 | + ) {} |
| 32 | + |
| 33 | + includes(requiredPermission: string): boolean { |
| 34 | + return this.isSuperuser || this.permissions.includes(requiredPermission); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export class OrganizationPermissions extends BasePermissions { |
| 39 | + constructor( |
| 40 | + public readonly organizationId: number, |
| 41 | + isSuperuser: boolean, |
| 42 | + permissions: string[] |
| 43 | + ) { |
| 44 | + super(isSuperuser, permissions); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +export class ProductPermissions extends BasePermissions { |
| 49 | + constructor( |
| 50 | + public readonly productId: number, |
| 51 | + isSuperuser: boolean, |
| 52 | + permissions: string[] |
| 53 | + ) { |
| 54 | + super(isSuperuser, permissions); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export class RepositoryPermissions extends BasePermissions { |
| 59 | + constructor( |
| 60 | + public readonly repositoryId: number, |
| 61 | + isSuperuser: boolean, |
| 62 | + permissions: string[] |
| 63 | + ) { |
| 64 | + super(isSuperuser, permissions); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +type PermissionEntity = 'organizationId' | 'productId' | 'repositoryId'; |
| 69 | + |
| 70 | +async function fetchPermissions<T extends BasePermissions>( |
| 71 | + queryClient: QueryClient, |
| 72 | + entityId: number, |
| 73 | + entityType: PermissionEntity, |
| 74 | + PermissionsClass: new ( |
| 75 | + entityId: number, |
| 76 | + isSuperuser: boolean, |
| 77 | + permissions: string[] |
| 78 | + ) => T |
| 79 | +): Promise<T> { |
| 80 | + const isSuperuser = await queryClient.fetchQuery({ |
| 81 | + ...getSuperuserOptions(), |
| 82 | + staleTime: 60000, |
| 83 | + }); |
| 84 | + |
| 85 | + if (isSuperuser) { |
| 86 | + return new PermissionsClass(entityId, true, []); |
| 87 | + } |
| 88 | + |
| 89 | + const userInfo = await queryClient.fetchQuery({ |
| 90 | + ...getUserInfoOptions({ |
| 91 | + query: { |
| 92 | + [entityType]: entityId, |
| 93 | + }, |
| 94 | + }), |
| 95 | + staleTime: 60000, |
| 96 | + }); |
| 97 | + |
| 98 | + return new PermissionsClass(entityId, false, userInfo.permissions); |
| 99 | +} |
| 100 | + |
| 101 | +export const fetchOrganizationPermissions = ( |
| 102 | + queryClient: QueryClient, |
| 103 | + organizationId: number |
| 104 | +) => |
| 105 | + fetchPermissions( |
| 106 | + queryClient, |
| 107 | + organizationId, |
| 108 | + 'organizationId', |
| 109 | + OrganizationPermissions |
| 110 | + ); |
| 111 | + |
| 112 | +export const fetchProductPermissions = ( |
| 113 | + queryClient: QueryClient, |
| 114 | + productId: number |
| 115 | +) => fetchPermissions(queryClient, productId, 'productId', ProductPermissions); |
| 116 | + |
| 117 | +export const fetchRepositoryPermissions = ( |
| 118 | + queryClient: QueryClient, |
| 119 | + repositoryId: number |
| 120 | +) => |
| 121 | + fetchPermissions( |
| 122 | + queryClient, |
| 123 | + repositoryId, |
| 124 | + 'repositoryId', |
| 125 | + RepositoryPermissions |
| 126 | + ); |
0 commit comments