Skip to content

Commit 6af7c71

Browse files
committed
chore: add entitlment validations and deactivate invalid entitlements
1 parent f3daff3 commit 6af7c71

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

src/nls/root/strings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,5 +1681,6 @@ define({
16811681
"PROMO_GET_APP_UPSELL_BUTTON": "Get {0}",
16821682
"PROMO_PRO_ENDED_TITLE": "Your {0} Trial has ended",
16831683
"PROMO_PRO_TRIAL_DAYS_LEFT": "Phoenix Pro Trial ({0} days left)",
1684-
"GET_PHOENIX_PRO": "Get Phoenix Pro"
1684+
"GET_PHOENIX_PRO": "Get Phoenix Pro",
1685+
"USER_FREE_PLAN_NAME": "Free Plan"
16851686
});

src/services/login-service.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
define(function (require, exports, module) {
2929
require("./setup-login-service"); // this adds loginService to KernalModeTrust
3030
require("./promotions");
31+
require("./login-utils");
3132

32-
const Metrics = require("utils/Metrics");
33-
const LoginUtils = require("./login-utils");
33+
const Metrics = require("utils/Metrics"),
34+
Strings = require("strings");
3435

3536
const MS_IN_DAY = 10 * 24 * 60 * 60 * 1000;
3637
const TEN_MINUTES = 10 * 60 * 1000;
38+
const FREE_PLAN_VALIDITY_DAYS = 10000;
3739

3840
// the fallback salt is always a constant as this will only fail in rare circumstatnces and it needs to
3941
// be exactly same across versions of the app. Changing this will not affect the large majority of users and
@@ -351,8 +353,8 @@ define(function (require, exports, module) {
351353
const current = await getEffectiveEntitlements(false); // Get effective entitlements
352354

353355
// Check if we need to refresh
354-
const expiredPlanName = LoginUtils.validTillExpired(current, lastRecordedState);
355-
const hasChanged = LoginUtils.haveEntitlementsChanged(current, lastRecordedState);
356+
const expiredPlanName = KernalModeTrust.LoginUtils.validTillExpired(current, lastRecordedState);
357+
const hasChanged = KernalModeTrust.LoginUtils.haveEntitlementsChanged(current, lastRecordedState);
356358

357359
if (expiredPlanName || hasChanged) {
358360
console.log(`Entitlements monitor detected changes, Expired: ${expiredPlanName},` +
@@ -376,6 +378,37 @@ define(function (require, exports, module) {
376378
console.log('Entitlements monitor started (10-minute interval)');
377379
}
378380

381+
function _validateAndFilterEntitlements(entitlements) {
382+
if (!entitlements) {
383+
return;
384+
}
385+
386+
const currentDate = Date.now();
387+
388+
if(entitlements.plan && (!entitlements.plan.validTill || currentDate > entitlements.plan.validTill)) {
389+
entitlements.plan = {
390+
...entitlements.plan,
391+
paidSubscriber: false,
392+
name: Strings.USER_FREE_PLAN_NAME,
393+
validTill: Date.now() + (FREE_PLAN_VALIDITY_DAYS * MS_IN_DAY)
394+
};
395+
}
396+
397+
const featureEntitlements = entitlements.entitlements;
398+
if (!featureEntitlements) {
399+
return;
400+
}
401+
402+
for(const featureName in featureEntitlements) {
403+
const feature = featureEntitlements[featureName];
404+
if(feature && feature.validTill && currentDate > feature.validTill) {
405+
feature.activated = false;
406+
feature.upgradeToPlan = feature.upgradeToPlan || brackets.config.main_pro_plan;
407+
feature.subscribeURL = feature.subscribeURL || brackets.config.purchase_url;
408+
}
409+
}
410+
}
411+
379412
/**
380413
* Get effective entitlements for determining feature availability throughout the app.
381414
* This is the primary API that should be used across Phoenix to check entitlements and enable/disable features.
@@ -479,13 +512,12 @@ define(function (require, exports, module) {
479512
// User has active trial
480513
if (serverEntitlements && serverEntitlements.plan) {
481514
// Logged-in user with trial
515+
_validateAndFilterEntitlements(serverEntitlements); // will prune invalid entitlements
482516
if (serverEntitlements.plan.paidSubscriber) {
483517
// Already a paid subscriber, return as-is
484-
// todo we need to check and filter valid till for each fields that we are interested in.
485518
return serverEntitlements;
486519
}
487520
// Enhance entitlements for trial user
488-
// todo we need to prune and filter serverEntitlements valid till for each fields that we are interested in.
489521
// ie if any entitlement has valid till expired, we need to deactivate that entitlement
490522
return {
491523
...serverEntitlements,

src/services/login-utils.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@
2525

2626
define(function (require, exports, module) {
2727

28+
const KernalModeTrust = window.KernalModeTrust;
29+
if(!KernalModeTrust){
30+
// integrated extensions will have access to kernal mode, but not external extensions
31+
throw new Error("Login utils should have access to KernalModeTrust. Cannot boot without trust ring");
32+
}
33+
2834
/**
2935
* Check if any validTill time has expired
30-
*
36+
*
3137
* @param {Object|null} entitlements - Current entitlements object
3238
* @param {Object|null} lastRecordedEntitlement - Previously recorded entitlements
3339
* @returns {string|null} - Name of expired plan/entitlement or null if none expired
@@ -85,7 +91,7 @@ define(function (require, exports, module) {
8591

8692
/**
8793
* Check if entitlements have changed from last recorded state
88-
*
94+
*
8995
* @param {Object|null} current - Current entitlements object
9096
* @param {Object|null} last - Last recorded entitlements object
9197
* @returns {boolean} - True if entitlements have changed, false otherwise
@@ -129,7 +135,13 @@ define(function (require, exports, module) {
129135
return false;
130136
}
131137

132-
// Export functions
133-
exports.validTillExpired = validTillExpired;
134-
exports.haveEntitlementsChanged = haveEntitlementsChanged;
135-
});
138+
KernalModeTrust.LoginUtils = {
139+
validTillExpired,
140+
haveEntitlementsChanged
141+
};
142+
// Test only Export functions
143+
if(Phoenix.isTestWindow) {
144+
exports.validTillExpired = validTillExpired;
145+
exports.haveEntitlementsChanged = haveEntitlementsChanged;
146+
}
147+
});

src/services/profile-menu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ define(function (require, exports, module) {
436436
initials: profileData.profileIcon.initials,
437437
avatarColor: profileData.profileIcon.color,
438438
planClass: "user-plan-free",
439-
planName: "Free Plan",
439+
planName: Strings.USER_FREE_PLAN_NAME,
440440
titleText: "Ai Quota Used",
441441
usageText: "100 / 200 credits",
442442
usedPercent: 0,

0 commit comments

Comments
 (0)