Skip to content

Commit

Permalink
feat: License v3 validations
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Oct 3, 2023
1 parent 3fd0bc4 commit 5b24b16
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 52 deletions.
17 changes: 17 additions & 0 deletions apps/meteor/ee/app/license/server/startup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cronJobs } from '@rocket.chat/cron';
import { License } from '@rocket.chat/license';
import { Subscriptions, Users } from '@rocket.chat/models';

Expand Down Expand Up @@ -26,3 +27,19 @@ License.setLicenseLimitCounter('privateApps', () => getAppCount('private'));
License.setLicenseLimitCounter('marketplaceApps', () => getAppCount('marketplace'));
// #TODO: Get real value
License.setLicenseLimitCounter('monthlyActiveContacts', async () => 0);

const updateCronJob = async () => {
if (License.hasValidLicense() === (await cronJobs.has('licenseLimitChecker'))) {
return;
}

if (License.hasValidLicense()) {
await cronJobs.add('licenseLimitChecker', '*/30 * * * *', () => License.revalidateLimits());
} else {
await cronJobs.remove('licenseLimitChecker');
}
};

License.onValidateLicense(async () => updateCronJob());
License.onInvalidateLicense(async () => updateCronJob());
void updateCronJob();
8 changes: 7 additions & 1 deletion ee/packages/license/src/definition/LicenseBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { LicenseModule } from './LicenseModule';

export type LicenseBehavior = 'invalidate_license' | 'start_fair_policy' | 'prevent_action' | 'prevent_installation' | 'disable_modules';
export type LicenseBehavior =
| 'invalidate_license'
| 'start_fair_policy'
| 'prevent_action'
| 'prevent_installation'
| 'disable_modules'
| 'custom';

export type BehaviorWithContext = {
behavior: LicenseBehavior;
Expand Down
26 changes: 24 additions & 2 deletions ee/packages/license/src/events/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LicenseLimitKind } from '../definition/ILicenseV3';
import type { LicenseBehavior } from '../definition/LicenseBehavior';
import type { LicenseModule } from '../definition/LicenseModule';
import type { LicenseManager } from '../license';
import { logger } from '../logger';
Expand All @@ -21,10 +22,31 @@ export function moduleRemoved(this: LicenseManager, module: LicenseModule) {
}
}

export function limitReached(this: LicenseManager, limitKind: LicenseLimitKind) {
export function limitReached(
this: LicenseManager,
limitKind: Exclude<LicenseLimitKind, 'roomsPerGuest'>,
limitBehavior: Exclude<LicenseBehavior, 'prevent_installation'>,
) {
try {
this.emit(`limitReached:${limitKind}`);
// This will never be emitted for limits that fallback to "not reached" when missing context params (eg: roomsPerGuest)
this.emit(`limitReached:${limitKind}:${limitBehavior}`);
} catch (error) {
logger.error({ msg: 'Error running limit reached event', error });
}
}

export function licenseValidated(this: LicenseManager) {
try {
this.emit('validate');
} catch (error) {
logger.error({ msg: 'Error running license validated event', error });
}
}

export function licenseInvalidated(this: LicenseManager) {
try {
this.emit('invalidate');
} catch (error) {
logger.error({ msg: 'Error running license invalidated event', error });
}
}
16 changes: 11 additions & 5 deletions ee/packages/license/src/events/listeners.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { LicenseLimitKind } from '../definition/ILicenseV3';
import type { LicenseBehavior } from '../definition/LicenseBehavior';
import type { LicenseModule } from '../definition/LicenseModule';
import type { LicenseManager } from '../license';
import { hasModule } from '../modules';
Expand Down Expand Up @@ -58,18 +59,23 @@ export function onToggledFeature(
};
}

export function onModule(this: LicenseManager, cb: (...args: any[]) => void) {
export function onModule(this: LicenseManager, cb: (data: { module: LicenseModule; valid: boolean }) => void) {
this.on('module', cb);
}

export function onValidateLicense(this: LicenseManager, cb: (...args: any[]) => void) {
export function onValidateLicense(this: LicenseManager, cb: () => void) {
this.on('validate', cb);
}

export function onInvalidateLicense(this: LicenseManager, cb: (...args: any[]) => void) {
export function onInvalidateLicense(this: LicenseManager, cb: () => void) {
this.on('invalidate', cb);
}

export function onLimitReached(this: LicenseManager, limitKind: LicenseLimitKind, cb: (...args: any[]) => void) {
this.on(`limitReached:${limitKind}`, cb);
export function onLimitReached(
this: LicenseManager,
limitKind: Exclude<LicenseLimitKind, 'roomsPerGuest'>,
cb: () => void,
limitBehavior: Exclude<LicenseBehavior, 'prevent_installation'> = 'custom',
) {
this.on(`limitReached:${limitKind}:${limitBehavior}`, cb);
}
17 changes: 14 additions & 3 deletions ee/packages/license/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ILicenseV3, LicenseLimitKind } from './definition/ILicenseV3';
import type { LicenseBehavior } from './definition/LicenseBehavior';
import type { LicenseModule } from './definition/LicenseModule';
import type { LimitContext } from './definition/LimitContext';
import { getAppsConfig, getMaxActiveUsers, getUnmodifiedLicenseAndModules } from './deprecated';
Expand Down Expand Up @@ -37,14 +38,20 @@ interface License {
overwriteClassOnLicense: typeof overwriteClassOnLicense;
setLicenseLimitCounter: typeof setLicenseLimitCounter;
getCurrentValueForLicenseLimit: typeof getCurrentValueForLicenseLimit;
isLimitReached: <T extends LicenseLimitKind>(action: T, context?: Partial<LimitContext<T>>) => Promise<boolean>;
isLimitReached: <T extends LicenseLimitKind>(
action: T,
behaviors: LicenseBehavior[],
context?: Partial<LimitContext<T>>,
) => Promise<boolean>;
onValidFeature: typeof onValidFeature;
onInvalidFeature: typeof onInvalidFeature;
onToggledFeature: typeof onToggledFeature;
onModule: typeof onModule;
onValidateLicense: typeof onValidateLicense;
onInvalidateLicense: typeof onInvalidateLicense;
onLimitReached: typeof onLimitReached;
revalidateLicense: () => Promise<void>;
revalidateLimits: () => Promise<void>;

getInfo: (loadCurrentValues: boolean) => Promise<{
license: ILicenseV3 | undefined;
Expand Down Expand Up @@ -78,8 +85,12 @@ export class LicenseImp extends LicenseManager implements License {

getCurrentValueForLicenseLimit = getCurrentValueForLicenseLimit;

public async isLimitReached<T extends LicenseLimitKind>(action: T, context?: Partial<LimitContext<T>>) {
return this.shouldPreventAction(action, context, 0);
public async isLimitReached<T extends LicenseLimitKind>(
action: T,
behaviors: LicenseBehavior[],
context?: Partial<LimitContext<T>>,
): Promise<boolean> {
return super.isLimitReached(action, behaviors, context);
}

onValidFeature = onValidFeature;
Expand Down
Loading

0 comments on commit 5b24b16

Please sign in to comment.