Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/authz-module/role-detail/RoleDetailPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,9 @@ describe('RoleDetailPage', () => {
expect(screen.getByRole('tab', { name: messages['authz.custom.role.tabs.comparison'].defaultMessage, selected: true })).toBeInTheDocument();
});

it('opens Members for #comparison on a custom role, which has no Comparison tab', () => {
it('opens Comparison for #comparison on a custom role', () => {
renderRoleDetail('custom-role-123', '#comparison');
expect(screen.queryByRole('tab', { name: messages['authz.custom.role.tabs.comparison'].defaultMessage })).not.toBeInTheDocument();
expect(screen.getByRole('tab', { name: messages['authz.custom.role.tabs.members'].defaultMessage, selected: true })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: messages['authz.custom.role.tabs.comparison'].defaultMessage, selected: true })).toBeInTheDocument();
});

it('opens Members for an unknown hash', () => {
Expand All @@ -318,6 +317,25 @@ describe('RoleDetailPage', () => {
});
});

it.each([
['custom', 'user_defined' as const],
['built-in', 'static' as const],
])('renders the Comparison tab for %s roles', (_kind, definitionKind) => {
mockCatalogWithRoles({ ...mockRole, definitionKind });
renderRoleDetail();
expect(screen.getByRole('tab', { name: messages['authz.custom.role.tabs.comparison'].defaultMessage })).toBeInTheDocument();
});

it('shows the roles and permissions matrix of the role scope type in the Comparison tab', async () => {
const user = userEvent.setup();
mockCatalogWithRoles({ ...mockRole, scopeTypes: ['lib'] });
renderRoleDetail();
await user.click(screen.getByRole('tab', { name: messages['authz.custom.role.tabs.comparison'].defaultMessage }));
expect(await screen.findByRole('columnheader', { name: 'Library Roles' })).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Courses' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Libraries' })).not.toBeInTheDocument();
});

