Skip to content

Commit

Permalink
fixed bug 1427
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhas Kurapati <[email protected]>
  • Loading branch information
prabhask5 committed Oct 18, 2023
1 parent d3b3f84 commit bf0a636
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
12 changes: 11 additions & 1 deletion public/apps/account/account-nav-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@elastic/eui';
import { CoreStart } from 'opensearch-dashboards/public';
import React, { useCallback } from 'react';
import { keys } from 'lodash';
import { RoleInfoPanel } from './role-info-panel';
import { PasswordResetPanel } from './password-reset-panel';
import { TenantSwitchPanel } from './tenant-switch-panel';
Expand All @@ -36,6 +37,7 @@ import { LogoutButton } from './log-out-button';
import { resolveTenantName } from '../configuration/utils/tenant-utils';
import { getShouldShowTenantPopup, setShouldShowTenantPopup } from '../../utils/storage-utils';
import { getDashboardsInfo } from '../../utils/dashboards-info-utils';
import { fetchAccountInfo } from './utils';

export function AccountNavButton(props: {
coreStart: CoreStart;
Expand All @@ -46,10 +48,12 @@ export function AccountNavButton(props: {
currAuthType: string;
}) {
const [isPopoverOpen, setPopoverOpen] = React.useState<boolean>(false);
const [tenants, setTenants] = React.useState<string[]>([]);
const [modal, setModal] = React.useState<React.ReactNode>(null);
const horizontalRule = <EuiHorizontalRule margin="xs" />;
const username = props.username;
const [isMultiTenancyEnabled, setIsMultiTenancyEnabled] = React.useState<boolean>(true);
const GLOBAL_TENANT_KEY_NAME = 'global_tenant';

const showTenantSwitchPanel = useCallback(
() =>
Expand All @@ -72,6 +76,9 @@ export function AccountNavButton(props: {
React.useEffect(() => {
const fetchData = async () => {
try {
const accountInfo = await fetchAccountInfo(props.coreStart.http);
const tenantsInfo = accountInfo.data.tenants || {};
setTenants(keys(tenantsInfo));
setIsMultiTenancyEnabled(
(await getDashboardsInfo(props.coreStart.http)).multitenancy_enabled
);
Expand All @@ -84,6 +91,9 @@ export function AccountNavButton(props: {
fetchData();
}, [props.coreStart.http]);

const isGlobalEnabled = props.config.multitenancy.tenants.enable_global;
const shouldDisableGlobal = !isGlobalEnabled || !tenants.includes(GLOBAL_TENANT_KEY_NAME);

// Check if the tenant modal should be shown on load
if (isMultiTenancyEnabled && getShouldShowTenantPopup() && props.config.multitenancy.enabled) {
setShouldShowTenantPopup(false);
Expand Down Expand Up @@ -113,7 +123,7 @@ export function AccountNavButton(props: {
key="tenant"
label={
<EuiText size="xs" id="tenantName">
{resolveTenantName(props.tenant || '', username)}
{resolveTenantName(props.tenant || '', username, shouldDisableGlobal)}
</EuiText>
}
/>
Expand Down
22 changes: 17 additions & 5 deletions public/apps/account/tenant-switch-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ export function TenantSwitchPanel(props: TenantSwitchPanelProps) {
);
const [isMultiTenancyEnabled, setIsMultiTenancyEnabled] = useState(true);

const setCurrentTenant = (currentRawTenantName: string, currentUserName: string) => {
const resolvedTenantName = resolveTenantName(currentRawTenantName, currentUserName);
const setCurrentTenant = (
currentRawTenantName: string,
currentUserName: string,
tempTenantsList: string[]
) => {
const isGlobalEnabled = props.config.multitenancy.tenants.enable_global;
const shouldDisableGlobal =
!isGlobalEnabled || !tempTenantsList.includes(GLOBAL_TENANT_KEY_NAME);
const resolvedTenantName = resolveTenantName(
currentRawTenantName,
currentUserName,
shouldDisableGlobal
);

if (resolvedTenantName === RESOLVED_GLOBAL_TENANT) {
setTenantSwitchRadioIdSelected(GLOBAL_TENANT_RADIO_ID);
Expand All @@ -91,8 +102,9 @@ export function TenantSwitchPanel(props: TenantSwitchPanelProps) {
const dashboardsInfo = await getDashboardsInfo(props.coreStart.http);
setIsMultiTenancyEnabled(dashboardsInfo.multitenancy_enabled);
setIsPrivateEnabled(dashboardsInfo.private_tenant_enabled);
const tenantsInfo = accountInfo.data.tenants || {};
setTenants(keys(tenantsInfo));
const tenantsInfo = accountInfo.data.tenants || {}; // temp variable to pass current tenants list state to setCurrentTenant
const tempTenantsList = keys(tenantsInfo);
setTenants(tempTenantsList);

const currentUserName = accountInfo.data.user_name;
setUsername(currentUserName);
Expand All @@ -103,7 +115,7 @@ export function TenantSwitchPanel(props: TenantSwitchPanelProps) {
} else {
currentRawTenantName = accountInfo.data.user_requested_tenant;
}
setCurrentTenant(currentRawTenantName || '', currentUserName);
setCurrentTenant(currentRawTenantName || '', currentUserName, tempTenantsList);
} catch (e) {
// TODO: switch to better error display.
console.error(e);
Expand Down
14 changes: 11 additions & 3 deletions public/apps/configuration/panels/tenant-list/manage_tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
EuiCallOut,
} from '@elastic/eui';
import React, { ReactNode, useState, useCallback } from 'react';
import { difference } from 'lodash';
import { difference, keys } from 'lodash';
import { HashRouter as Router, Route } from 'react-router-dom';
import { flow } from 'lodash';
import { TenancyConfigSettings } from '../tenancy-config/types';
Expand Down Expand Up @@ -72,6 +72,7 @@ import { getBreadcrumbs, Route_MAP } from '../../app-router';
import { buildUrl } from '../../utils/url-builder';
import { CrossPageToast } from '../../cross-page-toast';
import { getDashboardsInfo } from '../../../../utils/dashboards-info-utils';
import { fetchAccountInfo } from '../../../account/utils';

export function ManageTab(props: AppDependencies) {
const setGlobalBreadcrumbs = flow(getBreadcrumbs, props.coreStart.chrome.setBreadcrumbs);
Expand All @@ -92,16 +93,23 @@ export function ManageTab(props: AppDependencies) {
const [dashboardsDefaultTenant, setDashboardsDefaultTenant] = useState('');

const { http } = props.coreStart;
const GLOBAL_TENANT_KEY_NAME = 'global_tenant';

const fetchData = useCallback(async () => {
try {
setLoading(true);
const accountInfo = await fetchAccountInfo(http);
const tenantsInfo = accountInfo.data.tenants || {};
const tempTenantList = keys(tenantsInfo);
const isGlobalEnabled = props.config.multitenancy.tenants.enable_global;
const shouldDisableGlobal =
!isGlobalEnabled || !tempTenantList.includes(GLOBAL_TENANT_KEY_NAME);
const rawTenantData = await fetchTenants(http);
const processedTenantData = transformTenantData(rawTenantData);
const activeTenant = await fetchCurrentTenant(http);
const currentUser = await getCurrentUser(http);
setCurrentUsername(currentUser);
setCurrentTenant(resolveTenantName(activeTenant, currentUser));
setCurrentTenant(resolveTenantName(activeTenant, currentUser, shouldDisableGlobal));
setTenantData(processedTenantData);
const tenancyConfig = await getDashboardsInfo(http);
setIsMultiTenancyEnabled(tenancyConfig.multitenancy_enabled);
Expand All @@ -113,7 +121,7 @@ export function ManageTab(props: AppDependencies) {
} finally {
setLoading(false);
}
}, [http]);
}, [http, props.config.multitenancy.tenants.enable_global]);

React.useEffect(() => {
fetchData();
Expand Down
7 changes: 4 additions & 3 deletions public/apps/configuration/utils/tenant-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@ export async function selectTenant(http: HttpStart, selectObject: TenantSelect):
export const RESOLVED_GLOBAL_TENANT = 'Global';
export const RESOLVED_PRIVATE_TENANT = 'Private';

export function resolveTenantName(tenant: string, userName: string) {
if (!tenant || tenant === 'undefined') {
export function resolveTenantName(tenant: string, userName: string, isGlobalDisabled: boolean) {
const probablyGlobal = !tenant || tenant === 'undefined';
if (probablyGlobal && !isGlobalDisabled) {
return RESOLVED_GLOBAL_TENANT;
}
if (tenant === userName || tenant === '__user__') {
if (probablyGlobal || tenant === userName || tenant === '__user__') {
return RESOLVED_PRIVATE_TENANT;
} else {
return tenant;
Expand Down
28 changes: 18 additions & 10 deletions public/apps/configuration/utils/test/tenant-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ describe('Tenant list utils', () => {
});

it('transform global, private and custom tenant', () => {
const result = transformTenantData(
{ global_tenant: globalTenant, dummy: sampleTenant1 },
true
);
const result = transformTenantData({ global_tenant: globalTenant, dummy: sampleTenant1 });
expect(result.length).toBe(3);
expect(result[0]).toEqual(expectedGlobalTenantListing);
expect(result[1]).toEqual(expectedPrivateTenantListing);
Expand All @@ -114,33 +111,44 @@ describe('Tenant list utils', () => {
});

describe('Resolve tenant name', () => {
const tenantNames = ['', 'undefined', 'user1', '__user__', ' dummy'];
const tenantNames = ['', 'undefined', 'user1', '__user__', ' dummy', '', 'undefined'];
const isGlobalDisabled = [false, false, false, false, false, true, true];
const userName = 'user1';

it('resolve to global tenant when tenant name is empty', () => {
const result = resolveTenantName(tenantNames[0], userName);
const result = resolveTenantName(tenantNames[0], userName, isGlobalDisabled[0]);
expect(result).toBe(RESOLVED_GLOBAL_TENANT);
});

it('resolve to global tenant when tenant name is undefined', () => {
const result = resolveTenantName(tenantNames[1], userName);
const result = resolveTenantName(tenantNames[1], userName, isGlobalDisabled[1]);
expect(result).toBe(RESOLVED_GLOBAL_TENANT);
});

it('resolve to private tenant when tenant name is user name', () => {
const result = resolveTenantName(tenantNames[2], userName);
const result = resolveTenantName(tenantNames[2], userName, isGlobalDisabled[2]);
expect(result).toBe(RESOLVED_PRIVATE_TENANT);
});

it('resolve to private tenant when tenant name is __user__', () => {
const result = resolveTenantName(tenantNames[3], userName);
const result = resolveTenantName(tenantNames[3], userName, isGlobalDisabled[3]);
expect(result).toBe(RESOLVED_PRIVATE_TENANT);
});

it('resolve to actual tenant name when tenant name is custom', () => {
const result = resolveTenantName(tenantNames[4], userName);
const result = resolveTenantName(tenantNames[4], userName, isGlobalDisabled[4]);
expect(result).toBe(tenantNames[4]);
});

it('resolve to private tenant when tenant name is empty and global is disabled', () => {
const result = resolveTenantName(tenantNames[5], userName, isGlobalDisabled[5]);
expect(result).toBe(RESOLVED_PRIVATE_TENANT);
});

it('resolve to private tenant when tenant name is undefined and global is disabled', () => {
const result = resolveTenantName(tenantNames[6], userName, isGlobalDisabled[6]);
expect(result).toBe(RESOLVED_PRIVATE_TENANT);
});
});

describe('Format tenant name', () => {
Expand Down

0 comments on commit bf0a636

Please sign in to comment.