UserMembershipController.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const AuthenticationController = require('../Authentication/AuthenticationController')
  14. const UserMembershipHandler = require('./UserMembershipHandler')
  15. const Errors = require('../Errors/Errors')
  16. const EmailHelper = require('../Helpers/EmailHelper')
  17. const logger = require('logger-sharelatex')
  18. module.exports = {
  19. index(req, res, next) {
  20. const { entity, entityConfig } = req
  21. return entity.fetchV1Data(function(error, entity) {
  22. if (error != null) {
  23. return next(error)
  24. }
  25. return UserMembershipHandler.getUsers(entity, entityConfig, function(
  26. error,
  27. users
  28. ) {
  29. let entityName
  30. if (error != null) {
  31. return next(error)
  32. }
  33. const entityPrimaryKey = entity[
  34. entityConfig.fields.primaryKey
  35. ].toString()
  36. if (entityConfig.fields.name) {
  37. entityName = entity[entityConfig.fields.name]
  38. }
  39. return res.render('user_membership/index', {
  40. name: entityName,
  41. users,
  42. groupSize: entityConfig.hasMembersLimit
  43. ? entity.membersLimit
  44. : undefined,
  45. translations: entityConfig.translations,
  46. paths: entityConfig.pathsFor(entityPrimaryKey)
  47. })
  48. })
  49. })
  50. },
  51. add(req, res, next) {
  52. const { entity, entityConfig } = req
  53. const email = EmailHelper.parseEmail(req.body.email)
  54. if (email == null) {
  55. return res.status(400).json({
  56. error: {
  57. code: 'invalid_email',
  58. message: req.i18n.translate('invalid_email')
  59. }
  60. })
  61. }
  62. if (entityConfig.readOnly) {
  63. return next(new Errors.NotFoundError('Cannot add users to entity'))
  64. }
  65. return UserMembershipHandler.addUser(entity, entityConfig, email, function(
  66. error,
  67. user
  68. ) {
  69. if (error != null ? error.alreadyAdded : undefined) {
  70. return res.status(400).json({
  71. error: {
  72. code: 'user_already_added',
  73. message: req.i18n.translate('user_already_added')
  74. }
  75. })
  76. }
  77. if (error != null ? error.userNotFound : undefined) {
  78. return res.status(404).json({
  79. error: {
  80. code: 'user_not_found',
  81. message: req.i18n.translate('user_not_found')
  82. }
  83. })
  84. }
  85. if (error != null) {
  86. return next(error)
  87. }
  88. return res.json({ user })
  89. })
  90. },
  91. remove(req, res, next) {
  92. const { entity, entityConfig } = req
  93. const { userId } = req.params
  94. if (entityConfig.readOnly) {
  95. return next(new Errors.NotFoundError('Cannot remove users from entity'))
  96. }
  97. const loggedInUserId = AuthenticationController.getLoggedInUserId(req)
  98. if (loggedInUserId === userId) {
  99. return res.status(400).json({
  100. error: {
  101. code: 'managers_cannot_remove_self',
  102. message: req.i18n.translate('managers_cannot_remove_self')
  103. }
  104. })
  105. }
  106. return UserMembershipHandler.removeUser(
  107. entity,
  108. entityConfig,
  109. userId,
  110. function(error, user) {
  111. if (error != null ? error.isAdmin : undefined) {
  112. return res.status(400).json({
  113. error: {
  114. code: 'managers_cannot_remove_admin',
  115. message: req.i18n.translate('managers_cannot_remove_admin')
  116. }
  117. })
  118. }
  119. if (error != null) {
  120. return next(error)
  121. }
  122. return res.send()
  123. }
  124. )
  125. },
  126. exportCsv(req, res, next) {
  127. const { entity, entityConfig } = req
  128. logger.log({ subscriptionId: entity._id }, 'exporting csv')
  129. return UserMembershipHandler.getUsers(entity, entityConfig, function(
  130. error,
  131. users
  132. ) {
  133. if (error != null) {
  134. return next(error)
  135. }
  136. let csvOutput = ''
  137. for (let user of Array.from(users)) {
  138. csvOutput += user.email + '\n'
  139. }
  140. res.header('Content-Disposition', 'attachment; filename=Group.csv')
  141. res.contentType('text/csv')
  142. return res.send(csvOutput)
  143. })
  144. },
  145. new(req, res, next) {
  146. return res.render('user_membership/new', {
  147. entityName: req.params.name,
  148. entityId: req.params.id
  149. })
  150. },
  151. create(req, res, next) {
  152. const entityId = req.params.id
  153. const entityConfig = req.entityConfig
  154. return UserMembershipHandler.createEntity(entityId, entityConfig, function(
  155. error,
  156. entity
  157. ) {
  158. if (error != null) {
  159. return next(error)
  160. }
  161. return res.redirect(entityConfig.pathsFor(entityId).index)
  162. })
  163. }
  164. }