SubscriptionGroupController.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. SubscriptionGroupHandler = require("./SubscriptionGroupHandler")
  2. logger = require("logger-sharelatex")
  3. SubscriptionLocator = require("./SubscriptionLocator")
  4. ErrorsController = require("../Errors/ErrorController")
  5. SubscriptionDomainHandler = require("./SubscriptionDomainHandler")
  6. AuthenticationController = require('../Authentication/AuthenticationController')
  7. _ = require("underscore")
  8. async = require("async")
  9. module.exports =
  10. removeUserFromGroup: (req, res, next)->
  11. adminUserId = AuthenticationController.getLoggedInUserId(req)
  12. userToRemove_id = req.params.user_id
  13. getManagedSubscription adminUserId, (error, subscription) ->
  14. return next(error) if error?
  15. logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription"
  16. SubscriptionGroupHandler.removeUserFromGroup subscription._id, userToRemove_id, (err)->
  17. if err?
  18. logger.err err:err, adminUserId:adminUserId, userToRemove_id:userToRemove_id, "error removing user from group"
  19. return res.sendStatus 500
  20. res.send()
  21. removeSelfFromGroup: (req, res, next)->
  22. adminUserId = req.query.admin_user_id
  23. userToRemove_id = AuthenticationController.getLoggedInUserId(req)
  24. getManagedSubscription adminUserId, (error, subscription) ->
  25. return next(error) if error?
  26. logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription after self request"
  27. SubscriptionGroupHandler.removeUserFromGroup subscription._id, userToRemove_id, (err)->
  28. if err?
  29. logger.err err:err, userToRemove_id:userToRemove_id, adminUserId:adminUserId, "error removing self from group"
  30. return res.sendStatus 500
  31. res.send()
  32. exportGroupCsv: (req, res, next)->
  33. user_id = AuthenticationController.getLoggedInUserId(req)
  34. logger.log user_id: user_id, "exporting group csv"
  35. getManagedSubscription user_id, (err, subscription)->
  36. return next(error) if error?
  37. if !subscription.groupPlan
  38. return res.redirect("/")
  39. SubscriptionGroupHandler.getPopulatedListOfMembers subscription._id, (err, users)->
  40. groupCsv = ""
  41. for user in users
  42. groupCsv += user.email + "\n"
  43. res.header(
  44. "Content-Disposition",
  45. "attachment; filename=Group.csv"
  46. )
  47. res.contentType('text/csv')
  48. res.send(groupCsv)
  49. # legacy route
  50. redirectToSubscriptionGroupAdminPage: (req, res, next) ->
  51. user_id = AuthenticationController.getLoggedInUserId(req)
  52. getManagedSubscription user_id, (error, subscription) ->
  53. return next(error) if error?
  54. if !subscription?.groupPlan
  55. return res.redirect("/user/subscription")
  56. res.redirect("/manage/groups/#{subscription._id}/members")
  57. getManagedSubscription = (managerId, callback) ->
  58. SubscriptionLocator.findManagedSubscription managerId, (err, subscription)->
  59. if subscription?
  60. logger.log managerId: managerId, "got managed subscription"
  61. else
  62. err ||= new Error("No subscription found managed by user #{managerId}")
  63. return callback(err, subscription)