diff --git a/src/core/server/ui_settings/integration_tests/lib/servers.ts b/src/core/server/ui_settings/integration_tests/lib/servers.ts index b4cfc3c1efe8..04979b69b32b 100644 --- a/src/core/server/ui_settings/integration_tests/lib/servers.ts +++ b/src/core/server/ui_settings/integration_tests/lib/servers.ts @@ -73,9 +73,9 @@ export function getServices() { httpServerMock.createKibanaRequest() ); - const uiSettings = kbnServer.server.uiSettingsServiceFactory({ - savedObjectsClient, - }); + const uiSettings = kbnServer.newPlatform.start.core.uiSettings.asScopedToClient( + savedObjectsClient + ); services = { kbnServer, diff --git a/src/legacy/server/kbn_server.d.ts b/src/legacy/server/kbn_server.d.ts index 9bb091383ab1..1a1f43b93f26 100644 --- a/src/legacy/server/kbn_server.d.ts +++ b/src/legacy/server/kbn_server.d.ts @@ -45,7 +45,6 @@ import { LegacyConfig, ILegacyService, ILegacyInternals } from '../../core/serve import { UiPlugins } from '../../core/server/plugins'; import { CallClusterWithRequest, ElasticsearchPlugin } from '../core_plugins/elasticsearch'; import { UsageCollectionSetup } from '../../plugins/usage_collection/server'; -import { UiSettingsServiceFactoryOptions } from '../../legacy/ui/ui_settings/ui_settings_service_factory'; import { HomeServerPluginSetup } from '../../plugins/home/server'; // lot of legacy code was assuming this type only had these two methods @@ -78,7 +77,6 @@ declare module 'hapi' { name: string, factoryFn: (request: Request) => Record ) => void; - uiSettingsServiceFactory: (options?: UiSettingsServiceFactoryOptions) => IUiSettingsClient; logWithMetadata: (tags: string[], message: string, meta: Record) => void; newPlatform: KbnServer['newPlatform']; } diff --git a/src/legacy/ui/ui_mixin.js b/src/legacy/ui/ui_mixin.js index 432c4f02bc3e..54da001d2066 100644 --- a/src/legacy/ui/ui_mixin.js +++ b/src/legacy/ui/ui_mixin.js @@ -19,10 +19,8 @@ import { uiAppsMixin } from './ui_apps'; import { uiRenderMixin } from './ui_render'; -import { uiSettingsMixin } from './ui_settings'; export async function uiMixin(kbnServer) { await kbnServer.mixin(uiAppsMixin); - await kbnServer.mixin(uiSettingsMixin); await kbnServer.mixin(uiRenderMixin); } diff --git a/src/legacy/ui/ui_render/ui_render_mixin.js b/src/legacy/ui/ui_render/ui_render_mixin.js index 23fb6028f84d..8cc2cd1321a6 100644 --- a/src/legacy/ui/ui_render/ui_render_mixin.js +++ b/src/legacy/ui/ui_render/ui_render_mixin.js @@ -21,6 +21,7 @@ import { createHash } from 'crypto'; import Boom from 'boom'; import { i18n } from '@kbn/i18n'; import * as UiSharedDeps from '@kbn/ui-shared-deps'; +import { KibanaRequest } from '../../../core/server'; import { AppBootstrap } from './bootstrap'; import { getApmConfig } from '../apm'; @@ -79,7 +80,10 @@ export function uiRenderMixin(kbnServer, server, config) { auth: authEnabled ? { mode: 'try' } : false, }, async handler(request, h) { - const uiSettings = request.getUiSettingsService(); + const soClient = kbnServer.newPlatform.start.core.savedObjects.getScopedClient( + KibanaRequest.from(request) + ); + const uiSettings = kbnServer.newPlatform.start.core.uiSettings.asScopedToClient(soClient); const darkMode = !authEnabled || request.auth.isAuthenticated diff --git a/src/legacy/ui/ui_settings/index.js b/src/legacy/ui/ui_settings/index.js deleted file mode 100644 index ec3122c4e390..000000000000 --- a/src/legacy/ui/ui_settings/index.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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. - */ - -export { uiSettingsMixin } from './ui_settings_mixin'; diff --git a/src/legacy/ui/ui_settings/integration_tests/ui_settings_mixin.test.ts b/src/legacy/ui/ui_settings/integration_tests/ui_settings_mixin.test.ts deleted file mode 100644 index 84a64d3f46f1..000000000000 --- a/src/legacy/ui/ui_settings/integration_tests/ui_settings_mixin.test.ts +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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 sinon from 'sinon'; -import expect from '@kbn/expect'; - -import { savedObjectsClientMock } from '../../../../core/server/mocks'; -import * as uiSettingsServiceFactoryNS from '../ui_settings_service_factory'; -import * as getUiSettingsServiceForRequestNS from '../ui_settings_service_for_request'; -// @ts-ignore -import { uiSettingsMixin } from '../ui_settings_mixin'; - -interface Decorators { - server: { [name: string]: any }; - request: { [name: string]: any }; -} - -const uiSettingDefaults = { - application: { - defaultProperty1: 'value1', - }, -}; - -describe('uiSettingsMixin()', () => { - const sandbox = sinon.createSandbox(); - - function setup() { - // maps of decorations passed to `server.decorate()` - const decorations: Decorators = { - server: {}, - request: {}, - }; - - // mock hapi server - const server = { - log: sinon.stub(), - route: sinon.stub(), - addMemoizedFactoryToRequest(name: string, factory: (...args: any[]) => any) { - this.decorate('request', name, function (this: typeof server) { - return factory(this); - }); - }, - decorate: sinon.spy((type: keyof Decorators, name: string, value: any) => { - decorations[type][name] = value; - }), - newPlatform: { - setup: { - core: { - uiSettings: { - register: sinon.stub(), - }, - }, - }, - }, - }; - - // "promise" returned from kbnServer.ready() - const readyPromise = { - then: sinon.stub(), - }; - - const kbnServer = { - server, - uiExports: { uiSettingDefaults }, - ready: sinon.stub().returns(readyPromise), - }; - - uiSettingsMixin(kbnServer, server); - - return { - kbnServer, - server, - decorations, - readyPromise, - }; - } - - afterEach(() => sandbox.restore()); - - it('passes uiSettingsDefaults to the new platform', () => { - const { server } = setup(); - sinon.assert.calledOnce(server.newPlatform.setup.core.uiSettings.register); - sinon.assert.calledWithExactly( - server.newPlatform.setup.core.uiSettings.register, - uiSettingDefaults - ); - }); - - describe('server.uiSettingsServiceFactory()', () => { - it('decorates server with "uiSettingsServiceFactory"', () => { - const { decorations } = setup(); - expect(decorations.server).to.have.property('uiSettingsServiceFactory').a('function'); - - const uiSettingsServiceFactoryStub = sandbox.stub( - uiSettingsServiceFactoryNS, - 'uiSettingsServiceFactory' - ); - sinon.assert.notCalled(uiSettingsServiceFactoryStub); - decorations.server.uiSettingsServiceFactory(); - sinon.assert.calledOnce(uiSettingsServiceFactoryStub); - }); - - it('passes `server` and `options` argument to factory', () => { - const { decorations, server } = setup(); - expect(decorations.server).to.have.property('uiSettingsServiceFactory').a('function'); - - const uiSettingsServiceFactoryStub = sandbox.stub( - uiSettingsServiceFactoryNS, - 'uiSettingsServiceFactory' - ); - - sinon.assert.notCalled(uiSettingsServiceFactoryStub); - - const savedObjectsClient = savedObjectsClientMock.create(); - decorations.server.uiSettingsServiceFactory({ - savedObjectsClient, - }); - sinon.assert.calledOnce(uiSettingsServiceFactoryStub); - sinon.assert.calledWithExactly(uiSettingsServiceFactoryStub, server as any, { - savedObjectsClient, - }); - }); - }); - - describe('request.getUiSettingsService()', () => { - it('exposes "getUiSettingsService" on requests', () => { - const { decorations } = setup(); - expect(decorations.request).to.have.property('getUiSettingsService').a('function'); - - const getUiSettingsServiceForRequestStub = sandbox.stub( - getUiSettingsServiceForRequestNS, - 'getUiSettingsServiceForRequest' - ); - sinon.assert.notCalled(getUiSettingsServiceForRequestStub); - decorations.request.getUiSettingsService(); - sinon.assert.calledOnce(getUiSettingsServiceForRequestStub); - }); - - it('passes request to getUiSettingsServiceForRequest', () => { - const { server, decorations } = setup(); - expect(decorations.request).to.have.property('getUiSettingsService').a('function'); - - const getUiSettingsServiceForRequestStub = sandbox.stub( - getUiSettingsServiceForRequestNS, - 'getUiSettingsServiceForRequest' - ); - sinon.assert.notCalled(getUiSettingsServiceForRequestStub); - const request = {}; - decorations.request.getUiSettingsService.call(request); - sinon.assert.calledWith(getUiSettingsServiceForRequestStub, server as any, request as any); - }); - }); - - describe('server.uiSettings()', () => { - it('throws an error, links to pr', () => { - const { decorations } = setup(); - expect(decorations.server).to.have.property('uiSettings').a('function'); - expect(() => { - decorations.server.uiSettings(); - }).to.throwError('http://github.com' as any); // incorrect typings - }); - }); -}); diff --git a/src/legacy/ui/ui_settings/ui_exports_consumer.js b/src/legacy/ui/ui_settings/ui_exports_consumer.js deleted file mode 100644 index d2bb3a00ce0e..000000000000 --- a/src/legacy/ui/ui_settings/ui_exports_consumer.js +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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. - */ - -/** - * The UiExports class accepts consumer objects that it consults while - * trying to consume all of the `uiExport` declarations provided by - * plugins. - * - * UiExportConsumer is instantiated and passed to UiExports, then for - * every `uiExport` declaration the `exportConsumer(type)` method is - * with the key of the declaration. If this consumer knows how to handle - * that key we return a function that will be called with the plugins - * and values of all declarations using that key. - * - * With this, the consumer merges all of the declarations into the - * _uiSettingDefaults map, ensuring that there are not collisions along - * the way. - * - * @class UiExportsConsumer - */ -export class UiExportsConsumer { - _uiSettingDefaults = {}; - - exportConsumer(type) { - switch (type) { - case 'uiSettingDefaults': - return (plugin, settingDefinitions) => { - Object.keys(settingDefinitions).forEach((key) => { - if (key in this._uiSettingDefaults) { - throw new Error(`uiSettingDefaults for key "${key}" are already defined`); - } - - this._uiSettingDefaults[key] = settingDefinitions[key]; - }); - }; - } - } - - /** - * Get the map of uiSettingNames to "default" specifications - * @return {Object} - */ - getUiSettingDefaults() { - return this._uiSettingDefaults; - } -} diff --git a/src/legacy/ui/ui_settings/ui_settings_mixin.js b/src/legacy/ui/ui_settings/ui_settings_mixin.js deleted file mode 100644 index 8190b67732da..000000000000 --- a/src/legacy/ui/ui_settings/ui_settings_mixin.js +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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 { uiSettingsServiceFactory } from './ui_settings_service_factory'; -import { getUiSettingsServiceForRequest } from './ui_settings_service_for_request'; - -export function uiSettingsMixin(kbnServer, server) { - const { uiSettingDefaults = {} } = kbnServer.uiExports; - const mergedUiSettingDefaults = Object.keys(uiSettingDefaults).reduce((acc, currentKey) => { - const defaultSetting = uiSettingDefaults[currentKey]; - const updatedDefaultSetting = { - ...defaultSetting, - }; - if (typeof defaultSetting.options === 'function') { - updatedDefaultSetting.options = defaultSetting.options(server); - } - if (typeof defaultSetting.value === 'function') { - updatedDefaultSetting.value = defaultSetting.value(server); - } - acc[currentKey] = updatedDefaultSetting; - return acc; - }, {}); - - server.newPlatform.setup.core.uiSettings.register(mergedUiSettingDefaults); - - server.decorate('server', 'uiSettingsServiceFactory', (options = {}) => { - return uiSettingsServiceFactory(server, options); - }); - - server.addMemoizedFactoryToRequest('getUiSettingsService', (request) => { - return getUiSettingsServiceForRequest(server, request); - }); - - server.decorate('server', 'uiSettings', () => { - throw new Error(` - server.uiSettings has been removed, see https://github.com/elastic/kibana/pull/12243. - `); - }); -} diff --git a/src/legacy/ui/ui_settings/ui_settings_service_factory.ts b/src/legacy/ui/ui_settings/ui_settings_service_factory.ts deleted file mode 100644 index 6c3c50d175dc..000000000000 --- a/src/legacy/ui/ui_settings/ui_settings_service_factory.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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 { Legacy } from 'kibana'; -import { IUiSettingsClient, SavedObjectsClientContract } from 'src/core/server'; - -export interface UiSettingsServiceFactoryOptions { - savedObjectsClient: SavedObjectsClientContract; -} -/** - * Create an instance of UiSettingsClient that will use the - * passed `savedObjectsClient` to communicate with elasticsearch - * - * @return {IUiSettingsClient} - */ -export function uiSettingsServiceFactory( - server: Legacy.Server, - options: UiSettingsServiceFactoryOptions -): IUiSettingsClient { - return server.newPlatform.start.core.uiSettings.asScopedToClient(options.savedObjectsClient); -} diff --git a/src/legacy/ui/ui_settings/ui_settings_service_for_request.ts b/src/legacy/ui/ui_settings/ui_settings_service_for_request.ts deleted file mode 100644 index 057fc64c9ebd..000000000000 --- a/src/legacy/ui/ui_settings/ui_settings_service_for_request.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License 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 { Legacy } from 'kibana'; -import { IUiSettingsClient } from 'src/core/server'; -import { uiSettingsServiceFactory } from './ui_settings_service_factory'; - -/** - * Get/create an instance of UiSettingsService bound to a specific request. - * Each call is cached (keyed on the request object itself) and subsequent - * requests will get the first UiSettingsService instance even if the `options` - * have changed. - * - * @param {Hapi.Server} server - * @param {Hapi.Request} request - * @param {Object} [options={}] - - * @return {IUiSettingsClient} - */ -export function getUiSettingsServiceForRequest( - server: Legacy.Server, - request: Legacy.Request -): IUiSettingsClient { - const savedObjectsClient = request.getSavedObjectsClient(); - return uiSettingsServiceFactory(server, { savedObjectsClient }); -}