Subscription.mjs 6.0 KB

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