From 7268e0f0aaf3a1c3822801df3ec6c7b4475ac7da Mon Sep 17 00:00:00 2001 From: Alexandre Bourdin Date: Fri, 6 Oct 2023 10:59:37 +0200 Subject: [PATCH] Merge pull request #15084 from overleaf/ab-managers-users-ga [web] Managed users general availability prep GitOrigin-RevId: 9ab286a67c15f67e5d7617db506547b6ead8a9cb --- .../Subscription/SubscriptionController.js | 32 ++++++++++++++----- services/web/app/src/models/Subscription.js | 4 +-- .../dashboard/managed-group-subscriptions.tsx | 7 ++-- ...ubscriptions_managed_users_feature_flag.js | 17 ++++++++++ 4 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 services/web/migrations/20230928092537_backfill_subscriptions_managed_users_feature_flag.js diff --git a/services/web/app/src/Features/Subscription/SubscriptionController.js b/services/web/app/src/Features/Subscription/SubscriptionController.js index 0c98a253ce..fa18ac1743 100644 --- a/services/web/app/src/Features/Subscription/SubscriptionController.js +++ b/services/web/app/src/Features/Subscription/SubscriptionController.js @@ -18,8 +18,9 @@ const { expressify } = require('../../util/promises') const OError = require('@overleaf/o-error') const SplitTestHandler = require('../SplitTests/SplitTestHandler') const SubscriptionHelper = require('./SubscriptionHelper') -const Features = require('../../infrastructure/Features') const AuthorizationManager = require('../Authorization/AuthorizationManager') +const Modules = require('../../infrastructure/Modules') +const async = require('async') const groupPlanModalOptions = Settings.groupPlanModalOptions const validGroupPlanModalOptions = { @@ -251,15 +252,30 @@ async function userSubscriptionPage(req, res) { const groupPlansDataForDash = formatGroupPlansDataForDash() // display the Group Settings button only to admins of group subscriptions with the Managed Users feature available - const groupSettingsEnabledFor = (managedGroupSubscriptions || []) - .filter( - subscription => - Features.hasFeature('saas') && - subscription?.features?.managedUsers && - (subscription.admin_id._id || subscription.admin_id).toString() === + let groupSettingsEnabledFor + try { + const managedGroups = await async.filter( + managedGroupSubscriptions || [], + async subscription => { + const results = await Modules.promises.hooks.fire( + 'hasManagedUsersFeature', + subscription + ) + const isGroupAdmin = + (subscription.admin_id._id || subscription.admin_id).toString() === user._id.toString() + return results?.[0] === true && isGroupAdmin + } ) - .map(subscription => subscription._id.toString()) + groupSettingsEnabledFor = managedGroups.map(subscription => + subscription._id.toString() + ) + } catch (error) { + logger.error( + { err: error }, + 'Failed to list groups with group settings enabled' + ) + } const data = { title: 'your_subscription', diff --git a/services/web/app/src/models/Subscription.js b/services/web/app/src/models/Subscription.js index 77e3be8799..510df031d7 100644 --- a/services/web/app/src/models/Subscription.js +++ b/services/web/app/src/models/Subscription.js @@ -32,8 +32,8 @@ const SubscriptionSchema = new Schema( membersLimit: { type: Number, default: 0 }, customAccount: Boolean, features: { - managedUsers: { type: Boolean, default: false }, - groupSSO: { type: Boolean, default: false }, + managedUsers: { type: Boolean, default: null }, + groupSSO: { type: Boolean, default: null }, }, overleaf: { id: { diff --git a/services/web/frontend/js/features/subscription/components/dashboard/managed-group-subscriptions.tsx b/services/web/frontend/js/features/subscription/components/dashboard/managed-group-subscriptions.tsx index de06196d8e..2aa38b4c73 100644 --- a/services/web/frontend/js/features/subscription/components/dashboard/managed-group-subscriptions.tsx +++ b/services/web/frontend/js/features/subscription/components/dashboard/managed-group-subscriptions.tsx @@ -11,7 +11,10 @@ export default function ManagedGroupSubscriptions() { return null } - const groupSettingsEnabledFor = getMeta('ol-groupSettingsEnabledFor', []) + const groupSettingsEnabledFor = getMeta( + 'ol-groupSettingsEnabledFor', + [] + ) as string[] return ( <> @@ -52,7 +55,7 @@ export default function ManagedGroupSubscriptions() { subtext={t('manage_managers_subtext')} icon="manage_accounts" /> - {groupSettingsEnabledFor.includes(subscription._id) && ( + {groupSettingsEnabledFor?.includes(subscription._id) && ( { + const { db } = client + await db.subscriptions.updateMany( + { 'features.managedUsers': { $ne: true } }, + { $set: { 'features.managedUsers': null } } + ) +} + +exports.rollback = async client => { + const { db } = client + await db.subscriptions.updateMany( + { 'features.managedUsers': { $eq: null } }, + { $set: { 'features.managedUsers': false } } + ) +}