/* * Copyright OpenSearch Contributors * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ import React, { useState, Dispatch, SetStateAction } from 'react'; import { EuiInMemoryTable, EuiBasicTableColumn, RIGHT_ALIGNMENT, EuiButtonIcon, EuiText, EuiFlexGroup, EuiEmptyPrompt, EuiButton, } from '@elastic/eui'; import { PanelWithHeader } from '../../utils/panel-with-header'; import { DataObject, ActionGroupItem, ExpandedRowMapInterface, RoleIndexPermissionView, ResourceType, Action, } from '../../types'; import { truncatedListView, displayArray, tableItemsUIProps } from '../../utils/display-utils'; import { PermissionTree } from '../permission-tree'; import { getFieldLevelSecurityMethod } from '../../utils/index-permission-utils'; import { renderExpression, displayHeaderWithTooltip } from '../../utils/display-utils'; import { DocLinks, ToolTipContent } from '../../constants'; import { showTableStatusMessage } from '../../utils/loading-spinner-utils'; import { buildHashUrl } from '../../utils/url-builder'; import { EMPTY_FIELD_VALUE } from '../../ui-constants'; export function toggleRowDetails( item: RoleIndexPermissionView, actionGroupDict: DataObject, setItemIdToExpandedRowMap: Dispatch> ) { setItemIdToExpandedRowMap((prevState) => { const itemIdToExpandedRowMapValues = { ...prevState }; if (itemIdToExpandedRowMapValues[item.id]) { delete itemIdToExpandedRowMapValues[item.id]; } else { itemIdToExpandedRowMapValues[item.id] = ( ); } return itemIdToExpandedRowMapValues; }); } export function renderRowExpanstionArrow( itemIdToExpandedRowMap: ExpandedRowMapInterface, actionGroupDict: DataObject, setItemIdToExpandedRowMap: Dispatch> ) { return (item: RoleIndexPermissionView) => ( toggleRowDetails(item, actionGroupDict, setItemIdToExpandedRowMap)} aria-label={itemIdToExpandedRowMap[item.id] ? 'Collapse' : 'Expand'} iconType={itemIdToExpandedRowMap[item.id] ? 'arrowUp' : 'arrowDown'} /> ); } export function renderFieldLevelSecurity() { return (items: string[]) => { // Show - to indicate empty if (items === undefined || items.length === 0) { return ( {EMPTY_FIELD_VALUE} ); } return ( {getFieldLevelSecurityMethod(items) === 'exclude' ? 'Exclude' : 'Include'}:{' '} {displayArray(items.map((s: string) => s.replace(/^~/, '')))} ); }; } export function renderDocumentLevelSecurity() { return (dls: string) => { if (!dls) { return EMPTY_FIELD_VALUE; } // TODO: unify the experience for both cases which may require refactoring of renderExpression. try { return renderExpression('Document-level security', JSON.parse(dls)); } catch (e) { // Support the use case for $variable without double quotes in DLS, e.g. variable is an array. console.warn('Failed to parse dls as json!'); return dls; } }; } function getColumns( itemIdToExpandedRowMap: ExpandedRowMapInterface, actionGroupDict: DataObject, setItemIdToExpandedRowMap: Dispatch> ): Array> { return [ { field: 'index_patterns', name: 'Index', sortable: true, render: truncatedListView(tableItemsUIProps), truncateText: true, }, { field: 'allowed_actions', name: 'Permissions', render: truncatedListView(tableItemsUIProps), truncateText: true, }, { field: 'dls', name: displayHeaderWithTooltip( 'Document-level security', ToolTipContent.DocumentLevelSecurity ), render: renderDocumentLevelSecurity(), }, { field: 'fls', name: displayHeaderWithTooltip('Field-level security', ToolTipContent.FieldLevelSecurity), render: renderFieldLevelSecurity(), }, { field: 'masked_fields', name: 'Anonymizations', render: truncatedListView(tableItemsUIProps), truncateText: true, }, { align: RIGHT_ALIGNMENT, width: '40px', isExpander: true, render: renderRowExpanstionArrow( itemIdToExpandedRowMap, actionGroupDict, setItemIdToExpandedRowMap ), }, ]; } interface IndexPermissionPanelProps { roleName: string; indexPermissions: RoleIndexPermissionView[]; actionGroups: DataObject; errorFlag: boolean; loading: boolean; isReserved: boolean; } export function IndexPermissionPanel(props: IndexPermissionPanelProps) { const [itemIdToExpandedRowMap, setItemIdToExpandedRowMap] = useState({}); const emptyListMessage = ( No index permission} titleSize="s" actions={ { window.location.href = buildHashUrl(ResourceType.roles, Action.edit, props.roleName); }} > Add index permission } /> ); const headerText = 'Index permissions'; return ( ); }