2014-02-12 05:23:40 -05:00
|
|
|
SubscriptionGroupHandler = require("./SubscriptionGroupHandler")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2015-05-27 15:50:16 -04:00
|
|
|
ErrorsController = require("../Errors/ErrorController")
|
2015-05-27 15:57:54 -04:00
|
|
|
SubscriptionDomainHandler = require("./SubscriptionDomainHandler")
|
2016-09-05 10:58:31 -04:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
2015-05-27 10:50:28 -04:00
|
|
|
_ = require("underscore")
|
2016-03-22 12:59:47 -04:00
|
|
|
async = require("async")
|
2015-05-22 08:57:15 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
module.exports =
|
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
addUserToGroup: (req, res, next)->
|
2016-09-05 10:58:31 -04:00
|
|
|
adminUserId = AuthenticationController.getLoggedInUserId(req)
|
2016-02-25 09:15:56 -05:00
|
|
|
newEmail = req.body?.email?.toLowerCase()?.trim()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionLocator.getManagedSubscription adminUserId, (error, subscription) ->
|
|
|
|
return next(error) if error?
|
|
|
|
|
|
|
|
logger.log adminUserId:adminUserId, newEmail:newEmail, "adding user to group subscription"
|
|
|
|
|
|
|
|
SubscriptionGroupHandler.addUserToGroup subscription._id, newEmail, (err, user)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, newEmail:newEmail, adminUserId:adminUserId, "error adding user from group"
|
|
|
|
return res.sendStatus 500
|
|
|
|
result =
|
|
|
|
user:user
|
|
|
|
if err and err.limitReached
|
|
|
|
result.limitReached = true
|
|
|
|
res.json(result)
|
|
|
|
|
|
|
|
removeUserFromGroup: (req, res, next)->
|
2016-09-05 10:58:31 -04:00
|
|
|
adminUserId = AuthenticationController.getLoggedInUserId(req)
|
2014-02-12 05:23:40 -05:00
|
|
|
userToRemove_id = req.params.user_id
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionLocator.getManagedSubscription adminUserId, (error, subscription) ->
|
|
|
|
return next(error) if error?
|
|
|
|
logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription"
|
|
|
|
SubscriptionGroupHandler.removeUserFromGroup subscription._id, userToRemove_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, adminUserId:adminUserId, userToRemove_id:userToRemove_id, "error removing user from group"
|
|
|
|
return res.sendStatus 500
|
|
|
|
res.send()
|
2016-09-05 10:58:31 -04:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
removeSelfFromGroup: (req, res, next)->
|
2016-02-19 07:27:01 -05:00
|
|
|
adminUserId = req.query.admin_user_id
|
2016-09-05 10:58:31 -04:00
|
|
|
userToRemove_id = AuthenticationController.getLoggedInUserId(req)
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionLocator.getManagedSubscription adminUserId, (error, subscription) ->
|
|
|
|
return next(error) if error?
|
|
|
|
logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription after self request"
|
|
|
|
SubscriptionGroupHandler.removeUserFromGroup subscription._id, userToRemove_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, userToRemove_id:userToRemove_id, adminUserId:adminUserId, "error removing self from group"
|
|
|
|
return res.sendStatus 500
|
|
|
|
res.send()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-11 04:31:57 -04:00
|
|
|
renderSubscriptionGroupAdminPage: (req, res, next)->
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionLocator.getManagedSubscription user_id, (error, subscription)->
|
|
|
|
return next(error) if error?
|
2017-06-22 09:07:07 -04:00
|
|
|
if !subscription?.groupPlan
|
|
|
|
return res.redirect("/user/subscription")
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionGroupHandler.getPopulatedListOfMembers subscription._id, (err, users)->
|
2014-02-12 05:23:40 -05:00
|
|
|
res.render "subscriptions/group_admin",
|
2014-08-01 08:47:14 -04:00
|
|
|
title: 'group_admin'
|
2014-02-12 05:23:40 -05:00
|
|
|
users: users
|
|
|
|
subscription: subscription
|
2015-05-22 08:57:15 -04:00
|
|
|
|
2015-05-28 15:54:41 -04:00
|
|
|
exportGroupCsv: (req, res)->
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2015-05-28 15:54:41 -04:00
|
|
|
logger.log user_id: user_id, "exporting group csv"
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionLocator.getManagedSubscription user_id, (err, subscription)->
|
|
|
|
return next(error) if error?
|
2018-07-11 07:10:38 -04:00
|
|
|
if !subscription.groupPlan
|
|
|
|
return res.redirect("/")
|
2018-07-11 04:31:57 -04:00
|
|
|
SubscriptionGroupHandler.getPopulatedListOfMembers subscription._id, (err, users)->
|
2015-05-28 15:54:41 -04:00
|
|
|
groupCsv = ""
|
|
|
|
for user in users
|
|
|
|
groupCsv += user.email + "\n"
|
|
|
|
res.header(
|
|
|
|
"Content-Disposition",
|
|
|
|
"attachment; filename=Group.csv"
|
|
|
|
)
|
|
|
|
res.contentType('text/csv')
|
|
|
|
res.send(groupCsv)
|