#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
feat(licensing): take Team capacity from SaaS, with the licence as fallback (#7872)
ConnorYoha8c597da9550
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/AccountLinkClient.java b/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/AccountLinkClient.javaindex 558341745d..921bb1c3c0 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/AccountLinkClient.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/AccountLinkClient.java@@ -343,6 +343,8 @@ public class AccountLinkClient {Long periodCap =root.hasNonNull("periodCapUnits") ? root.get("periodCapUnits").asLong() : null;EntitlementState state = mapState(root.path("state").asText(null));+ Integer licensedUsers =+ root.hasNonNull("licensedUsers") ? root.get("licensedUsers").asInt() : null;return new InstanceEntitlement(subscribed,freeRemaining,@@ -351,7 +353,8 @@ public class AccountLinkClient {state,parseUnitCalcPolicy(root),parseDateTime(root, "periodStart"),- parseDateTime(root, "periodEnd"));+ parseDateTime(root, "periodEnd"),+ licensedUsers);}/** Parses the nested unit-calc policy; null if absent or any knob is invalid (e.g. zero). */diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/InstanceEntitlement.java b/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/InstanceEntitlement.javaindex 8760239957..d2da1f697e 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/InstanceEntitlement.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/accountlink/InstanceEntitlement.java@@ -31,7 +31,8 @@ public record InstanceEntitlement(EntitlementState state,UnitCalcPolicy unitCalcPolicy,LocalDateTime periodStart,- LocalDateTime periodEnd) {+ LocalDateTime periodEnd,+ Integer licensedUsers) {/** Gate-only view with no metering config — used by the revoked sentinel and gate tests. */public InstanceEntitlement(@@ -48,6 +49,7 @@ public record InstanceEntitlement(state,null,null,+ null,null);}}diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/service/UserLicenseSettingsService.java b/app/proprietary/src/main/java/stirling/software/proprietary/service/UserLicenseSettingsService.javaindex 085a9ffcf1..a299384f03 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/service/UserLicenseSettingsService.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/service/UserLicenseSettingsService.java@@ -18,6 +18,8 @@ import lombok.RequiredArgsConstructor;import lombok.extern.slf4j.Slf4j;import stirling.software.common.model.ApplicationProperties;+import stirling.software.proprietary.accountlink.EntitlementCache;+import stirling.software.proprietary.accountlink.InstanceEntitlement;import stirling.software.proprietary.model.UserLicenseSettings;import stirling.software.proprietary.security.configuration.ee.KeygenLicenseVerifier.License;import stirling.software.proprietary.security.configuration.ee.LicenseKeyChecker;@@ -51,6 +53,9 @@ public class UserLicenseSettingsService {private final ApplicationProperties applicationProperties;private final ObjectProvider<LicenseKeyChecker> licenseKeyChecker;+ /** Absent unless this instance is linked to SaaS (account-link disabled by default). */+ private final ObjectProvider<EntitlementCache> entitlementCache;+/*** Gets the current user license settings, creating them if they don't exist.*@@ -316,8 +321,16 @@ public class UserLicenseSettingsService {grandfatheredLimit = DEFAULT_USER_LIMIT;}- // No license: use grandfathered limit+ // A valid licence answers first. Team is moving to being sold on SaaS with no licence at+ // all, so in the end state only Enterprise holds one, and Enterprise should outrank SaaS:+ // it is contracted and has to keep working offline. Until then a legacy licence keeps+ // whatever it granted, and a customer worse off under it can simply remove it.if (!hasPaidLicense()) {+ Integer fromSaas = linkedTeamAllowance();+ if (fromSaas != null) {+ log.debug("No licence; linked team allowance: {} users", fromSaas);+ return fromSaas;+ }log.debug("No license: using grandfathered limit of {}", grandfatheredLimit);return grandfatheredLimit;}@@ -338,6 +351,25 @@ public class UserLicenseSettingsService {return licenseMaxUsers;}+ /**+ * Users this instance's linked team is entitled to, or null when SaaS is not the authority+ * here.+ *+ * <p>Only consulted when no licence is installed. Null covers three indistinguishable cases+ * that all fall through to the grandfathered limit: the instance is not linked, SaaS has never+ * answered, or it answered with no user limit — which is also what an older SaaS sends.+ *+ * <p>When SaaS is merely unreachable, {@link EntitlementCache} keeps serving the freshest+ * snapshot it has, so a linked instance holds its last known allowance rather than losing it.+ */+ private Integer linkedTeamAllowance() {+ EntitlementCache cache = entitlementCache.getIfAvailable();+ if (cache == null) {+ return null;+ }+ return cache.current().map(InstanceEntitlement::licensedUsers).orElse(null);+ }+/*** Checks if a user is eligible to use OAuth/SAML authentication.*diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/InstanceEntitlementInterceptorTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/InstanceEntitlementInterceptorTest.javaindex 082da85fd7..4d35d009c5 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/InstanceEntitlementInterceptorTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/InstanceEntitlementInterceptorTest.java@@ -194,7 +194,7 @@ class InstanceEntitlementInterceptorTest {private static InstanceEntitlement entitled(UnitCalcPolicy policy, LocalDateTime period) {return new InstanceEntitlement(- true, 0, 0, 100L, EntitlementState.OK, policy, period, period.plusMonths(1));+ true, 0, 0, 100L, EntitlementState.OK, policy, period, period.plusMonths(1), null);}private static byte[] fivePagePdf() throws Exception {diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/LocalUsageServiceTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/LocalUsageServiceTest.javaindex 4605348666..0234e4561b 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/LocalUsageServiceTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/accountlink/LocalUsageServiceTest.java@@ -41,7 +41,8 @@ class LocalUsageServiceTest {EntitlementState.OK,null,periodStart,- periodStart.plusMonths(1));+ periodStart.plusMonths(1),+ null);}@Testdiff --git a/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceMoreTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceMoreTest.javaindex e99b262ff7..b681740986 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceMoreTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceMoreTest.java@@ -21,6 +21,9 @@ import org.mockito.quality.Strictness;import org.springframework.beans.factory.ObjectProvider;import stirling.software.common.model.ApplicationProperties;+import stirling.software.proprietary.accountlink.EntitlementCache;+import stirling.software.proprietary.accountlink.EntitlementState;+import stirling.software.proprietary.accountlink.InstanceEntitlement;import stirling.software.proprietary.model.UserLicenseSettings;import stirling.software.proprietary.security.configuration.ee.KeygenLicenseVerifier.License;import stirling.software.proprietary.security.configuration.ee.LicenseKeyChecker;@@ -40,6 +43,7 @@ class UserLicenseSettingsServiceMoreTest {@Mock private UserService userService;@Mock private LicenseKeyChecker licenseKeyChecker;@Mock private ObjectProvider<LicenseKeyChecker> licenseKeyCheckerProvider;+ @Mock private ObjectProvider<EntitlementCache> entitlementCacheProvider;private ApplicationProperties applicationProperties;private UserLicenseSettingsService service;@@ -60,7 +64,8 @@ class UserLicenseSettingsServiceMoreTest {settingsRepository,userService,applicationProperties,- licenseKeyCheckerProvider);+ licenseKeyCheckerProvider,+ entitlementCacheProvider);}// Saves a freshly initialized + locked settings row with a valid signature.@@ -79,6 +84,83 @@ class UserLicenseSettingsServiceMoreTest {return s;}+ /**+ * A valid licence answers first; SaaS answers when there is none. Team is moving to being sold+ * with no licence at all, so in the end state only Enterprise holds one and it should outrank+ * SaaS -- it is contracted and must work offline. These pin that order and the fallbacks+ * beneath it.+ */+ @Nested+ class LinkedTeamAllowance {++ private void cacheReturns(InstanceEntitlement entitlement) {+ EntitlementCache cache = org.mockito.Mockito.mock(EntitlementCache.class);+ when(cache.current()).thenReturn(Optional.ofNullable(entitlement));+ when(entitlementCacheProvider.getIfAvailable()).thenReturn(cache);+ }++ private InstanceEntitlement withAllowance(Integer licensedUsers) {+ return new InstanceEntitlement(+ true, 0, 0, null, EntitlementState.OK, null, null, null, licensedUsers);+ }++ @Test+ @DisplayName("no licence: SaaS states the allowance")+ void saasAnswersWithoutALicence() {+ lockedSettings(7);+ cacheReturns(withAllowance(300));++ assertThat(service.calculateMaxAllowedUsers()).isEqualTo(300);+ }++ /**+ * The precedence decision. A legacy unlimited Server licence keeps what it granted rather+ * than being silently reduced to the subscription's number; a customer worse off under it+ * removes the licence.+ */+ @Test+ @DisplayName("a valid licence outranks the SaaS allowance")+ void licenceOutranksSaas() {+ lockedSettings(7);+ when(licenseKeyChecker.getPremiumLicenseEnabledResult()).thenReturn(License.SERVER);+ cacheReturns(withAllowance(100));++ // A SERVER licence with licenseMaxUsers = 0 is unlimited; it is not lowered to 100.+ assertThat(service.calculateMaxAllowedUsers()).isEqualTo(Integer.MAX_VALUE);+ }++ @Test+ @DisplayName("not linked: the grandfathered limit answers")+ void unlinkedFallsBackToGrandfathered() {+ lockedSettings(7);+ when(entitlementCacheProvider.getIfAvailable()).thenReturn(null);++ assertThat(service.calculateMaxAllowedUsers()).isEqualTo(7);+ }++ @Test+ @DisplayName("linked but SaaS has never answered: the grandfathered limit answers")+ void neverFetchedFallsBackToGrandfathered() {+ lockedSettings(7);+ cacheReturns(null);++ assertThat(service.calculateMaxAllowedUsers()).isEqualTo(7);+ }++ /**+ * Null is also what an older SaaS sends and what "no user limit" looks like, and the two+ * are indistinguishable — so it must fall through rather than cap the instance.+ */+ @Test+ @DisplayName("SaaS states no limit: the grandfathered limit answers")+ void noLimitFallsBackToGrandfathered() {+ lockedSettings(7);+ cacheReturns(withAllowance(null));++ assertThat(service.calculateMaxAllowedUsers()).isEqualTo(7);+ }+ }+@Nested@DisplayName("getOrCreateSettings")class GetOrCreateSettings {diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceTest.javaindex 3f0214573f..fe25862aac 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/service/UserLicenseSettingsServiceTest.java@@ -19,6 +19,7 @@ import org.mockito.quality.Strictness;import org.springframework.beans.factory.ObjectProvider;import stirling.software.common.model.ApplicationProperties;+import stirling.software.proprietary.accountlink.EntitlementCache;import stirling.software.proprietary.model.UserLicenseSettings;import stirling.software.proprietary.security.configuration.ee.KeygenLicenseVerifier.License;import stirling.software.proprietary.security.configuration.ee.LicenseKeyChecker;@@ -36,6 +37,7 @@ class UserLicenseSettingsServiceTest {@Mock private ApplicationProperties.AutomaticallyGenerated automaticallyGenerated;@Mock private LicenseKeyChecker licenseKeyChecker;@Mock private ObjectProvider<LicenseKeyChecker> licenseKeyCheckerProvider;+ @Mock private ObjectProvider<EntitlementCache> entitlementCacheProvider;private UserLicenseSettingsService service;private UserLicenseSettings mockSettings;@@ -66,7 +68,8 @@ class UserLicenseSettingsServiceTest {settingsRepository,userService,applicationProperties,- licenseKeyCheckerProvider) {+ licenseKeyCheckerProvider,+ entitlementCacheProvider) {@Overridepublic void validateSettingsIntegrity() {// Override to do nothing in tests - avoid HMAC signature validationdiff --git a/app/saas/src/main/java/stirling/software/saas/accountlink/InstanceController.java b/app/saas/src/main/java/stirling/software/saas/accountlink/InstanceController.javaindex 9c313a1dfa..ecb8d0ecd1 100644--- a/app/saas/src/main/java/stirling/software/saas/accountlink/InstanceController.java+++ b/app/saas/src/main/java/stirling/software/saas/accountlink/InstanceController.java@@ -21,6 +21,7 @@ import io.swagger.v3.oas.annotations.Hidden;import lombok.extern.slf4j.Slf4j;import stirling.software.proprietary.billing.UnitCalcPolicy;+import stirling.software.saas.model.SaasTeamExtensions;import stirling.software.saas.payg.billing.TeamBillingContext;import stirling.software.saas.payg.billing.TeamBillingService;import stirling.software.saas.payg.entitlement.EntitlementService;@@ -30,6 +31,7 @@ import stirling.software.saas.payg.model.BillingCategory;import stirling.software.saas.payg.model.EntitlementState;import stirling.software.saas.payg.policy.PricingPolicy;import stirling.software.saas.payg.policy.PricingPolicyService;+import stirling.software.saas.repository.SaasTeamExtensionsRepository;/*** Instance-facing surface (combined billing), authenticated by the <b>device credential</b> — not a@@ -58,6 +60,7 @@ public class InstanceController {private final PricingPolicyService pricingPolicyService;private final InstanceUsageIngestService usageIngestService;private final LinkedInstanceRepository linkedInstanceRepository;+ private final SaasTeamExtensionsRepository teamExtensionsRepository;public InstanceController(EntitlementService entitlementService,@@ -65,12 +68,14 @@ public class InstanceController {AccountLinkService accountLinkService,PricingPolicyService pricingPolicyService,InstanceUsageIngestService usageIngestService,- LinkedInstanceRepository linkedInstanceRepository) {+ LinkedInstanceRepository linkedInstanceRepository,+ SaasTeamExtensionsRepository teamExtensionsRepository) {this.entitlementService = entitlementService;this.billingService = billingService;this.accountLinkService = accountLinkService;this.pricingPolicyService = pricingPolicyService;this.usageIngestService = usageIngestService;+ this.teamExtensionsRepository = teamExtensionsRepository;this.linkedInstanceRepository = linkedInstanceRepository;}@@ -87,6 +92,11 @@ public class InstanceController {long periodSpendUnits,Long periodCapUnits,String state,+ // Users the team's Team plan covers, so the instance enforces the capacity the customer+ // bought rather than reading it from a licence. Null = no user limit, which is both a+ // team with no Team plan today and the historic unlimited licence; a limit is never+ // expressed as a sentinel, so no caller can do arithmetic on Integer.MAX_VALUE.+ Integer licensedUsers,// Metering inputs the instance needs to cost + bucket its own usage (Phase 2). The// instance computes units locally with this policy and resets its per-period cumulative// counters on the [periodStart, periodEnd) boundary.@@ -210,6 +220,7 @@ public class InstanceController {snap.periodSpendUnits(),snap.periodCapUnits(),coarseState(snap.state()),+ licensedUsers(teamId),new UnitCalcPolicy(policy.getDocPagesPerUnit(),policy.getDocBytesPerUnit(),@@ -219,6 +230,22 @@ public class InstanceController {snap.periodEnd());}+ /**+ * Users the team's Team plan covers, or null when it has no user limit.+ *+ * <p>Read from the seat cap the subscription writes, so the instance and the cloud team enforce+ * one number. {@code Integer.MAX_VALUE} is the historic "unlimited" sentinel and becomes null+ * here: the wire contract expresses no-limit as absence, so nothing downstream can accidentally+ * do arithmetic on it.+ */+ private Integer licensedUsers(Long teamId) {+ return teamExtensionsRepository+ .findByTeamId(teamId)+ .map(SaasTeamExtensions::getMaxSeats)+ .filter(max -> max != null && max > 0 && max < Integer.MAX_VALUE)+ .orElse(null);+ }+/*** Collapses the SaaS feature-state machine into the OK / OVER_LIMIT vocabulary the instance* gate parses. DEGRADED means automation + AI are gated off — which, for a gate that governsdiff --git a/app/saas/src/test/java/stirling/software/saas/accountlink/InstanceControllerTest.java b/app/saas/src/test/java/stirling/software/saas/accountlink/InstanceControllerTest.javaindex 4c9cc3b76a..07e8fd76ea 100644--- a/app/saas/src/test/java/stirling/software/saas/accountlink/InstanceControllerTest.java+++ b/app/saas/src/test/java/stirling/software/saas/accountlink/InstanceControllerTest.java@@ -36,6 +36,7 @@ import stirling.software.saas.payg.model.FeatureGate;import stirling.software.saas.payg.model.FeatureSet;import stirling.software.saas.payg.policy.PricingPolicy;import stirling.software.saas.payg.policy.PricingPolicyService;+import stirling.software.saas.repository.SaasTeamExtensionsRepository;/*** Pure-Mockito unit tests for {@link InstanceController} — the device-credential entitlement read.@@ -51,6 +52,7 @@ class InstanceControllerTest {@Mock private PricingPolicyService pricingPolicyService;@Mock private InstanceUsageIngestService usageIngestService;@Mock private LinkedInstanceRepository linkedInstanceRepository;+ @Mock private SaasTeamExtensionsRepository teamExtensionsRepository;private InstanceController controller() {return new InstanceController(@@ -59,7 +61,8 @@ class InstanceControllerTest {accountLinkService,pricingPolicyService,usageIngestService,- linkedInstanceRepository);+ linkedInstanceRepository,+ teamExtensionsRepository);}private static PricingPolicy policy() {