Skip to content

Commit

Permalink
[Discover] Do not update defaultIndex in case of insufficient permiss…
Browse files Browse the repository at this point in the history
…ions (#134202)

Co-authored-by: Matt Kime <[email protected]>
  • Loading branch information
kertal and mattkime authored Jun 24, 2022
1 parent 7369816 commit b475ed6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe('IndexPatterns', () => {
onError: () => {},
onRedirectNoIndexPattern: () => {},
getCanSave: () => Promise.resolve(true),
getCanSaveAdvancedSettings: () => Promise.resolve(true),
});

indexPatternsNoAccess = new DataViewsService({
Expand All @@ -114,6 +115,7 @@ describe('IndexPatterns', () => {
onError: () => {},
onRedirectNoIndexPattern: () => {},
getCanSave: () => Promise.resolve(false),
getCanSaveAdvancedSettings: () => Promise.resolve(false),
});
});

Expand Down Expand Up @@ -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);
});
});
});
25 changes: 20 additions & 5 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface DataViewsServiceDeps {
*/
fieldFormats: FieldFormatsStartCommon;
/**
* Hander for service notifications
* Handler for service notifications
*/
onNotification: OnNotification;
/**
Expand All @@ -111,6 +111,10 @@ export interface DataViewsServiceDeps {
* Determines whether the user can save data views
*/
getCanSave: () => Promise<boolean>;
/**
* Determines whether the user can save advancedSettings (used for defaultIndex)
*/
getCanSaveAdvancedSettings: () => Promise<boolean>;
}

/**
Expand Down Expand Up @@ -275,11 +279,14 @@ export class DataViewsService {
*/
private onError: OnError;
private dataViewCache: ReturnType<typeof createDataViewCache>;
/**
* Can the user save advanced settings?
*/
private getCanSaveAdvancedSettings: () => Promise<boolean>;
/**
* Can the user save data views?
*/
public getCanSave: () => Promise<boolean>;

/**
* DataViewsService constructor
* @param deps Service dependencies
Expand All @@ -293,6 +300,7 @@ export class DataViewsService {
onNotification,
onError,
getCanSave = () => Promise.resolve(false),
getCanSaveAdvancedSettings,
} = deps;
this.apiClient = apiClient;
this.config = uiSettings;
Expand All @@ -301,6 +309,7 @@ export class DataViewsService {
this.onNotification = onNotification;
this.onError = onError;
this.getCanSave = getCanSave;
this.getCanSaveAdvancedSettings = getCanSaveAdvancedSettings;

this.dataViewCache = createDataViewCache();
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data_views/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/plugins/data_views/server/data_views_service_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

0 comments on commit b475ed6

Please sign in to comment.