Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Always check license at plugin startup #87873

Merged
merged 8 commits into from
Jan 19, 2021
46 changes: 29 additions & 17 deletions x-pack/plugins/maps/public/licensed_features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,40 @@ export const LICENCED_FEATURES_DETAILS: Record<LICENSED_FEATURES, LicensedFeatur

let licenseId: string | undefined;
let isGoldPlus: boolean = false;

let isEnterprisePlus: boolean = false;

export const getLicenseId = () => licenseId;
export const getIsGoldPlus = () => isGoldPlus;

export const getIsEnterprisePlus = () => isEnterprisePlus;

let licensingPluginStart: LicensingPluginStart;
let initializeLicense: (value: unknown) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: how about moving initializeLicense, licenseInitialized, and whenLicenseInitialized next to setLicensingPluginStart so all of the logic sits in a single place in the file.

const licenseInitialized = new Promise((resolve) => {
initializeLicense = resolve;
});
export const whenLicenseInitialized = async (): Promise<void> => {
await licenseInitialized;
};

export async function setLicensingPluginStart(licensingPlugin: LicensingPluginStart) {
const license = await licensingPlugin.refresh();
updateLicenseState(license);

licensingPluginStart = licensingPlugin;
licensingPluginStart.license$.subscribe(updateLicenseState);

initializeLicense(undefined);
}

function updateLicenseState(license: ILicense) {
const gold = license.check(APP_ID, 'gold');
isGoldPlus = gold.state === 'valid';

const enterprise = license.check(APP_ID, 'enterprise');
isEnterprisePlus = enterprise.state === 'valid';

licenseId = license.uid;
}

export function registerLicensedFeatures(licensingPlugin: LicensingPluginSetup) {
for (const licensedFeature of Object.values(LICENSED_FEATURES)) {
licensingPlugin.featureUsage.register(
Expand All @@ -53,20 +79,6 @@ export function registerLicensedFeatures(licensingPlugin: LicensingPluginSetup)
}
}

let licensingPluginStart: LicensingPluginStart;
export function setLicensingPluginStart(licensingPlugin: LicensingPluginStart) {
licensingPluginStart = licensingPlugin;
licensingPluginStart.license$.subscribe((license: ILicense) => {
const gold = license.check(APP_ID, 'gold');
isGoldPlus = gold.state === 'valid';

const enterprise = license.check(APP_ID, 'enterprise');
isEnterprisePlus = enterprise.state === 'valid';

licenseId = license.uid;
});
}

export function notifyLicensedFeatureUsage(licensedFeature: LICENSED_FEATURES) {
if (!licensingPluginStart) {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { copyPersistentState } from '../../../reducers/util';
import { getBreadcrumbs } from './get_breadcrumbs';
import { DEFAULT_IS_LAYER_TOC_OPEN } from '../../../reducers/ui';
import { createBasemapLayerDescriptor } from '../../../classes/layers/create_basemap_layer_descriptor';
import { whenLicenseInitialized } from '../../../licensed_features';

export class SavedMap {
private _attributes: MapSavedObjectAttributes | null = null;
Expand Down Expand Up @@ -87,6 +88,8 @@ export class SavedMap {
}

async whenReady() {
await whenLicenseInitialized();

if (!this._mapEmbeddableInput) {
this._attributes = {
title: '',
Expand Down