From f021a8292b2e09cf71aafdbaf936eaa9b78ce200 Mon Sep 17 00:00:00 2001 From: Elena Shostak Date: Mon, 30 Dec 2024 20:13:28 +0100 Subject: [PATCH] Fixes --- .../privilege_summary_expanded_row.tsx | 125 ++++++++++-------- .../privilege_summary_table.test.tsx | 32 ++--- .../privilege_summary_table.tsx | 33 ++++- 3 files changed, 114 insertions(+), 76 deletions(-) diff --git a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_expanded_row.tsx b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_expanded_row.tsx index 52f1367ad3552..901f8dfd7483d 100644 --- a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_expanded_row.tsx +++ b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_expanded_row.tsx @@ -6,11 +6,12 @@ */ import { EuiFlexGroup, EuiFlexItem, EuiIconTip, EuiText } from '@elastic/eui'; -import React from 'react'; +import React, { useCallback } from 'react'; import { i18n } from '@kbn/i18n'; import type { SecuredFeature, + SecuredSubFeature, SubFeaturePrivilege, SubFeaturePrivilegeGroup, } from '@kbn/security-role-management-model'; @@ -26,65 +27,60 @@ interface Props { } export const PrivilegeSummaryExpandedRow = (props: Props) => { - return ( - - {props.feature.getSubFeatures().map((subFeature) => { + const allSpacesEffectivePrivileges = props.effectiveFeaturePrivileges.find(([spaces]) => + spaces.includes(ALL_SPACES_ID) + ); + + const renderPrivilegeGroup = useCallback( + ( + effectiveSubFeaturePrivileges: string[], + { requireAllSpaces, spaces }: { requireAllSpaces: boolean; spaces: string[] } + ) => { + return (privilegeGroup: SubFeaturePrivilegeGroup, index: number) => { + const isDisabledDueToSpaceSelection = requireAllSpaces && !spaces.includes(ALL_SPACES_ID); + + switch (privilegeGroup.groupType) { + case 'independent': + return renderIndependentPrivilegeGroup( + effectiveSubFeaturePrivileges, + privilegeGroup, + index + ); + case 'mutually_exclusive': + return renderMutuallyExclusivePrivilegeGroup( + effectiveSubFeaturePrivileges, + privilegeGroup, + index, + isDisabledDueToSpaceSelection + ); + default: + throw new Error(`Unsupported privilege group type: ${privilegeGroup.groupType}`); + } + }; + }, + [] + ); + + const getEffectiveFeaturePrivileges = useCallback( + (subFeature: SecuredSubFeature) => { + return props.effectiveFeaturePrivileges.map((entry, index) => { + const [spaces, privs] = allSpacesEffectivePrivileges ?? entry; + return ( - - - - - {subFeature.name} - - - {props.effectiveFeaturePrivileges.map( - ([spaces, privs]: EffectivePrivilegesTuple, index) => { - return ( - - {subFeature.getPrivilegeGroups().map( - renderPrivilegeGroup(privs.subFeature, { - requireAllSpaces: subFeature.requireAllSpaces, - spaces, - }) - )} - - ); - } - )} - + + {subFeature.getPrivilegeGroups().map( + renderPrivilegeGroup(privs.subFeature, { + requireAllSpaces: subFeature.requireAllSpaces, + spaces, + }) + )} ); - })} - + }); + }, + [props.effectiveFeaturePrivileges, allSpacesEffectivePrivileges, renderPrivilegeGroup] ); - function renderPrivilegeGroup( - effectiveSubFeaturePrivileges: string[], - { requireAllSpaces, spaces }: { requireAllSpaces: boolean; spaces: string[] } - ) { - return (privilegeGroup: SubFeaturePrivilegeGroup, index: number) => { - const isDisabledDueToSpaceSelection = requireAllSpaces && !spaces.includes(ALL_SPACES_ID); - - switch (privilegeGroup.groupType) { - case 'independent': - return renderIndependentPrivilegeGroup( - effectiveSubFeaturePrivileges, - privilegeGroup, - index - ); - case 'mutually_exclusive': - return renderMutuallyExclusivePrivilegeGroup( - effectiveSubFeaturePrivileges, - privilegeGroup, - index, - isDisabledDueToSpaceSelection - ); - default: - throw new Error(`Unsupported privilege group type: ${privilegeGroup.groupType}`); - } - }; - } - function renderIndependentPrivilegeGroup( effectiveSubFeaturePrivileges: string[], privilegeGroup: SubFeaturePrivilegeGroup, @@ -152,4 +148,23 @@ export const PrivilegeSummaryExpandedRow = (props: Props) => { ); } + + return ( + + {props.feature.getSubFeatures().map((subFeature) => { + return ( + + + + + {subFeature.name} + + + {getEffectiveFeaturePrivileges(subFeature)} + + + ); + })} + + ); }; diff --git a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.test.tsx b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.test.tsx index fa689b181e05e..f9ebe836253d6 100644 --- a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.test.tsx +++ b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.test.tsx @@ -281,7 +281,7 @@ describe('PrivilegeSummaryTable', () => { with_require_all_spaces_for_feature_and_sub_features: { '*': { hasCustomizedSubFeaturePrivileges: false, - primaryFeaturePrivilege: 'All', + primaryFeaturePrivilege: 'None', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { 'Require all spaces Sub Feature': ['Cool toggle 1'], }), @@ -361,7 +361,7 @@ describe('PrivilegeSummaryTable', () => { with_require_all_spaces_for_feature_and_sub_features: { '*': { hasCustomizedSubFeaturePrivileges: false, - primaryFeaturePrivilege: 'All', + primaryFeaturePrivilege: 'None', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { 'Require all spaces Sub Feature': ['Cool toggle 1'], }), @@ -611,7 +611,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 1', 'Cool toggle 2', 'All'], + 'Cool Sub Feature': ['Cool toggle 2', 'Read'], }), }, }, @@ -627,7 +627,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, }, @@ -735,7 +735,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges, primaryFeaturePrivilege: 'Read', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 2', 'All'], + 'Cool Sub Feature': ['Cool toggle 2', 'Read'], }), }, }, @@ -751,7 +751,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'Read', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, }, @@ -859,7 +859,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges, primaryFeaturePrivilege: 'Read', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 2', 'All'], + 'Cool Sub Feature': ['All'], }), }, }, @@ -985,7 +985,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 1', 'Cool toggle 2', 'All'], + 'Cool Sub Feature': ['All'], }), }, }, @@ -1133,7 +1133,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, }, @@ -1147,9 +1147,9 @@ describe('PrivilegeSummaryTable', () => { }, 'default, space-1': { hasCustomizedSubFeaturePrivileges: false, - primaryFeaturePrivilege: 'None', + primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, }, @@ -1214,7 +1214,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 2'], + 'Cool Sub Feature': [], }), }, }, @@ -1251,7 +1251,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Excluded Sub Feature': ['Cool toggle 1'], + 'Excluded Sub Feature': [], }), }, }, @@ -1267,7 +1267,7 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Cool Sub Feature': ['Cool toggle 1', 'Cool toggle 2', 'All'], + 'Cool Sub Feature': ['Cool toggle 2', 'Read'], }), }, 'space-1, space-2': { @@ -1290,14 +1290,14 @@ describe('PrivilegeSummaryTable', () => { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'All', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, 'space-1, space-2': { hasCustomizedSubFeaturePrivileges: false, primaryFeaturePrivilege: 'Read', ...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, { - 'Require all spaces Sub Feature': [], + 'Require all spaces Sub Feature': ['Cool toggle 1'], }), }, }, diff --git a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.tsx b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.tsx index 3665afa1cc040..906daac1965d0 100644 --- a/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.tsx +++ b/x-pack/platform/plugins/shared/security/public/management/roles/edit_role/privileges/kibana/privilege_summary/privilege_summary_table.tsx @@ -48,7 +48,15 @@ function getColumnKey(entry: RoleKibanaPrivilege) { return `privilege_entry_${entry.spaces.join('|')}`; } -function showPrivilege(allSpacesSelected: boolean, primaryFeature?: PrimaryFeaturePrivilege) { +function showPrivilege({ + allSpacesSelected, + primaryFeature, + globalPrimaryFeature, +}: { + allSpacesSelected: boolean; + primaryFeature?: PrimaryFeaturePrivilege; + globalPrimaryFeature?: PrimaryFeaturePrivilege; +}) { if ( primaryFeature?.name == null || primaryFeature?.disabled || @@ -56,6 +64,11 @@ function showPrivilege(allSpacesSelected: boolean, primaryFeature?: PrimaryFeatu ) { return 'None'; } + + if (primaryFeature?.requireAllSpaces && allSpacesSelected) { + return globalPrimaryFeature?.name ?? primaryFeature?.name; + } + return primaryFeature?.name; } @@ -127,6 +140,15 @@ export const PrivilegeSummaryTable = (props: PrivilegeSummaryTableProps) => { } return 0; }); + + const globalRawPrivilege = rawKibanaPrivileges.find((entry) => + isGlobalPrivilegeDefinition(entry) + ); + + const globalPrivilege = globalRawPrivilege + ? calculator.getEffectiveFeaturePrivileges(globalRawPrivilege) + : null; + const privilegeColumns = rawKibanaPrivileges.map((entry) => { const key = getColumnKey(entry); return { @@ -161,10 +183,11 @@ export const PrivilegeSummaryTable = (props: PrivilegeSummaryTableProps) => { hasCustomizedSubFeaturePrivileges ? 'additionalPrivilegesGranted' : '' }`} > - {showPrivilege( - entry.spaces.some((space) => space === ALL_SPACES_ID), - primary - )}{' '} + {showPrivilege({ + allSpacesSelected: props.spaces.some((space) => space.id === ALL_SPACES_ID), + primaryFeature: primary, + globalPrimaryFeature: globalPrivilege?.[record.featureId]?.primary, + })}{' '} {iconTip} );