UserMembershipController.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. AuthenticationController = require('../Authentication/AuthenticationController')
  2. UserMembershipHandler = require('./UserMembershipHandler')
  3. EntityConfigs = require('./UserMembershipEntityConfigs')
  4. Errors = require('../Errors/Errors')
  5. EmailHelper = require("../Helpers/EmailHelper")
  6. logger = require("logger-sharelatex")
  7. module.exports =
  8. index: (req, res, next)->
  9. { entity, entityConfig } = req
  10. UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
  11. return next(error) if error?
  12. entityPrimaryKey = entity[entityConfig.fields.primaryKey].toString()
  13. res.render "user_membership/index",
  14. users: users
  15. groupSize: entity.membersLimit if entityConfig.hasMembersLimit
  16. translations: entityConfig.translations
  17. paths: entityConfig.pathsFor(entityPrimaryKey)
  18. add: (req, res, next)->
  19. { entity, entityConfig } = req
  20. email = EmailHelper.parseEmail(req.body.email)
  21. if !email?
  22. return res.status(400).json error:
  23. code: 'invalid_email'
  24. message: req.i18n.translate('invalid_email')
  25. if entityConfig.readOnly
  26. return next(new Errors.NotFoundError("Cannot add users to entity"))
  27. UserMembershipHandler.addUser entity, entityConfig, email, (error, user)->
  28. if error?.alreadyAdded
  29. return res.status(400).json error:
  30. code: 'user_already_added'
  31. message: req.i18n.translate('user_already_added')
  32. if error?.userNotFound
  33. return res.status(404).json error:
  34. code: 'user_not_found'
  35. message: req.i18n.translate('user_not_found')
  36. return next(error) if error?
  37. res.json(user: user)
  38. remove: (req, res, next)->
  39. { entity, entityConfig } = req
  40. userId = req.params.userId
  41. if entityConfig.readOnly
  42. return next(new Errors.NotFoundError("Cannot remove users from entity"))
  43. loggedInUserId = AuthenticationController.getLoggedInUserId(req)
  44. if loggedInUserId == userId
  45. return res.status(400).json error:
  46. code: 'managers_cannot_remove_self'
  47. message: req.i18n.translate('managers_cannot_remove_self')
  48. UserMembershipHandler.removeUser entity, entityConfig, userId, (error, user)->
  49. if error?.isAdmin
  50. return res.status(400).json error:
  51. code: 'managers_cannot_remove_admin'
  52. message: req.i18n.translate('managers_cannot_remove_admin')
  53. return next(error) if error?
  54. res.send()
  55. exportCsv: (req, res, next)->
  56. { entity, entityConfig } = req
  57. logger.log subscriptionId: entity._id, "exporting csv"
  58. UserMembershipHandler.getUsers entity, entityConfig, (error, users)->
  59. return next(error) if error?
  60. csvOutput = ""
  61. for user in users
  62. csvOutput += user.email + "\n"
  63. res.header(
  64. "Content-Disposition",
  65. "attachment; filename=Group.csv"
  66. )
  67. res.contentType('text/csv')
  68. res.send(csvOutput)