Skip to content

Commit

Permalink
Consolidate capabilities check for Stack Management (elastic#69437)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
2 people authored and Bamieh committed Jul 1, 2020
1 parent a9aa9b0 commit 8584dbb
Show file tree
Hide file tree
Showing 28 changed files with 119 additions and 136 deletions.
13 changes: 4 additions & 9 deletions src/plugins/advanced_settings/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/
import { i18n } from '@kbn/i18n';
import { CoreSetup, CoreStart, Plugin } from 'kibana/public';
import { ManagementApp, ManagementSectionId } from '../../management/public';
import { CoreSetup, Plugin } from 'kibana/public';
import { ManagementSectionId } from '../../management/public';
import { ComponentRegistry } from './component_registry';
import { AdvancedSettingsSetup, AdvancedSettingsStart, AdvancedSettingsPluginSetup } from './types';

Expand All @@ -30,11 +30,10 @@ const title = i18n.translate('advancedSettings.advancedSettingsLabel', {

export class AdvancedSettingsPlugin
implements Plugin<AdvancedSettingsSetup, AdvancedSettingsStart, AdvancedSettingsPluginSetup> {
private managementApp?: ManagementApp;
public setup(core: CoreSetup, { management }: AdvancedSettingsPluginSetup) {
const kibanaSection = management.sections.getSection(ManagementSectionId.Kibana);

this.managementApp = kibanaSection.registerApp({
kibanaSection.registerApp({
id: 'settings',
title,
order: 3,
Expand All @@ -51,11 +50,7 @@ export class AdvancedSettingsPlugin
};
}

public start(core: CoreStart) {
if (!core.application.capabilities.management.kibana.settings) {
this.managementApp!.disable();
}

public start() {
return {
component: component.start,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const onRedirectNoIndexPattern = (
navigateToApp: CoreStart['application']['navigateToApp'],
overlays: CoreStart['overlays']
) => () => {
const canManageIndexPatterns = capabilities.management.kibana.index_patterns;
const canManageIndexPatterns = capabilities.management.kibana.indexPatterns;
const redirectTarget = canManageIndexPatterns ? '/management/kibana/indexPatterns' : '/home';
let timeoutId: NodeJS.Timeout | undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/server/saved_objects/index_patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const indexPatternSavedObjectType: SavedObjectsType = {
getInAppUrl(obj) {
return {
path: `/app/management/kibana/indexPatterns/patterns/${encodeURIComponent(obj.id)}`,
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
};
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/home/server/capabilities_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export const capabilitiesProvider = () => ({
visualize: true,
console: true,
advanced_settings: true,
index_patterns: true,
indexPatterns: true,
},
});
9 changes: 2 additions & 7 deletions src/plugins/index_pattern_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
IndexPatternManagementServiceStart,
} from './service';

import { ManagementSetup, ManagementApp, ManagementSectionId } from '../../management/public';
import { ManagementSetup, ManagementSectionId } from '../../management/public';

export interface IndexPatternManagementSetupDependencies {
management: ManagementSetup;
Expand Down Expand Up @@ -57,7 +57,6 @@ export class IndexPatternManagementPlugin
IndexPatternManagementStartDependencies
> {
private readonly indexPatternManagementService = new IndexPatternManagementService();
private managementApp?: ManagementApp;

constructor(initializerContext: PluginInitializerContext) {}

Expand All @@ -80,7 +79,7 @@ export class IndexPatternManagementPlugin
return pathInApp && `/patterns${pathInApp}`;
});

this.managementApp = kibanaSection.registerApp({
kibanaSection.registerApp({
id: IPM_APP_ID,
title: sectionsHeader,
order: 0,
Expand All @@ -95,10 +94,6 @@ export class IndexPatternManagementPlugin
}

public start(core: CoreStart, plugins: IndexPatternManagementStartDependencies) {
if (!core.application.capabilities.management.kibana.index_patterns) {
this.managementApp!.disable();
}

return this.indexPatternManagementService.start();
}

Expand Down
44 changes: 42 additions & 2 deletions src/plugins/management/public/management_sections_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ describe('ManagementService', () => {
managementService = new ManagementSectionsService();
});

const capabilities = {
navLinks: {},
catalogue: {},
management: {},
};

test('Provides default sections', () => {
managementService.setup();
const start = managementService.start();
const start = managementService.start({ capabilities });

expect(start.getAllSections().length).toEqual(6);
expect(start.getSection(ManagementSectionId.Ingest)).toBeDefined();
Expand All @@ -48,12 +54,46 @@ describe('ManagementService', () => {
expect(setup.getSection('test-section')).not.toBeUndefined();

// Start phase:
const start = managementService.start();
const start = managementService.start({ capabilities });

expect(start.getSectionsEnabled().length).toEqual(7);

testSection.disable();

expect(start.getSectionsEnabled().length).toEqual(6);
});

test('Disables items that are not allowed by Capabilities', () => {
// Setup phase:
const setup = managementService.setup();
const testSection = setup.register({ id: 'test-section', title: 'Test Section' });
testSection.registerApp({ id: 'test-app-1', title: 'Test App 1', mount: jest.fn() });
testSection.registerApp({ id: 'test-app-2', title: 'Test App 2', mount: jest.fn() });
testSection.registerApp({ id: 'test-app-3', title: 'Test App 3', mount: jest.fn() });

expect(setup.getSection('test-section')).not.toBeUndefined();

// Start phase:
managementService.start({
capabilities: {
navLinks: {},
catalogue: {},
management: {
['test-section']: {
'test-app-1': true,
'test-app-2': false,
// test-app-3 intentionally left undefined. Should be enabled by default
},
},
},
});

expect(testSection.apps).toHaveLength(3);
expect(testSection.getAppsEnabled().map((app) => app.id)).toMatchInlineSnapshot(`
Array [
"test-app-1",
"test-app-3",
]
`);
});
});
20 changes: 18 additions & 2 deletions src/plugins/management/public/management_sections_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { ReactElement } from 'react';
import { ManagementSection, RegisterManagementSectionArgs } from './utils';
import { managementSections } from './components/management_sections';

import { ManagementSectionId, SectionsServiceSetup, SectionsServiceStart } from './types';
import {
ManagementSectionId,
SectionsServiceSetup,
SectionsServiceStart,
SectionsServiceStartDeps,
} from './types';

export class ManagementSectionsService {
private sections: Map<ManagementSectionId | string, ManagementSection> = new Map();
Expand Down Expand Up @@ -55,7 +60,18 @@ export class ManagementSectionsService {
};
}

start(): SectionsServiceStart {
start({ capabilities }: SectionsServiceStartDeps): SectionsServiceStart {
this.getAllSections().forEach((section) => {
if (capabilities.management.hasOwnProperty(section.id)) {
const sectionCapabilities = capabilities.management[section.id];
section.apps.forEach((app) => {
if (sectionCapabilities.hasOwnProperty(app.id) && sectionCapabilities[app.id] !== true) {
app.disable();
}
});
}
});

return {
getSection: this.getSection,
getAllSections: this.getAllSections,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class ManagementPlugin implements Plugin<ManagementSetup, ManagementStart

public start(core: CoreStart) {
return {
sections: this.managementSections.start(),
sections: this.managementSections.start({ capabilities: core.application.capabilities }),
};
}
}
8 changes: 6 additions & 2 deletions src/plugins/management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { ReactElement } from 'react';
import { ScopedHistory } from 'kibana/public';
import { ScopedHistory, Capabilities } from 'kibana/public';
import { ManagementSection, RegisterManagementSectionArgs } from './utils';
import { ChromeBreadcrumb } from '../../../core/public/';

Expand All @@ -30,8 +30,12 @@ export interface ManagementStart {
sections: SectionsServiceStart;
}

export interface SectionsServiceStartDeps {
capabilities: Capabilities;
}

export interface SectionsServiceSetup {
register: (args: RegisterManagementSectionArgs) => ManagementSection;
register: (args: Omit<RegisterManagementSectionArgs, 'capabilities'>) => ManagementSection;
getSection: (sectionId: ManagementSectionId | string) => ManagementSection;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/management/server/capabilities_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const capabilitiesProvider = () => ({
*/
kibana: {
settings: true,
index_patterns: true,
indexPatterns: true,
objects: true,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('canViewInApp', () => {
let uiCapabilities = createCapabilities({
management: {
kibana: {
index_patterns: true,
indexPatterns: true,
},
},
});
Expand All @@ -81,7 +81,7 @@ describe('canViewInApp', () => {
uiCapabilities = createCapabilities({
management: {
kibana: {
index_patterns: false,
indexPatterns: false,
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function canViewInApp(uiCapabilities: Capabilities, type: string): boolea
case 'index-pattern':
case 'index-patterns':
case 'indexPatterns':
return uiCapabilities.management.kibana.index_patterns as boolean;
return uiCapabilities.management.kibana.indexPatterns as boolean;
case 'dashboard':
case 'dashboards':
return uiCapabilities.dashboard.show as boolean;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Relationships', () => {
editUrl: '#/management/kibana/indexPatterns/patterns/1',
inAppUrl: {
path: '/management/kibana/indexPatterns/patterns/1',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
},
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('Relationships', () => {
icon: 'indexPatternApp',
inAppUrl: {
path: '/app/management/kibana/indexPatterns/patterns/1',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
title: 'My Index Pattern',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const defaultProps: TableProps = {
editUrl: '#/management/kibana/indexPatterns/patterns/1',
inAppUrl: {
path: '/management/kibana/indexPatterns/patterns/1',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
},
Expand All @@ -68,7 +68,7 @@ const defaultProps: TableProps = {
editUrl: '#/management/kibana/indexPatterns/patterns/1',
inAppUrl: {
path: '/management/kibana/indexPatterns/patterns/1',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('SavedObjectsTable', () => {
editUrl: '#/management/kibana/indexPatterns/patterns/1',
inAppUrl: {
path: '/management/kibana/indexPatterns/patterns/1',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion test/api_integration/apis/saved_objects_management/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export default function ({ getService }: FtrProviderContext) {
inAppUrl: {
path:
'/app/management/kibana/indexPatterns/patterns/8963ca30-3224-11e8-a572-ffca06da1357',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function ({ getService }: FtrProviderContext) {
inAppUrl: {
path:
'/app/management/kibana/indexPatterns/patterns/8963ca30-3224-11e8-a572-ffca06da1357',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
},
Expand Down Expand Up @@ -128,7 +128,7 @@ export default function ({ getService }: FtrProviderContext) {
inAppUrl: {
path:
'/app/management/kibana/indexPatterns/patterns/8963ca30-3224-11e8-a572-ffca06da1357',
uiCapabilitiesPath: 'management.kibana.index_patterns',
uiCapabilitiesPath: 'management.kibana.indexPatterns',
},
},
relationship: 'child',
Expand Down
Loading

0 comments on commit 8584dbb

Please sign in to comment.