diff --git a/src/plugins/data_views/common/data_views/data_views.test.ts b/src/plugins/data_views/common/data_views/data_views.test.ts index b3088bcd79330..d374253347b55 100644 --- a/src/plugins/data_views/common/data_views/data_views.test.ts +++ b/src/plugins/data_views/common/data_views/data_views.test.ts @@ -103,6 +103,7 @@ describe('IndexPatterns', () => { onError: () => {}, onRedirectNoIndexPattern: () => {}, getCanSave: () => Promise.resolve(true), + getCanSaveAdvancedSettings: () => Promise.resolve(true), }); indexPatternsNoAccess = new DataViewsService({ @@ -114,6 +115,7 @@ describe('IndexPatterns', () => { onError: () => {}, onRedirectNoIndexPattern: () => {}, getCanSave: () => Promise.resolve(false), + getCanSaveAdvancedSettings: () => Promise.resolve(false), }); }); @@ -438,5 +440,30 @@ describe('IndexPatterns', () => { expect(uiSettings.remove).toBeCalledTimes(0); expect(uiSettings.set).toBeCalledTimes(1); }); + test('dont set defaultIndex without capability allowing advancedSettings save', async () => { + savedObjectsClient.find = jest.fn().mockResolvedValue([ + { + id: 'id1', + version: 'a', + attributes: { title: '1' }, + }, + { + id: 'id2', + version: 'a', + attributes: { title: '2' }, + }, + ]); + + savedObjectsClient.get = jest + .fn() + .mockImplementation((type: string, id: string) => + Promise.resolve({ id, version: 'a', attributes: { title: '1' } }) + ); + + const defaultDataViewResult = await indexPatternsNoAccess.getDefaultDataView(); + expect(defaultDataViewResult).toBeInstanceOf(DataView); + expect(defaultDataViewResult?.id).toBe('id1'); + expect(uiSettings.set).toBeCalledTimes(0); + }); }); }); diff --git a/src/plugins/data_views/common/data_views/data_views.ts b/src/plugins/data_views/common/data_views/data_views.ts index e22a39107c949..3ffb326cd4611 100644 --- a/src/plugins/data_views/common/data_views/data_views.ts +++ b/src/plugins/data_views/common/data_views/data_views.ts @@ -96,7 +96,7 @@ export interface DataViewsServiceDeps { */ fieldFormats: FieldFormatsStartCommon; /** - * Hander for service notifications + * Handler for service notifications */ onNotification: OnNotification; /** @@ -111,6 +111,10 @@ export interface DataViewsServiceDeps { * Determines whether the user can save data views */ getCanSave: () => Promise; + /** + * Determines whether the user can save advancedSettings (used for defaultIndex) + */ + getCanSaveAdvancedSettings: () => Promise; } /** @@ -275,11 +279,14 @@ export class DataViewsService { */ private onError: OnError; private dataViewCache: ReturnType; + /** + * Can the user save advanced settings? + */ + private getCanSaveAdvancedSettings: () => Promise; /** * Can the user save data views? */ public getCanSave: () => Promise; - /** * DataViewsService constructor * @param deps Service dependencies @@ -293,6 +300,7 @@ export class DataViewsService { onNotification, onError, getCanSave = () => Promise.resolve(false), + getCanSaveAdvancedSettings, } = deps; this.apiClient = apiClient; this.config = uiSettings; @@ -301,6 +309,7 @@ export class DataViewsService { this.onNotification = onNotification; this.onError = onError; this.getCanSave = getCanSave; + this.getCanSaveAdvancedSettings = getCanSaveAdvancedSettings; this.dataViewCache = createDataViewCache(); } @@ -837,9 +846,10 @@ export class DataViewsService { } /** - * Save existing dat aview. Will attempt to merge differences if there are conflicts. + * Save existing data view. Will attempt to merge differences if there are conflicts. * @param indexPattern * @param saveAttempts + * @param ignoreErrors */ async updateSavedObject( @@ -959,7 +969,10 @@ export class DataViewsService { const exists = defaultId ? patterns.some((pattern) => pattern.id === defaultId) : false; if (defaultId && !exists) { - await this.config.remove('defaultIndex'); + if (await this.getCanSaveAdvancedSettings()) { + await this.config.remove('defaultIndex'); + } + defaultId = undefined; } @@ -973,7 +986,9 @@ export class DataViewsService { ); defaultId = userDataViews[0]?.id ?? patterns[0].id; - await this.config.set('defaultIndex', defaultId); + if (await this.getCanSaveAdvancedSettings()) { + await this.config.set('defaultIndex', defaultId); + } } if (defaultId) { diff --git a/src/plugins/data_views/public/plugin.ts b/src/plugins/data_views/public/plugin.ts index 1de31618172b6..df379ab18964e 100644 --- a/src/plugins/data_views/public/plugin.ts +++ b/src/plugins/data_views/public/plugin.ts @@ -74,6 +74,8 @@ export class DataViewsPublicPlugin }, getCanSave: () => Promise.resolve(application.capabilities.indexPatterns.save === true), getCanSaveSync: () => application.capabilities.indexPatterns.save === true, + getCanSaveAdvancedSettings: () => + Promise.resolve(application.capabilities.advancedSettings.save === true), }); } diff --git a/src/plugins/data_views/server/data_views_service_factory.ts b/src/plugins/data_views/server/data_views_service_factory.ts index f1501b19ca438..bc910b3822dba 100644 --- a/src/plugins/data_views/server/data_views_service_factory.ts +++ b/src/plugins/data_views/server/data_views_service_factory.ts @@ -59,5 +59,11 @@ export const dataViewsServiceFactory = (deps: DataViewsServiceFactoryDeps) => : request ? (await capabilities.resolveCapabilities(request)).indexPatterns.save === true : false, + getCanSaveAdvancedSettings: async () => + byPassCapabilities + ? true + : request + ? (await capabilities.resolveCapabilities(request)).advancedSettings.save === true + : false, }); };