mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-12 07:30:46 +02:00
b8d279c74b
GitOrigin-RevId: 4e9d3176b4b5a5504afc102e569a27d7788864a3
118 lines
3.0 KiB
JavaScript
118 lines
3.0 KiB
JavaScript
// ts-check
|
|
import SubscriptionGroupHandler from './SubscriptionGroupHandler.js'
|
|
|
|
import OError from '@overleaf/o-error'
|
|
import logger from '@overleaf/logger'
|
|
import SubscriptionLocator from './SubscriptionLocator.js'
|
|
import SessionManager from '../Authentication/SessionManager.js'
|
|
import UserAuditLogHandler from '../User/UserAuditLogHandler.js'
|
|
import { expressify } from '@overleaf/promise-utils'
|
|
import Modules from '../../infrastructure/Modules.js'
|
|
|
|
/**
|
|
* @import { Subscription } from "../../../../types/subscription/dashboard/subscription"
|
|
*/
|
|
|
|
/**
|
|
* @param {import("express").Request} req
|
|
* @param {import("express").Response} res
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function removeUserFromGroup(req, res) {
|
|
const subscription = req.entity
|
|
const userToRemoveId = req.params.user_id
|
|
const loggedInUserId = SessionManager.getLoggedInUserId(req.session)
|
|
const subscriptionId = subscription._id
|
|
logger.debug(
|
|
{ subscriptionId, userToRemoveId },
|
|
'removing user from group subscription'
|
|
)
|
|
|
|
await _removeUserFromGroup(req, res, {
|
|
userToRemoveId,
|
|
loggedInUserId,
|
|
subscription,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @param {import("express").Request} req
|
|
* @param {import("express").Response} res
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function removeSelfFromGroup(req, res) {
|
|
const userToRemoveId = SessionManager.getLoggedInUserId(req.session)
|
|
const subscription = await SubscriptionLocator.promises.getSubscription(
|
|
req.query.subscriptionId
|
|
)
|
|
|
|
await _removeUserFromGroup(req, res, {
|
|
userToRemoveId,
|
|
loggedInUserId: userToRemoveId,
|
|
subscription,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* @param {import("express").Request} req
|
|
* @param {import("express").Response} res
|
|
* @param {string} userToRemoveId
|
|
* @param {string} loggedInUserId
|
|
* @param {Subscription} subscription
|
|
* @returns {Promise<void>}
|
|
* @private
|
|
*/
|
|
async function _removeUserFromGroup(
|
|
req,
|
|
res,
|
|
{ userToRemoveId, loggedInUserId, subscription }
|
|
) {
|
|
const subscriptionId = subscription._id
|
|
|
|
const groupSSOActive = (
|
|
await Modules.promises.hooks.fire('hasGroupSSOEnabled', subscription)
|
|
)?.[0]
|
|
if (groupSSOActive) {
|
|
await Modules.promises.hooks.fire(
|
|
'unlinkUserFromGroupSSO',
|
|
userToRemoveId,
|
|
subscriptionId
|
|
)
|
|
}
|
|
|
|
try {
|
|
await UserAuditLogHandler.promises.addEntry(
|
|
userToRemoveId,
|
|
'remove-from-group-subscription',
|
|
loggedInUserId,
|
|
req.ip,
|
|
{ subscriptionId }
|
|
)
|
|
} catch (auditLogError) {
|
|
throw OError.tag(auditLogError, 'error adding audit log entry', {
|
|
userToRemoveId,
|
|
subscriptionId,
|
|
})
|
|
}
|
|
|
|
try {
|
|
await SubscriptionGroupHandler.promises.removeUserFromGroup(
|
|
subscriptionId,
|
|
userToRemoveId
|
|
)
|
|
} catch (error) {
|
|
logger.err(
|
|
{ err: error, userToRemoveId, subscriptionId },
|
|
'error removing self from group'
|
|
)
|
|
return res.sendStatus(500)
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
}
|
|
|
|
export default {
|
|
removeUserFromGroup: expressify(removeUserFromGroup),
|
|
removeSelfFromGroup: expressify(removeSelfFromGroup),
|
|
}
|