UserMembershipHandler.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-unused-vars,
  5. standard/no-callback-literal,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS101: Remove unnecessary use of Array.from
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const { ObjectId } = require('mongodb')
  17. const async = require('async')
  18. const { promisifyAll } = require('../../util/promises')
  19. const Errors = require('../Errors/Errors')
  20. const EntityModels = {
  21. Institution: require('../../models/Institution').Institution,
  22. Subscription: require('../../models/Subscription').Subscription,
  23. Publisher: require('../../models/Publisher').Publisher
  24. }
  25. const UserMembershipViewModel = require('./UserMembershipViewModel')
  26. const UserGetter = require('../User/UserGetter')
  27. const logger = require('logger-sharelatex')
  28. const UserMembershipEntityConfigs = require('./UserMembershipEntityConfigs')
  29. const UserMembershipHandler = {
  30. getEntityWithoutAuthorizationCheck(entityId, entityConfig, callback) {
  31. if (callback == null) {
  32. callback = function(error, entity) {}
  33. }
  34. const query = buildEntityQuery(entityId, entityConfig)
  35. return EntityModels[entityConfig.modelName].findOne(query, callback)
  36. },
  37. createEntity(entityId, entityConfig, callback) {
  38. if (callback == null) {
  39. callback = function(error, entity) {}
  40. }
  41. const data = buildEntityQuery(entityId, entityConfig)
  42. return EntityModels[entityConfig.modelName].create(data, callback)
  43. },
  44. getUsers(entity, entityConfig, callback) {
  45. if (callback == null) {
  46. callback = function(error, users) {}
  47. }
  48. const attributes = entityConfig.fields.read
  49. return getPopulatedListOfMembers(entity, attributes, callback)
  50. },
  51. addUser(entity, entityConfig, email, callback) {
  52. if (callback == null) {
  53. callback = function(error, user) {}
  54. }
  55. const attribute = entityConfig.fields.write
  56. return UserGetter.getUserByAnyEmail(email, function(error, user) {
  57. if (error != null) {
  58. return callback(error)
  59. }
  60. if (!user) {
  61. return callback({ userNotFound: true })
  62. }
  63. if (entity[attribute].some(managerId => managerId.equals(user._id))) {
  64. return callback({ alreadyAdded: true })
  65. }
  66. return addUserToEntity(entity, attribute, user, error =>
  67. callback(error, UserMembershipViewModel.build(user))
  68. )
  69. })
  70. },
  71. removeUser(entity, entityConfig, userId, callback) {
  72. if (callback == null) {
  73. callback = function(error) {}
  74. }
  75. const attribute = entityConfig.fields.write
  76. if (entity.admin_id != null ? entity.admin_id.equals(userId) : undefined) {
  77. return callback({ isAdmin: true })
  78. }
  79. return removeUserFromEntity(entity, attribute, userId, callback)
  80. }
  81. }
  82. UserMembershipHandler.promises = promisifyAll(UserMembershipHandler)
  83. module.exports = UserMembershipHandler
  84. var getPopulatedListOfMembers = function(entity, attributes, callback) {
  85. if (callback == null) {
  86. callback = function(error, users) {}
  87. }
  88. const userObjects = []
  89. for (let attribute of Array.from(attributes)) {
  90. for (let userObject of Array.from(entity[attribute] || [])) {
  91. // userObject can be an email as String, a user id as ObjectId or an
  92. // invite as Object with an email attribute as String. We want to pass to
  93. // UserMembershipViewModel either an email as (String) or a user id (ObjectId)
  94. const userIdOrEmail = userObject.email || userObject
  95. userObjects.push(userIdOrEmail)
  96. }
  97. }
  98. return async.map(userObjects, UserMembershipViewModel.buildAsync, callback)
  99. }
  100. var addUserToEntity = function(entity, attribute, user, callback) {
  101. if (callback == null) {
  102. callback = function(error) {}
  103. }
  104. const fieldUpdate = {}
  105. fieldUpdate[attribute] = user._id
  106. return entity.update({ $addToSet: fieldUpdate }, callback)
  107. }
  108. var removeUserFromEntity = function(entity, attribute, userId, callback) {
  109. if (callback == null) {
  110. callback = function(error) {}
  111. }
  112. const fieldUpdate = {}
  113. fieldUpdate[attribute] = userId
  114. return entity.update({ $pull: fieldUpdate }, callback)
  115. }
  116. var buildEntityQuery = function(entityId, entityConfig, loggedInUser) {
  117. if (ObjectId.isValid(entityId.toString())) {
  118. entityId = ObjectId(entityId)
  119. }
  120. const query = Object.assign({}, entityConfig.baseQuery)
  121. query[entityConfig.fields.primaryKey] = entityId
  122. return query
  123. }