Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elena-shostak committed Jan 6, 2025
1 parent c6b979a commit f021a82
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,65 +27,60 @@ interface Props {
}

export const PrivilegeSummaryExpandedRow = (props: Props) => {
return (
<EuiFlexGroup direction="column">
{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 (
<EuiFlexItem key={subFeature.name} data-test-subj="subFeatureEntry">
<EuiFlexGroup>
<EuiFlexItem>
<EuiText size="s" data-test-subj="subFeatureName">
{subFeature.name}
</EuiText>
</EuiFlexItem>
{props.effectiveFeaturePrivileges.map(
([spaces, privs]: EffectivePrivilegesTuple, index) => {
return (
<EuiFlexItem key={index} data-test-subj={`entry-${index}`}>
{subFeature.getPrivilegeGroups().map(
renderPrivilegeGroup(privs.subFeature, {
requireAllSpaces: subFeature.requireAllSpaces,
spaces,
})
)}
</EuiFlexItem>
);
}
)}
</EuiFlexGroup>
<EuiFlexItem key={index} data-test-subj={`entry-${index}`}>
{subFeature.getPrivilegeGroups().map(
renderPrivilegeGroup(privs.subFeature, {
requireAllSpaces: subFeature.requireAllSpaces,
spaces,
})
)}
</EuiFlexItem>
);
})}
</EuiFlexGroup>
});
},
[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,
Expand Down Expand Up @@ -152,4 +148,23 @@ export const PrivilegeSummaryExpandedRow = (props: Props) => {
</EuiFlexGroup>
);
}

return (
<EuiFlexGroup direction="column">
{props.feature.getSubFeatures().map((subFeature) => {
return (
<EuiFlexItem key={subFeature.name} data-test-subj="subFeatureEntry">
<EuiFlexGroup>
<EuiFlexItem>
<EuiText size="s" data-test-subj="subFeatureName">
{subFeature.name}
</EuiText>
</EuiFlexItem>
{getEffectiveFeaturePrivileges(subFeature)}
</EuiFlexGroup>
</EuiFlexItem>
);
})}
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}),
Expand Down Expand Up @@ -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'],
}),
Expand Down Expand Up @@ -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'],
}),
},
},
Expand All @@ -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'],
}),
},
},
Expand Down Expand Up @@ -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'],
}),
},
},
Expand All @@ -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'],
}),
},
},
Expand Down Expand Up @@ -859,7 +859,7 @@ describe('PrivilegeSummaryTable', () => {
hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges,
primaryFeaturePrivilege: 'Read',
...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, {
'Cool Sub Feature': ['Cool toggle 2', 'All'],
'Cool Sub Feature': ['All'],
}),
},
},
Expand Down Expand Up @@ -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'],
}),
},
},
Expand Down Expand Up @@ -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'],
}),
},
},
Expand All @@ -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'],
}),
},
},
Expand Down Expand Up @@ -1214,7 +1214,7 @@ describe('PrivilegeSummaryTable', () => {
hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges,
primaryFeaturePrivilege: 'All',
...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, {
'Cool Sub Feature': ['Cool toggle 2'],
'Cool Sub Feature': [],
}),
},
},
Expand Down Expand Up @@ -1251,7 +1251,7 @@ describe('PrivilegeSummaryTable', () => {
hasCustomizedSubFeaturePrivileges: allowSubFeaturePrivileges,
primaryFeaturePrivilege: 'All',
...maybeExpectSubFeaturePrivileges(allowSubFeaturePrivileges, {
'Excluded Sub Feature': ['Cool toggle 1'],
'Excluded Sub Feature': [],
}),
},
},
Expand All @@ -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': {
Expand All @@ -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'],
}),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,27 @@ 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 ||
(primaryFeature?.requireAllSpaces && !allSpacesSelected)
) {
return 'None';
}

if (primaryFeature?.requireAllSpaces && allSpacesSelected) {
return globalPrimaryFeature?.name ?? primaryFeature?.name;
}

return primaryFeature?.name;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
</span>
);
Expand Down

0 comments on commit f021a82

Please sign in to comment.