( organizationIds: string[] | undefined, )
| 325 | * If organizations are undefined, return a disabled query. |
| 326 | */ |
| 327 | export const organizationsPermissions = ( |
| 328 | organizationIds: string[] | undefined, |
| 329 | ) => { |
| 330 | return { |
| 331 | enabled: Boolean(organizationIds), |
| 332 | queryKey: [ |
| 333 | "organizations", |
| 334 | [...(organizationIds ?? []).sort()], |
| 335 | "permissions", |
| 336 | ], |
| 337 | queryFn: async () => { |
| 338 | // Only request what we need for the sidebar, which is one edit permission |
| 339 | // per sub-link (settings, groups, roles, and members pages) that tells us |
| 340 | // whether to show that page, since we only show them if you can edit (and |
| 341 | // not, at the moment if you can only view). |
| 342 | |
| 343 | // The endpoint takes a flat array, so to avoid collisions prepend each |
| 344 | // check with the org ID (the key can be anything we want). |
| 345 | const prefixedChecks = (organizationIds ?? []).flatMap((orgId) => |
| 346 | Object.entries(organizationPermissionChecks(orgId)).map( |
| 347 | ([key, val]) => [`${orgId}.${key}`, val], |
| 348 | ), |
| 349 | ); |
| 350 | |
| 351 | const response = await API.checkAuthorization({ |
| 352 | checks: Object.fromEntries(prefixedChecks), |
| 353 | }); |
| 354 | |
| 355 | // Now we can unflatten by parsing out the org ID from each check. |
| 356 | return Object.entries(response).reduce( |
| 357 | (acc, [key, value]) => { |
| 358 | const index = key.indexOf("."); |
| 359 | const orgId = key.substring(0, index); |
| 360 | const perm = key.substring(index + 1); |
| 361 | if (!acc[orgId]) { |
| 362 | acc[orgId] = {}; |
| 363 | } |
| 364 | acc[orgId][perm as OrganizationPermissionName] = value; |
| 365 | return acc; |
| 366 | }, |
| 367 | {} as Record<string, Partial<OrganizationPermissions>>, |
| 368 | ) as Record<string, OrganizationPermissions>; |
| 369 | }, |
| 370 | }; |
| 371 | }; |
| 372 | |
| 373 | export const workspacePermissionsByOrganization = ( |
| 374 | organizationIds: string[] | undefined, |
no test coverage detected