Subscription.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { db, ObjectId } from '../../../../app/src/infrastructure/mongodb.js'
  2. import { expect } from 'chai'
  3. import { callbackifyClass } from '@overleaf/promise-utils'
  4. import SubscriptionUpdater from '../../../../app/src/Features/Subscription/SubscriptionUpdater.js'
  5. import PermissionsManager from '../../../../app/src/Features/Authorization/PermissionsManager.js'
  6. import SSOConfigManager from '../../../../modules/group-settings/app/src/sso/SSOConfigManager.mjs'
  7. import { Subscription as SubscriptionModel } from '../../../../app/src/models/Subscription.js'
  8. import { DeletedSubscription as DeletedSubscriptionModel } from '../../../../app/src/models/DeletedSubscription.js'
  9. import Modules from '../../../../app/src/infrastructure/Modules.js'
  10. class PromisifiedSubscription {
  11. constructor(options = {}) {
  12. this.admin_id = options.adminId || new ObjectId()
  13. this.overleaf = options.overleaf || {}
  14. this.groupPlan = options.groupPlan
  15. this.manager_ids = options.managerIds || [this.admin_id]
  16. this.member_ids = options.memberIds || []
  17. this.membersLimit = options.membersLimit || 0
  18. this.invited_emails = options.invitedEmails || []
  19. this.teamName = options.teamName
  20. this.teamInvites = options.teamInvites || []
  21. this.planCode = options.planCode
  22. this.recurlySubscription_id = options.recurlySubscription_id
  23. this.features = options.features
  24. this.ssoConfig = options.ssoConfig
  25. this.groupPolicy = options.groupPolicy
  26. }
  27. async ensureExists() {
  28. if (this._id) {
  29. return null
  30. }
  31. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  32. const subscription = await SubscriptionModel.findOneAndUpdate(
  33. { admin_id: this.admin_id },
  34. this,
  35. options
  36. ).exec()
  37. this._id = subscription._id
  38. }
  39. async get() {
  40. return await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  41. }
  42. async getWithGroupPolicy() {
  43. // eslint-disable-next-line no-restricted-syntax
  44. return await SubscriptionModel.findById(this._id)
  45. .populate('groupPolicy')
  46. .exec()
  47. }
  48. async setManagerIds(managerIds) {
  49. return await SubscriptionModel.findOneAndUpdate(
  50. { _id: new ObjectId(this._id) },
  51. { manager_ids: managerIds }
  52. )
  53. }
  54. async setSSOConfig(ssoConfig) {
  55. const subscription = await this.get()
  56. return await SSOConfigManager.promises.updateSubscriptionSSOConfig(
  57. subscription,
  58. ssoConfig
  59. )
  60. }
  61. async refreshUsersFeatures() {
  62. return await SubscriptionUpdater.promises.refreshUsersFeatures(this)
  63. }
  64. async enableManagedUsers() {
  65. await Modules.promises.hooks.fire('enableManagedUsers', this._id)
  66. }
  67. async enableFeatureSSO() {
  68. await SubscriptionModel.findOneAndUpdate(
  69. { _id: new ObjectId(this._id) },
  70. { 'features.groupSSO': true }
  71. ).exec()
  72. }
  73. async setValidatedSSO() {
  74. const doc = await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  75. const ssoConfigId = doc.ssoConfig
  76. return await db.ssoConfigs.findOneAndUpdate(
  77. { _id: ssoConfigId },
  78. { $set: { validated: true } }
  79. )
  80. }
  81. async setValidatedAndEnabledSSO() {
  82. const doc = await db.subscriptions.findOne({ _id: new ObjectId(this._id) })
  83. const ssoConfigId = doc.ssoConfig
  84. return await db.ssoConfigs.findOneAndUpdate(
  85. { _id: ssoConfigId },
  86. { $set: { enabled: true, validated: true } }
  87. )
  88. }
  89. async getEnrollmentForUser(user) {
  90. const [enrollment] = await Modules.promises.hooks.fire(
  91. 'getManagedUsersEnrollmentForUser',
  92. user
  93. )
  94. return enrollment
  95. }
  96. getCapabilities(groupPolicy) {
  97. return PermissionsManager.getUserCapabilities(groupPolicy)
  98. }
  99. async getUserValidationStatus(params) {
  100. return await PermissionsManager.promises.getUserValidationStatus(params)
  101. }
  102. async enrollManagedUser(user) {
  103. const subscription = await SubscriptionModel.findById(this._id).exec()
  104. return await Modules.promises.hooks.fire(
  105. 'enrollInManagedSubscription',
  106. user._id,
  107. subscription
  108. )
  109. }
  110. async expectDeleted(deleterData) {
  111. const deletedSubscriptions = await DeletedSubscriptionModel.find({
  112. 'subscription._id': this._id,
  113. }).exec()
  114. expect(deletedSubscriptions.length).to.equal(1)
  115. const deletedSubscription = deletedSubscriptions[0]
  116. expect(deletedSubscription.subscription.teamInvites).to.be.empty
  117. expect(deletedSubscription.subscription.invited_emails).to.be.empty
  118. expect(deletedSubscription.deleterData.deleterIpAddress).to.equal(
  119. deleterData.ip
  120. )
  121. if (deleterData.id) {
  122. expect(deletedSubscription.deleterData.deleterId.toString()).to.equal(
  123. deleterData.id.toString()
  124. )
  125. } else {
  126. expect(deletedSubscription.deleterData.deleterId).to.be.undefined
  127. }
  128. const subscription = await SubscriptionModel.findById(this._id).exec()
  129. expect(subscription).to.be.null
  130. }
  131. async addMember(userId) {
  132. return await SubscriptionModel.findOneAndUpdate(
  133. { _id: new ObjectId(this._id) },
  134. { $push: { member_ids: userId } }
  135. ).exec()
  136. }
  137. async inviteUser(adminUser, email) {
  138. await adminUser.login()
  139. return await adminUser.doRequest('POST', {
  140. url: `/manage/groups/${this._id}/invites`,
  141. json: {
  142. email,
  143. },
  144. })
  145. }
  146. }
  147. const Subscription = callbackifyClass(PromisifiedSubscription, {
  148. without: ['getCapabilities'],
  149. })
  150. Subscription.promises = class extends PromisifiedSubscription {}
  151. export default Subscription