Skip to content

Commit

Permalink
[Maps] Always check license at plugin startup (#87873)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jan 19, 2021
1 parent 978f7a3 commit 1e71320
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
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;
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

0 comments on commit 1e71320

Please sign in to comment.