From 37e874da4d3de5b5a0abec2832d8bc5a433dc2b6 Mon Sep 17 00:00:00 2001 From: James Irwin Date: Fri, 29 Mar 2024 22:21:39 +0100 Subject: [PATCH 1/3] Fix multiorg license report --- packages/back-end/src/controllers/license.ts | 15 ++++++++++++--- .../organizations/organizations.controller.ts | 4 ++-- .../License/DownloadLicenseUsageButton.tsx | 11 ++++++++--- .../components/License/ShowLicenseInfo.tsx | 3 ++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/back-end/src/controllers/license.ts b/packages/back-end/src/controllers/license.ts index b006c8c7e46..affac48976a 100644 --- a/packages/back-end/src/controllers/license.ts +++ b/packages/back-end/src/controllers/license.ts @@ -7,6 +7,7 @@ import { postResendEmailVerificationEmailToLicenseServer, postVerifyEmailToLicenseServer, } from "enterprise"; +import md5 from "md5"; import { getLicenseMetaData, initializeLicenseForOrg, @@ -14,7 +15,10 @@ import { import { getUserLicenseCodes } from "../services/users"; import { AuthRequest } from "../types/AuthRequest"; import { getContextFromReq } from "../services/organizations"; -import { updateOrganization } from "../models/OrganizationModel"; +import { + getAllInviteEmailsInDb, + updateOrganization, +} from "../models/OrganizationModel"; import { PrivateApiErrorResponse } from "../../types/api"; import { updateSubscriptionInDb } from "../services/stripe"; @@ -53,7 +57,11 @@ export async function getLicenseReport(req: AuthRequest, res: Response) { const timestamp = new Date().toISOString(); const licenseMetaData = await getLicenseMetaData(); - const userLicenseCodes = await getUserLicenseCodes(); + const userEmailCodes = await getUserLicenseCodes(); + const inviteEmails = await getAllInviteEmailsInDb(); + const inviteEmailCodes: string[] = inviteEmails.map((email) => { + return md5(email).slice(0, 8); + }); // Create a hmac signature of the license data const hmac = crypto.createHmac("sha256", licenseMetaData.installationId); @@ -61,7 +69,8 @@ export async function getLicenseReport(req: AuthRequest, res: Response) { const report = { timestamp, licenseMetaData, - userLicenseCodes, + userEmailCodes, + inviteEmailCodes, }; return res.status(200).json({ diff --git a/packages/back-end/src/routers/organizations/organizations.controller.ts b/packages/back-end/src/routers/organizations/organizations.controller.ts index 07b15bcb4d3..5f03c55d3f8 100644 --- a/packages/back-end/src/routers/organizations/organizations.controller.ts +++ b/packages/back-end/src/routers/organizations/organizations.controller.ts @@ -621,9 +621,9 @@ export async function getOrganization(req: AuthRequest, res: Response) { } = org; let license; - if (licenseKey) { + if (licenseKey || process.env.LICENSE_KEY) { // automatically set the license data based on org license key - license = getLicense(org.licenseKey); + license = getLicense(org.licenseKey || process.env.LICENSE_KEY); if (!license || (license.organizationId && license.organizationId !== id)) { try { license = await initializeLicenseForOrg(org); diff --git a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx index 12401399472..e408e0f9a69 100644 --- a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx +++ b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx @@ -15,7 +15,8 @@ const DownloadLicenseUsageButton: FC = () => { const res = await apiCall<{ status: number; licenseMetaData: LicenseMetaData; - userLicenseCodes: string[]; + userEmailCodes: string[]; + inviteEmailCodes: string[]; signature: string; timestamp: string; }>(`/license/report`, { @@ -32,8 +33,12 @@ const DownloadLicenseUsageButton: FC = () => { { license: license, licenseMetaData: res.licenseMetaData, - userLicenseCodes: res.userLicenseCodes, - seatsUsed: res.userLicenseCodes.length, + userEmailCodes: res.userEmailCodes, + inviteEmailCodes: res.inviteEmailCodes, + activeSeatsUsed: res.userEmailCodes.length, + seatsUsed: Array.from( + new Set(res.userEmailCodes.concat(res.inviteEmailCodes)) + ).length, signature: res.signature, timestamp: res.timestamp, }, diff --git a/packages/front-end/components/License/ShowLicenseInfo.tsx b/packages/front-end/components/License/ShowLicenseInfo.tsx index f90958d4707..fac7614cac7 100644 --- a/packages/front-end/components/License/ShowLicenseInfo.tsx +++ b/packages/front-end/components/License/ShowLicenseInfo.tsx @@ -7,6 +7,7 @@ import EditLicenseModal from "@/components/Settings/EditLicenseModal"; import { GBPremiumBadge } from "@/components/Icons"; import UpgradeModal from "@/components/Settings/UpgradeModal"; import AccountPlanNotices from "@/components/Layout/AccountPlanNotices"; +import { isCloud } from "@/services/env"; import RefreshLicenseButton from "./RefreshLicenseButton"; import DownloadLicenseUsageButton from "./DownloadLicenseUsageButton"; @@ -35,7 +36,7 @@ const ShowLicenseInfo: FC<{ // TODO: Remove this once we have migrated all organizations to use the license key const usesLicenseInfoOnModel = - !showUpgradeButton && !organization?.licenseKey; + isCloud() && !showUpgradeButton && !organization?.licenseKey; return (
From 9ffc784a00e21fdc180efa98246efcdca93bb6dd Mon Sep 17 00:00:00 2001 From: James Irwin Date: Sat, 30 Mar 2024 14:09:06 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Michael Samper --- .../src/routers/organizations/organizations.controller.ts | 2 +- .../components/License/DownloadLicenseUsageButton.tsx | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/back-end/src/routers/organizations/organizations.controller.ts b/packages/back-end/src/routers/organizations/organizations.controller.ts index 5f03c55d3f8..a1f0cd73526 100644 --- a/packages/back-end/src/routers/organizations/organizations.controller.ts +++ b/packages/back-end/src/routers/organizations/organizations.controller.ts @@ -623,7 +623,7 @@ export async function getOrganization(req: AuthRequest, res: Response) { let license; if (licenseKey || process.env.LICENSE_KEY) { // automatically set the license data based on org license key - license = getLicense(org.licenseKey || process.env.LICENSE_KEY); + license = getLicense(licenseKey || process.env.LICENSE_KEY); if (!license || (license.organizationId && license.organizationId !== id)) { try { license = await initializeLicenseForOrg(org); diff --git a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx index e408e0f9a69..97da744b02c 100644 --- a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx +++ b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx @@ -36,9 +36,8 @@ const DownloadLicenseUsageButton: FC = () => { userEmailCodes: res.userEmailCodes, inviteEmailCodes: res.inviteEmailCodes, activeSeatsUsed: res.userEmailCodes.length, - seatsUsed: Array.from( - new Set(res.userEmailCodes.concat(res.inviteEmailCodes)) - ).length, + seatsUsed: + new Set(res.userEmailCodes.concat(res.inviteEmailCodes).size, signature: res.signature, timestamp: res.timestamp, }, From 13498e86e9ec82c2a599d71e70b21d87dfb0926e Mon Sep 17 00:00:00 2001 From: James Irwin Date: Sat, 30 Mar 2024 14:11:39 +0100 Subject: [PATCH 3/3] Fix missing parenthesis --- .../components/License/DownloadLicenseUsageButton.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx index 97da744b02c..0290d07bb59 100644 --- a/packages/front-end/components/License/DownloadLicenseUsageButton.tsx +++ b/packages/front-end/components/License/DownloadLicenseUsageButton.tsx @@ -36,8 +36,9 @@ const DownloadLicenseUsageButton: FC = () => { userEmailCodes: res.userEmailCodes, inviteEmailCodes: res.inviteEmailCodes, activeSeatsUsed: res.userEmailCodes.length, - seatsUsed: - new Set(res.userEmailCodes.concat(res.inviteEmailCodes).size, + seatsUsed: new Set( + res.userEmailCodes.concat(res.inviteEmailCodes) + ).size, signature: res.signature, timestamp: res.timestamp, },