Merge pull request #14102 from overleaf/ab-disable-managed-users

[web] Disable managed users from admin panel

GitOrigin-RevId: 04328fe0caf81cacc42d05100f6400d5dfb748ce
This commit is contained in:
Alexandre Bourdin 2023-08-03 12:05:56 +02:00 committed by Copybot
parent bf2e33ec83
commit d981ac2279
2 changed files with 38 additions and 1 deletions

View file

@ -8,6 +8,8 @@ const {
UserNotFoundError,
SubscriptionNotFoundError,
} = require('../Errors/Errors')
const UserGetter = require('../User/UserGetter')
const UserUpdater = require('../User/UserUpdater')
/**
* This module contains functions for handling managed users in a
@ -36,11 +38,39 @@ async function enableManagedUsers(subscriptionId) {
await subscription.save()
}
/**
* Disables managed users for a given subscription by removing the
* group policy and deleting enrolment information for all managed users.
* @async
* @function
* @param {string} subscriptionId - The ID of the subscription to disable
* managed users for.
* @returns {Promise<void>} - A Promise that resolves when the subscription and
* users have been updated.
*/
async function disableManagedUsers(subscriptionId) {
const subscription = await Subscription.findById(subscriptionId).exec()
for (const userId of subscription.member_ids || []) {
const user = await UserGetter.promises.getUser(userId, { enrollment: 1 })
if (
user.enrollment?.managedBy?.toString() === subscription._id.toString()
) {
await UserUpdater.promises.updateUser(userId, {
$unset: { enrollment: 1 },
})
}
}
subscription.groupPolicy = undefined
await subscription.save()
}
/**
* Retrieves the group policy for a user enrolled in a managed group.
* @async
* @function
* @param {Object} user - The user object to retrieve the group policy for.
* @param {Object} requestedUser - The user object to retrieve the group policy for.
* @returns {Promise<Object>} - A Promise that resolves with the group policy
* and subscription objects for the user's enrollment, or null if it does not exist.
*/
@ -118,10 +148,12 @@ async function enrollInSubscription(userId, subscription) {
module.exports = {
promises: {
enableManagedUsers,
disableManagedUsers,
getEnrollmentForUser,
enrollInSubscription,
},
enableManagedUsers: callbackify(enableManagedUsers),
getEnrollmentForUser: callbackify(getEnrollmentForUser),
enrollInSubscription: callbackify(enrollInSubscription),
disableManagedUsers: callbackify(disableManagedUsers),
}

View file

@ -40,6 +40,10 @@ type Recurly = {
pendingTotalLicenses?: number
}
export type GroupPolicy = {
[policy: string]: boolean
}
export type Subscription = {
_id: string
admin_id: string
@ -47,6 +51,7 @@ export type Subscription = {
member_ids: string[]
invited_emails: string[]
groupPlan: boolean
groupPolicy?: GroupPolicy
membersLimit: number
teamInvites: object[]
planCode: string