Skip to content

Commit 43e8d5b

Browse files
Improvements
1 parent b5bf2dc commit 43e8d5b

File tree

9 files changed

+60
-74
lines changed

9 files changed

+60
-74
lines changed

packages/x-data-grid-premium/src/tests/license.DataGridPremium.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('<DataGridPremium /> - License', () => {
1515
orderNumber: 'Test',
1616
licensingModel: 'subscription',
1717
scope: 'pro',
18-
planVersion: 'initial',
18+
planVersion: 'Q3-2024',
1919
}),
2020
);
2121
expect(() => render(<DataGridPremium columns={[]} rows={[]} autoHeight />)).toErrorDev([

packages/x-license/src/Watermark/Watermark.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
2-
import { MuiCommercialPackageName, useLicenseVerifier } from '../useLicenseVerifier';
2+
import { useLicenseVerifier } from '../useLicenseVerifier';
33
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
4+
import { MuiCommercialPackageName } from '../utils/commercialPackages';
45

56
function getLicenseErrorMessage(licenseStatus: LicenseStatus) {
67
switch (licenseStatus) {
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export { useLicenseVerifier } from './useLicenseVerifier';
2-
export type { MuiCommercialPackageName } from './useLicenseVerifier';

packages/x-license/src/useLicenseVerifier/useLicenseVerifier.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,8 @@ import {
1111
showNotAvailableInInitialProPlanError,
1212
} from '../utils/licenseErrorMessageUtils';
1313
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
14-
import { extractAcceptedScopes, extractProductLine } from '../utils/licenseScope';
1514
import MuiLicenseInfoContext from '../Unstable_LicenseInfoProvider/MuiLicenseInfoContext';
16-
17-
export type MuiCommercialPackageName =
18-
| 'x-data-grid-pro'
19-
| 'x-data-grid-premium'
20-
| 'x-date-pickers-pro'
21-
| 'x-tree-view-pro'
22-
| 'x-charts-pro';
15+
import { MuiCommercialPackageName } from '../utils/commercialPackages';
2316

2417
export const sharedLicenseStatuses: {
2518
[packageName in MuiCommercialPackageName]?: {
@@ -48,15 +41,11 @@ export function useLicenseVerifier(
4841
return sharedLicenseStatuses[packageName]!.licenseVerifier;
4942
}
5043

51-
const acceptedScopes = extractAcceptedScopes(packageName);
52-
const productLine = extractProductLine(packageName);
53-
5444
const plan = packageName.includes('premium') ? 'Premium' : 'Pro';
5545
const licenseStatus = verifyLicense({
5646
releaseInfo,
5747
licenseKey,
58-
acceptedScopes,
59-
productLine,
48+
packageName,
6049
});
6150

6251
const fullPackageName = `@mui/${packageName}`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type MuiCommercialPackageName =
2+
| 'x-data-grid-pro'
3+
| 'x-data-grid-premium'
4+
| 'x-date-pickers-pro'
5+
| 'x-tree-view-pro'
6+
| 'x-charts-pro';

packages/x-license/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './licenseInfo';
33
export * from './licenseStatus';
44
export type { LicenseScope } from './licenseScope';
55
export type { LicensingModel } from './licensingModel';
6+
export type { MuiCommercialPackageName } from './commercialPackages';

packages/x-license/src/utils/licenseScope.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,3 @@ export const PLAN_VERSIONS = ['initial', 'Q3-2024'] as const;
33

44
export type LicenseScope = (typeof LICENSE_SCOPES)[number];
55
export type PlanVersion = (typeof PLAN_VERSIONS)[number];
6-
export type ProductLine = 'data-grid' | 'date-pickers' | 'charts' | 'tree-view'
7-
8-
export const extractProductLine = (packageName: string): ProductLine => {
9-
// extract the part between "x-" and "-pro"/"-premium"
10-
const regex = /x-(.*?)(-pro|-premium)?$/;
11-
const match = packageName.match(regex);
12-
return match![1] as ProductLine;
13-
};
14-
15-
export const extractAcceptedScopes = (packageName: string): readonly LicenseScope[] => {
16-
return packageName.includes('premium')
17-
? LICENSE_SCOPES.filter((scope) => scope.includes('premium'))
18-
: LICENSE_SCOPES;
19-
};

packages/x-license/src/verifyLicense/verifyLicense.test.ts

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ describe('License: verifyLicense', () => {
3030
verifyLicense({
3131
releaseInfo: '__RELEASE_INFO__',
3232
licenseKey,
33-
acceptedScopes: ['pro', 'premium'],
34-
productLine: 'data-grid',
33+
packageName: 'x-data-grid-pro',
3534
}).status,
3635
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
3736
});
@@ -42,8 +41,7 @@ describe('License: verifyLicense', () => {
4241
verifyLicense({
4342
releaseInfo: RELEASE_INFO,
4443
licenseKey,
45-
acceptedScopes: ['pro', 'premium'],
46-
productLine: 'data-grid',
44+
packageName: 'x-data-grid-pro',
4745
}).status,
4846
).to.equal(LICENSE_STATUS.Valid);
4947
});
@@ -62,8 +60,7 @@ describe('License: verifyLicense', () => {
6260
verifyLicense({
6361
releaseInfo: RELEASE_INFO,
6462
licenseKey: expiredLicenseKey,
65-
acceptedScopes: ['pro', 'premium'],
66-
productLine: 'data-grid',
63+
packageName: 'x-data-grid-pro',
6764
}).status,
6865
).to.equal(LICENSE_STATUS.ExpiredVersion);
6966
});
@@ -75,8 +72,7 @@ describe('License: verifyLicense', () => {
7572
releaseInfo: RELEASE_INFO,
7673
licenseKey:
7774
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
78-
acceptedScopes: ['pro', 'premium'],
79-
productLine: 'data-grid',
75+
packageName: 'x-data-grid-pro',
8076
}).status,
8177
).to.equal(LICENSE_STATUS.Invalid);
8278
});
@@ -106,8 +102,7 @@ describe('License: verifyLicense', () => {
106102
verifyLicense({
107103
releaseInfo: '__RELEASE_INFO__',
108104
licenseKey: licenseKeyPro,
109-
acceptedScopes: ['pro', 'premium'],
110-
productLine: 'data-grid',
105+
packageName: 'x-data-grid-pro',
111106
}).status,
112107
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
113108
});
@@ -119,8 +114,7 @@ describe('License: verifyLicense', () => {
119114
verifyLicense({
120115
releaseInfo: RELEASE_INFO,
121116
licenseKey: licenseKeyPro,
122-
acceptedScopes: ['pro', 'premium'],
123-
productLine: 'data-grid',
117+
packageName: 'x-data-grid-pro',
124118
}).status,
125119
).to.equal(LICENSE_STATUS.Valid);
126120
});
@@ -131,8 +125,7 @@ describe('License: verifyLicense', () => {
131125
verifyLicense({
132126
releaseInfo: RELEASE_INFO,
133127
licenseKey: licenseKeyPremium,
134-
acceptedScopes: ['premium'],
135-
productLine: 'data-grid',
128+
packageName: 'x-data-grid-premium',
136129
}).status,
137130
).to.equal(LICENSE_STATUS.Valid);
138131
});
@@ -143,8 +136,7 @@ describe('License: verifyLicense', () => {
143136
verifyLicense({
144137
releaseInfo: RELEASE_INFO,
145138
licenseKey: licenseKeyPro,
146-
acceptedScopes: ['premium'],
147-
productLine: 'data-grid',
139+
packageName: 'x-data-grid-premium',
148140
}).status,
149141
).to.equal(LICENSE_STATUS.OutOfScope);
150142
});
@@ -165,8 +157,7 @@ describe('License: verifyLicense', () => {
165157
verifyLicense({
166158
releaseInfo: RELEASE_INFO,
167159
licenseKey: expiredLicenseKey,
168-
acceptedScopes: ['pro', 'premium'],
169-
productLine: 'data-grid',
160+
packageName: 'x-data-grid-pro',
170161
}).status,
171162
).to.equal(LICENSE_STATUS.Valid);
172163
});
@@ -184,8 +175,7 @@ describe('License: verifyLicense', () => {
184175
verifyLicense({
185176
releaseInfo: RELEASE_INFO,
186177
licenseKey: expiredLicenseKey,
187-
acceptedScopes: ['pro', 'premium'],
188-
productLine: 'data-grid',
178+
packageName: 'x-data-grid-pro',
189179
}).status,
190180
).to.equal(LICENSE_STATUS.ExpiredAnnualGrace);
191181
});
@@ -204,8 +194,7 @@ describe('License: verifyLicense', () => {
204194
verifyLicense({
205195
releaseInfo: RELEASE_INFO,
206196
licenseKey: expiredLicenseKey,
207-
acceptedScopes: ['pro', 'premium'],
208-
productLine: 'data-grid',
197+
packageName: 'x-data-grid-pro',
209198
}).status,
210199
).to.equal(LICENSE_STATUS.ExpiredAnnual);
211200
});
@@ -223,8 +212,7 @@ describe('License: verifyLicense', () => {
223212
verifyLicense({
224213
releaseInfo: RELEASE_INFO,
225214
licenseKey: expiredLicenseKey,
226-
acceptedScopes: ['pro', 'premium'],
227-
productLine: 'data-grid',
215+
packageName: 'x-data-grid-pro',
228216
}).status,
229217
).to.equal(LICENSE_STATUS.Valid);
230218
});
@@ -237,8 +225,7 @@ describe('License: verifyLicense', () => {
237225
releaseInfo: RELEASE_INFO,
238226
licenseKey:
239227
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
240-
acceptedScopes: ['pro', 'premium'],
241-
productLine: 'data-grid',
228+
packageName: 'x-data-grid-pro',
242229
}).status,
243230
).to.equal(LICENSE_STATUS.Invalid);
244231
});
@@ -259,8 +246,7 @@ describe('License: verifyLicense', () => {
259246
verifyLicense({
260247
releaseInfo: RELEASE_INFO,
261248
licenseKey: licenseKeyPro,
262-
acceptedScopes: ['pro', 'premium'],
263-
productLine: 'data-grid',
249+
packageName: 'x-data-grid-pro',
264250
}).status,
265251
).to.equal(LICENSE_STATUS.Valid);
266252
});
@@ -289,8 +275,7 @@ describe('License: verifyLicense', () => {
289275
verifyLicense({
290276
releaseInfo: RELEASE_INFO,
291277
licenseKey: licenseKeyInitial,
292-
acceptedScopes: ['pro', 'premium'],
293-
productLine: 'charts',
278+
packageName: 'x-charts-pro',
294279
}).status,
295280
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
296281
});
@@ -301,8 +286,7 @@ describe('License: verifyLicense', () => {
301286
verifyLicense({
302287
releaseInfo: RELEASE_INFO,
303288
licenseKey: licenseKeyInitial,
304-
acceptedScopes: ['pro', 'premium'],
305-
productLine: 'tree-view',
289+
packageName: 'x-tree-view-pro',
306290
}).status,
307291
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
308292
});
@@ -313,8 +297,7 @@ describe('License: verifyLicense', () => {
313297
verifyLicense({
314298
releaseInfo: RELEASE_INFO,
315299
licenseKey: licenseKey2,
316-
acceptedScopes: ['pro', 'premium'],
317-
productLine: 'charts',
300+
packageName: 'x-charts-pro',
318301
}).status,
319302
).to.equal(LICENSE_STATUS.Valid);
320303
});
@@ -325,8 +308,7 @@ describe('License: verifyLicense', () => {
325308
verifyLicense({
326309
releaseInfo: RELEASE_INFO,
327310
licenseKey: licenseKey2,
328-
acceptedScopes: ['pro', 'premium'],
329-
productLine: 'tree-view',
311+
packageName: 'x-tree-view-pro',
330312
}).status,
331313
).to.equal(LICENSE_STATUS.Valid);
332314
});