it('renders Permissions tab that can be navigated to', async () => {
const user = userEvent.setup();
renderRoleDetail();
Expand Down
23 changes: 10 additions & 13 deletions src/authz-module/role-detail/RoleDetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import AddRoleButton from '../components/AddRoleButton';
import DuplicateRoleButton from '../components/DuplicateRoleButton';
import { useCanManageRoles } from '../hooks/useCanManageRoles';
import RoleMembersTable from './members/RoleMembersTable';
import RolesPermissions from './permissions/RolesPermissions';
import RolePermissionEditor from './permissions/RolePermissionEditor';

// Tab keys, also used as the URL hash that opens each tab (e.g. `#comparison`).
const ROLE_DETAIL_TABS = {
Expand All @@ -28,14 +30,11 @@ const ROLE_DETAIL_TABS = {
COMPARISON: 'comparison',
} as const;

/**
* The tab the URL hash asks for. Comparison only exists for built-in roles, so a custom role
* falls back to Members, as does any other hash.
*/
const getTabFromHash = (hash: string, isCustom: boolean) => {
/** The tab the URL hash asks for; any other hash, or none, opens Members. */
const getTabFromHash = (hash: string) => {
const tab = hash.replace('#', '');
if (tab === ROLE_DETAIL_TABS.PERMISSIONS) return ROLE_DETAIL_TABS.PERMISSIONS;
if (tab === ROLE_DETAIL_TABS.COMPARISON && !isCustom) return ROLE_DETAIL_TABS.COMPARISON;
if (tab === ROLE_DETAIL_TABS.COMPARISON) return ROLE_DETAIL_TABS.COMPARISON;
return ROLE_DETAIL_TABS.MEMBERS;
};

Expand Down Expand Up @@ -155,20 +154,18 @@ const RoleDetailPage = () => {
)}
<Tabs
variant="tabs"
defaultActiveKey={getTabFromHash(hash, isCustom)}
defaultActiveKey={getTabFromHash(hash)}
className="page-band bg-light-100"
>
<Tab eventKey={ROLE_DETAIL_TABS.MEMBERS} title={intl.formatMessage(messages['authz.custom.role.tabs.members'])} className="page-band py-3">
<RoleMembersTable scopeTypes={scopeTypes} onDelete={handleDeleteMember} />
</Tab>
<Tab id="role-permissions-tab" eventKey={ROLE_DETAIL_TABS.PERMISSIONS} title={intl.formatMessage(messages['authz.custom.role.tabs.permissions'])} className="page-band py-3">

<RolePermissionEditor />
</Tab>
<Tab id="role-comparison-tab" eventKey={ROLE_DETAIL_TABS.COMPARISON} title={intl.formatMessage(messages['authz.custom.role.tabs.comparison'])} className="page-band py-3">
<RolesPermissions scopeType={scopeTypes[0]} />
</Tab>
{!isCustom && (
<Tab id="role-comparison-tab" eventKey={ROLE_DETAIL_TABS.COMPARISON} title={intl.formatMessage(messages['authz.custom.role.tabs.comparison'])} className="page-band py-3">

</Tab>
)}
</Tabs>
</AuthZLayout>
);
Expand Down
50 changes: 50 additions & 0 deletions src/authz-module/role-detail/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,56 @@ const messages = defineMessages({
defaultMessage: 'No users assigned to this role',
description: 'Message displayed when the members table is empty',
},
'authz.tabs.permissionsRoles.courses.alert.title': {
id: 'authz.tabs.permissionsRoles.courses.alert.title',
defaultMessage: 'Course Roles',
description: 'Libraries AuthZ title for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.tab': {
id: 'authz.tabs.permissionsRoles.courses.tab',
defaultMessage: 'Courses',
description: 'Libraries AuthZ title for the course roles tab',
},
'authz.tabs.permissionsRoles.libraries.tab': {
id: 'authz.tabs.permissionsRoles.libraries.tab',
defaultMessage: 'Libraries',
description: 'Libraries AuthZ title for the libraries roles tab',
},
'authz.tabs.permissionsRoles.libraries.tab.title': {
id: 'authz.tabs.permissionsRoles.libraries.tab.title',
defaultMessage: 'Library Roles',
description: 'Libraries AuthZ title for the library roles table',
},
'authz.tabs.permissionsRoles.courses.tab.title': {
id: 'authz.tabs.permissionsRoles.courses.tab.title',
defaultMessage: 'Course Roles',
description: 'Libraries AuthZ title for the course roles table',
},
'authz.tabs.permissionsRoles.courses.alert.note': {
id: 'authz.tabs.permissionsRoles.courses.alert.note',
defaultMessage: 'Note:',
description: 'Libraries AuthZ note for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.alert.description': {
id: 'authz.tabs.permissionsRoles.courses.alert.description',
defaultMessage: 'This list shows the permissions currently available in Authoring Studio. Some roles may grant additional permissions managed outside this interface.',
description: 'Libraries AuthZ description for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.alert.link': {
id: 'authz.tabs.permissionsRoles.courses.alert.link',
defaultMessage: 'See full documentation',
description: 'Libraries AuthZ link for the course roles alert',
},
'authz.tabs.permissionsRoles.loading': {
id: 'authz.tabs.permissionsRoles.loading',
defaultMessage: 'Loading roles and permissions',
description: 'Libraries AuthZ screen reader text while the roles and permissions are loading',
},
'authz.tabs.roleComparison.error': {
id: 'authz.tabs.roleComparison.error',
defaultMessage: 'The roles and permissions could not be loaded. Please try again later.',
description: 'Libraries AuthZ message shown when the roles and permissions fail to load',
},
});

export default messages;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const RolePermissionEditor = () => {
return <div></div>;
};

export default RolePermissionEditor;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jest.mock('@src/authz-module/catalog/data/hooks', () => ({
useAuthzCatalog: jest.fn(),
}));

jest.mock('../components/AnchorButton', () => function MockAnchorButton() {
jest.mock('@src/authz-module/components/AnchorButton', () => function MockAnchorButton() {
return <div data-testid="anchor-button" />;
});

Expand Down Expand Up @@ -71,6 +71,14 @@ describe('RolesPermissions', () => {
expect(screen.queryByText('See full documentation')).not.toBeInTheDocument();
});

it('shows only the given scope type, without the buttons to switch', async () => {
renderWithAllProviders(<RolesPermissions scopeType={SCOPE_TYPES.LIBRARY} />);
expect(await screen.findByRole('columnheader', { name: 'Library Roles' })).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Courses' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Libraries' })).not.toBeInTheDocument();
expect(screen.queryByRole('columnheader', { name: 'Course Roles' })).not.toBeInTheDocument();
});

it('shows a loading indicator while the catalog loads', () => {
mockAuthzCatalog({ isLoading: true });
renderWithAllProviders(<RolesPermissions />);
Expand Down

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider moving this, to role-detail. If not please use a barrel file for expose content to other features.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import AnchorButton from '@src/authz-module/components/AnchorButton';
import PermissionTable from '@src/authz-module/components/PermissionTable';
import componentMessages from '@src/authz-module/components/messages';

import { buildPermissionMatrix } from './utils';
import { buildPermissionMatrix } from '@src/authz-module/roles-permissions';

import messages from './messages';
import messages from '../messages';

// Category of the permissions the catalog sends without one.
const OTHER_CATEGORY_ID = 'other';
Expand All @@ -34,20 +34,28 @@ const SCOPE_TYPE_LABELS: Partial<Record<string, { tab: MessageDescriptor; title:
},
};

interface RolesPermissionsProps {
// Fixes the matrix to this scope type and hides the buttons that switch between scope types.
scopeType?: string;
}

/**
* Roles and permissions matrix of one scope type at a time.
*
* Not rendered at the moment: the Roles and Permissions tab shows RolesPermissionsTable instead.
* It is kept on purpose because it will be reused later.
* Without `scopeType`, buttons switch between the scope types the user can view. The role detail
* page passes the role's scope type, so it only shows that one.
*/
const RolesPermissions = () => {
const RolesPermissions = ({ scopeType: fixedScopeType }: RolesPermissionsProps) => {
const intl = useIntl();
// The catalog only holds the scope types the user can view.
const {
scopeTypes, getScopeCatalog, isLoading, isError,
} = useAuthzCatalog();
const [selectedScopeType, setSelectedScopeType] = useState<string>();
const activeScopeType = selectedScopeType ?? scopeTypes[0];

const canSwitchScopeType = !fixedScopeType;
const userScopeType = selectedScopeType ?? scopeTypes[0];
const activeScopeType = canSwitchScopeType ? userScopeType : fixedScopeType;
const scopeCatalog = activeScopeType ? getScopeCatalog(activeScopeType) : undefined;

const matrix = useMemo(() => (scopeCatalog ? buildPermissionMatrix(scopeCatalog, {
Expand All @@ -73,7 +81,7 @@ const RolesPermissions = () => {
if (isError) {
return (
<Alert variant="danger">
{intl.formatMessage(messages['authz.tabs.permissionsRoles.error'])}
{intl.formatMessage(messages['authz.tabs.roleComparison.error'])}
</Alert>
);
}
Expand All @@ -88,19 +96,21 @@ const RolesPermissions = () => {
return (
<>
<Card className="bg-light-200">
<div className="px-3 py-4">
<ButtonGroup size="md">
{scopeTypes.map((scopeType) => (
<Button
key={scopeType}
onClick={() => setSelectedScopeType(scopeType)}
variant={scopeType === activeScopeType ? 'primary' : 'outline-primary'}
>
{formatScopeTypeLabel(scopeType, 'tab')}
</Button>
))}
</ButtonGroup>
</div>
{canSwitchScopeType && (
<div className="px-3 py-4">
<ButtonGroup size="md">
{scopeTypes.map((scopeType) => (
<Button
key={scopeType}
onClick={() => setSelectedScopeType(scopeType)}
variant={scopeType === activeScopeType ? 'primary' : 'outline-primary'}
>
{formatScopeTypeLabel(scopeType, 'tab')}
</Button>
))}
</ButtonGroup>
</div>
)}
<div className="position-relative">
<PermissionTable
matrix={matrix}
Expand Down
45 changes: 0 additions & 45 deletions src/authz-module/roles-permissions/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,6 @@ const messages = defineMessages({
defaultMessage: 'Roles and Permissions',
description: 'Libraries AuthZ title for the permissions and roles tab',
},
'authz.tabs.permissionsRoles.courses.alert.title': {
id: 'authz.tabs.permissionsRoles.courses.alert.title',
defaultMessage: 'Course Roles',
description: 'Libraries AuthZ title for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.tab': {
id: 'authz.tabs.permissionsRoles.courses.tab',
defaultMessage: 'Courses',
description: 'Libraries AuthZ title for the course roles tab',
},
'authz.tabs.permissionsRoles.libraries.tab': {
id: 'authz.tabs.permissionsRoles.libraries.tab',
defaultMessage: 'Libraries',
description: 'Libraries AuthZ title for the libraries roles tab',
},
'authz.tabs.permissionsRoles.libraries.tab.title': {
id: 'authz.tabs.permissionsRoles.libraries.tab.title',
defaultMessage: 'Library Roles',
description: 'Libraries AuthZ title for the library roles table',
},
'authz.tabs.permissionsRoles.courses.tab.title': {
id: 'authz.tabs.permissionsRoles.courses.tab.title',
defaultMessage: 'Course Roles',
description: 'Libraries AuthZ title for the course roles table',
},
'authz.tabs.permissionsRoles.courses.alert.note': {
id: 'authz.tabs.permissionsRoles.courses.alert.note',
defaultMessage: 'Note:',
description: 'Libraries AuthZ note for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.alert.description': {
id: 'authz.tabs.permissionsRoles.courses.alert.description',
defaultMessage: 'This list shows the permissions currently available in Authoring Studio. Some roles may grant additional permissions managed outside this interface.',
description: 'Libraries AuthZ description for the course roles alert',
},
'authz.tabs.permissionsRoles.courses.alert.link': {
id: 'authz.tabs.permissionsRoles.courses.alert.link',
defaultMessage: 'See full documentation',
description: 'Libraries AuthZ link for the course roles alert',
},
'authz.tabs.permissionsRoles.loading': {
id: 'authz.tabs.permissionsRoles.loading',
defaultMessage: 'Loading roles and permissions',
description: 'Libraries AuthZ screen reader text while the roles and permissions are loading',
},
'authz.tabs.permissionsRoles.error': {
id: 'authz.tabs.permissionsRoles.error',
defaultMessage: 'The roles and permissions could not be loaded. Please try again later.',
Expand Down