|
| 1 | +# How to: Query Permissions from openedx-authz |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +`@openedx/frontend-base` provides hooks and utilities to validate user permissions against the |
| 6 | +`openedx-authz` service. Results are cached automatically via TanStack Query to minimize calls |
| 7 | +to the backend. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Ensure your app root is wrapped with a `QueryClientProvider` from `@tanstack/react-query`. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Core Concepts |
| 16 | + |
| 17 | +### Permission query shape |
| 18 | + |
| 19 | +Permissions are expressed as a key/value map where: |
| 20 | +- **keys** are arbitrary semantic names you choose (e.g. `canEditGrading`) |
| 21 | +- **values** describe the `action` string and optional `scope` (resource identifier) |
| 22 | + |
| 23 | +```typescript |
| 24 | +import type { PermissionValidationQuery } from '@openedx/frontend-base'; |
| 25 | + |
| 26 | +const query: PermissionValidationQuery = { |
| 27 | + canViewGrading: { |
| 28 | + action: 'courses.view_grading_settings', |
| 29 | + scope: 'course-v1:org+course+run', |
| 30 | + }, |
| 31 | + canEditGrading: { |
| 32 | + action: 'courses.edit_grading_settings', |
| 33 | + scope: 'course-v1:org+course+run', |
| 34 | + }, |
| 35 | +}; |
| 36 | +``` |
| 37 | + |
| 38 | +### Caching |
| 39 | + |
| 40 | +Results are cached using TanStack Query. The cache key includes the query object and the |
| 41 | +resolved `apiBaseUrl`, so different backends and different permission sets are cached |
| 42 | +independently. Results are reused across components that request the same permissions within |
| 43 | +one session. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## `usePermissions` |
| 48 | + |
| 49 | +The single hook for querying permissions. Requires a `featureEnabled` boolean — always |
| 50 | +pass the resolved waffle flag value so the caller explicitly opts in or out of authz. |
| 51 | +Permission keys are spread at the top level — no nested `.permissions` object. |
| 52 | + |
| 53 | +```typescript |
| 54 | +import { usePermissions } from '@openedx/frontend-base'; |
| 55 | +import { getConfig } from '@edx/frontend-platform'; |
| 56 | + |
| 57 | +// featureEnabled is required — always pass the resolved waffle flag boolean: |
| 58 | +const { enableAuthz } = useWaffleFlags(resourceId); |
| 59 | +const { isLoading, isAuthzEnabled, canViewGrading, canEditGrading } = usePermissions( |
| 60 | + { |
| 61 | + canViewGrading: { action: 'courses.view_grading_settings', scope: resourceId }, |
| 62 | + canEditGrading: { action: 'courses.edit_grading_settings', scope: resourceId }, |
| 63 | + }, |
| 64 | + enableAuthz ?? false, |
| 65 | +); |
| 66 | + |
| 67 | +// Override the backend URL (e.g. MFEs using @edx/frontend-platform): |
| 68 | +const { isLoading, canViewGrading } = usePermissions( |
| 69 | + { canViewGrading: { action: 'courses.view_grading_settings', scope: courseId } }, |
| 70 | + enableAuthz ?? false, |
| 71 | + { apiBaseUrl: getConfig().LMS_BASE_URL }, |
| 72 | +); |
| 73 | + |
| 74 | +if (!canViewGrading) { return <PermissionDeniedAlert />; } |
| 75 | +``` |
| 76 | + |
| 77 | +When `featureEnabled` is `false`: no API call is made and all keys return `true`, |
| 78 | +preserving the pre-authz behavior during rollout. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Recommended: create an MFE-specific wrapper |
| 83 | + |
| 84 | +Avoid calling `usePermissions` directly in every component. Create a single MFE-level |
| 85 | +wrapper that encapsulates the waffle flag check and base URL: |
| 86 | + |
| 87 | +```typescript |
| 88 | +import { usePermissions } from '@openedx/frontend-base'; |
| 89 | +import { getConfig } from '@edx/frontend-platform'; |
| 90 | +import { useWaffleFlags } from './waffleHooks'; // your MFE's waffle flag hook |
| 91 | +import type { PermissionValidationQuery } from '@openedx/frontend-base'; |
| 92 | + |
| 93 | +export const useResourcePermissions = <Query extends PermissionValidationQuery>( |
| 94 | + resourceId: string, |
| 95 | + permissions: Query, |
| 96 | +) => { |
| 97 | + const { enableAuthz } = useWaffleFlags(resourceId); |
| 98 | + return usePermissions( |
| 99 | + permissions, |
| 100 | + enableAuthz ?? false, |
| 101 | + { apiBaseUrl: getConfig().LMS_BASE_URL }, |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +export const getResourcePermissions = (resourceId: string): PermissionValidationQuery => ({ |
| 106 | + canView: { action: 'resources.view', scope: resourceId }, |
| 107 | + canEdit: { action: 'resources.edit', scope: resourceId }, |
| 108 | +}); |
| 109 | + |
| 110 | +// Usage in any component: |
| 111 | +const { isLoading, canView, canEdit } = |
| 112 | + useResourcePermissions(resourceId, getResourcePermissions(resourceId)); |
| 113 | +``` |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Best Practices |
| 118 | + |
| 119 | +- **Define permission constants** in your MFE (`COURSE_PERMISSIONS`, etc.) rather than |
| 120 | + inline strings — prevents typos and makes global renames easy. |
| 121 | +- **Use query builder helpers** (`getGradingPermissions(courseId)`) to build the query |
| 122 | + object — keeps permission definitions co-located with the feature they belong to. |
| 123 | +- **Do not duplicate `{ action, scope }` pairs** within a single query — only the first |
| 124 | + matching key is mapped in the response. |
| 125 | +- **Keep `featureEnabled` close to the flag source** — the boolean should come directly |
| 126 | + from your waffle flag check, not be stored in state or passed through many layers. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Manual Cache Invalidation |
| 131 | + |
| 132 | +If user roles change mid-session and you need to force a refetch: |
| 133 | + |
| 134 | +```typescript |
| 135 | +import { permissionsQueryKeys } from '@openedx/frontend-base'; |
| 136 | +import { getConfig } from '@edx/frontend-platform'; |
| 137 | + |
| 138 | +// Default — URL comes from getSiteConfig().lmsBaseUrl (set via mergeSiteConfig): |
| 139 | +queryClient.invalidateQueries({ |
| 140 | + queryKey: permissionsQueryKeys.validate(myQuery), |
| 141 | +}); |
| 142 | + |
| 143 | +// Explicit URL — use when you passed apiBaseUrl in UsePermissionsOptions: |
| 144 | +queryClient.invalidateQueries({ |
| 145 | + queryKey: permissionsQueryKeys.validate(myQuery, getConfig().LMS_BASE_URL), |
| 146 | +}); |
| 147 | +``` |
0 commit comments