packages/x-license/src/verifyLicense/verifyLicense.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { base64Decode, base64Encode } from '../encoding/base64';
22
import { md5 } from '../encoding/md5';
33
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
4-
import { LicenseScope, LICENSE_SCOPES, ProductLine, PlanVersion } from '../utils/licenseScope';
4+
import { LicenseScope, LICENSE_SCOPES, PlanVersion } from '../utils/licenseScope';
55
import { LicensingModel, LICENSING_MODELS } from '../utils/licensingModel';
6+
import { MuiCommercialPackageName } from '../utils/commercialPackages';
67

78
const getDefaultReleaseDate = () => {
89
const today = new Date();
@@ -15,6 +16,22 @@ export function generateReleaseInfo(releaseDate = getDefaultReleaseDate()) {
1516
return base64Encode(releaseDate.getTime().toString());
1617
}
1718

19+
function isLicenseScopeSufficient(
20+
packageName: MuiCommercialPackageName,
21+
licenseScope: LicenseScope,
22+
) {
23+
let acceptedScopes: LicenseScope[];
24+
if (packageName.includes('pro')) {
25+
acceptedScopes = ['pro', 'premium'];
26+
} else if (packageName.includes('premium')) {
27+
acceptedScopes = ['premium'];
28+
} else {
29+
acceptedScopes = [];
30+
}
31+
32+
return acceptedScopes.includes(licenseScope);
33+
}
34+
1835
const expiryReg = /^.*EXPIRY=([0-9]+),.*$/;
1936

2037
interface MuiLicense {
@@ -24,7 +41,10 @@ interface MuiLicense {
2441
planVersion: PlanVersion;
2542
}
2643

27-
const PRODUCT_LINES_AVAILABLE_IN_INITIAL_PRO_PLAN: ProductLine[] = ['data-grid', 'date-pickers']
44+
const PRO_PACKAGES_AVAILABLE_IN_INITIAL_PRO_PLAN: MuiCommercialPackageName[] = [
45+
'x-data-grid-pro',
46+
'x-date-pickers-pro',
47+
];
2848

2949
/**
3050
* Format: ORDER:${orderNumber},EXPIRY=${expiryTimestamp},KEYVERSION=1
@@ -107,13 +127,11 @@ const decodeLicense = (encodedLicense: string): MuiLicense | null => {
107127
export function verifyLicense({
108128
releaseInfo,
109129
licenseKey,
110-
acceptedScopes,
111-
productLine,
130+
packageName,
112131
}: {
113132
releaseInfo: string;
114133
licenseKey?: string;
115-
acceptedScopes: readonly LicenseScope[];
116-
productLine: ProductLine;
134+
packageName: MuiCommercialPackageName;
117135
}): { status: LicenseStatus; meta?: any } {
118136
if (!releaseInfo) {
119137
throw new Error('MUI X: The release information is missing. Not able to validate license.');
@@ -181,11 +199,15 @@ export function verifyLicense({
181199
}
182200

183201
// 'charts-pro' or 'tree-view-pro' can only be used with a newer Pro license
184-
if (license.planVersion === 'initial' && license.scope === 'pro' && !PRODUCT_LINES_AVAILABLE_IN_INITIAL_PRO_PLAN.includes(productLine)) {
202+
if (
203+
license.planVersion === 'initial' &&
204+
license.scope === 'pro' &&
205+
!PRO_PACKAGES_AVAILABLE_IN_INITIAL_PRO_PLAN.includes(packageName)
206+
) {
185207
return { status: LICENSE_STATUS.NotAvailableInInitialProPlan };
186208
}
187209

188-
if (!acceptedScopes.includes(license.scope)) {
210+
if (!isLicenseScopeSufficient(packageName, license.scope)) {
189211
return { status: LICENSE_STATUS.OutOfScope };
190212
}
191213

0 commit comments

Comments
 (0)