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 =
|
|
|
|
|
|
|
|
addUserToGroup: (req, res)->
|
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
|
|
|
logger.log adminUserId:adminUserId, newEmail:newEmail, "adding user to group subscription"
|
|
|
|
SubscriptionGroupHandler.addUserToGroup adminUserId, newEmail, (err, user)->
|
2016-02-22 11:01:17 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, newEmail:newEmail, adminUserId:adminUserId, "error adding user from group"
|
|
|
|
return res.sendStatus 500
|
2016-09-05 10:58:31 -04:00
|
|
|
result =
|
2014-02-12 05:23:40 -05:00
|
|
|
user:user
|
|
|
|
if err and err.limitReached
|
|
|
|
result.limitReached = true
|
|
|
|
res.json(result)
|
|
|
|
|
|
|
|
removeUserFromGroup: (req, res)->
|
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
|
|
|
|
logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription"
|
2016-02-22 11:01:17 -05:00
|
|
|
SubscriptionGroupHandler.removeUserFromGroup adminUserId, userToRemove_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, adminUserId:adminUserId, userToRemove_id:userToRemove_id, "error removing user from group"
|
|
|
|
return res.sendStatus 500
|
2014-02-12 05:23:40 -05:00
|
|
|
res.send()
|
2016-09-05 10:58:31 -04:00
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
removeEmailInviteFromGroup: (req, res)->
|
|
|
|
adminUserId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
email = req.params.email
|
|
|
|
logger.log {adminUserId, email}, "removing email invite from group subscription"
|
|
|
|
SubscriptionGroupHandler.removeEmailInviteFromGroup adminUserId, email, (err)->
|
|
|
|
return next(error) if error?
|
|
|
|
res.send()
|
|
|
|
|
2016-02-19 07:27:01 -05:00
|
|
|
removeSelfFromGroup: (req, res)->
|
|
|
|
adminUserId = req.query.admin_user_id
|
2016-09-05 10:58:31 -04:00
|
|
|
userToRemove_id = AuthenticationController.getLoggedInUserId(req)
|
2016-02-19 07:27:01 -05:00
|
|
|
logger.log adminUserId:adminUserId, userToRemove_id:userToRemove_id, "removing user from group subscription after self request"
|
2016-02-22 11:01:17 -05:00
|
|
|
SubscriptionGroupHandler.removeUserFromGroup adminUserId, userToRemove_id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, userToRemove_id:userToRemove_id, adminUserId:adminUserId, "error removing self from group"
|
|
|
|
return res.sendStatus 500
|
2016-02-19 07:27:01 -05:00
|
|
|
res.send()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
renderSubscriptionGroupAdminPage: (req, res)->
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2014-02-12 05:23:40 -05:00
|
|
|
SubscriptionLocator.getUsersSubscription user_id, (err, subscription)->
|
|
|
|
if !subscription.groupPlan
|
|
|
|
return res.redirect("/")
|
|
|
|
SubscriptionGroupHandler.getPopulatedListOfMembers user_id, (err, users)->
|
|
|
|
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
|
|
|
|
|
|
|
renderGroupInvitePage: (req, res)->
|
2016-03-22 12:59:47 -04:00
|
|
|
group_subscription_id = req.params.subscription_id
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2016-03-22 12:59:47 -04:00
|
|
|
licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(group_subscription_id)
|
2015-05-27 15:50:16 -04:00
|
|
|
if !licence?
|
|
|
|
return ErrorsController.notFound(req, res)
|
2016-09-05 10:58:31 -04:00
|
|
|
jobs =
|
2016-03-22 12:59:47 -04:00
|
|
|
partOfGroup: (cb)->
|
|
|
|
SubscriptionGroupHandler.isUserPartOfGroup user_id, licence.group_subscription_id, cb
|
|
|
|
subscription: (cb)->
|
|
|
|
SubscriptionLocator.getUsersSubscription user_id, cb
|
|
|
|
async.series jobs, (err, results)->
|
|
|
|
{partOfGroup, subscription} = results
|
2015-05-27 11:33:47 -04:00
|
|
|
if partOfGroup
|
2015-05-27 10:50:28 -04:00
|
|
|
return res.redirect("/user/subscription/custom_account")
|
2015-05-27 11:33:47 -04:00
|
|
|
else
|
|
|
|
res.render "subscriptions/group/invite",
|
|
|
|
title: "Group Invitation"
|
2016-03-22 12:59:47 -04:00
|
|
|
group_subscription_id:group_subscription_id
|
2015-05-27 11:33:47 -04:00
|
|
|
licenceName:licence.name
|
2016-03-22 12:59:47 -04:00
|
|
|
has_personal_subscription: subscription?
|
2015-05-22 08:57:15 -04:00
|
|
|
|
|
|
|
beginJoinGroup: (req, res)->
|
|
|
|
subscription_id = req.params.subscription_id
|
2016-09-07 05:30:58 -04:00
|
|
|
currentUser = AuthenticationController.getSessionUser(req)
|
|
|
|
if !currentUser?
|
|
|
|
logger.err {subscription_id}, "error getting current user"
|
|
|
|
return res.sendStatus 500
|
|
|
|
licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)
|
|
|
|
if !licence?
|
|
|
|
return ErrorsController.notFound(req, res)
|
|
|
|
SubscriptionGroupHandler.sendVerificationEmail subscription_id, licence.name, currentUser.email, (err)->
|
2015-05-27 15:50:16 -04:00
|
|
|
if err?
|
2016-09-07 05:30:58 -04:00
|
|
|
res.sendStatus 500
|
|
|
|
else
|
|
|
|
res.sendStatus 200
|
2015-05-22 08:57:15 -04:00
|
|
|
|
|
|
|
completeJoin: (req, res)->
|
2016-09-22 10:33:50 -04:00
|
|
|
currentUser = AuthenticationController.getSessionUser(req)
|
2015-05-22 08:57:15 -04:00
|
|
|
subscription_id = req.params.subscription_id
|
2015-05-27 15:57:54 -04:00
|
|
|
if !SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)?
|
2015-05-27 15:50:16 -04:00
|
|
|
return ErrorsController.notFound(req, res)
|
2016-09-22 10:33:50 -04:00
|
|
|
email = currentUser?.email
|
|
|
|
logger.log subscription_id:subscription_id, user_id:currentUser?._id, email:email, "starting the completion of joining group"
|
2016-02-18 06:43:43 -05:00
|
|
|
SubscriptionGroupHandler.processGroupVerification email, subscription_id, req.query?.token, (err)->
|
2015-06-01 07:43:42 -04:00
|
|
|
if err? and err == "token_not_found"
|
2016-03-09 06:17:03 -05:00
|
|
|
return res.redirect "/user/subscription/#{subscription_id}/group/invited?expired=true"
|
2015-06-01 07:43:42 -04:00
|
|
|
else if err?
|
2016-03-09 06:17:03 -05:00
|
|
|
return res.sendStatus 500
|
2015-05-27 15:50:16 -04:00
|
|
|
else
|
2016-03-09 06:17:03 -05:00
|
|
|
logger.log subscription_id:subscription_id, email:email, "user successful completed join of group subscription"
|
|
|
|
return res.redirect "/user/subscription/#{subscription_id}/group/successful-join"
|
2015-05-22 08:57:15 -04:00
|
|
|
|
2015-05-26 10:25:46 -04:00
|
|
|
renderSuccessfulJoinPage: (req, res)->
|
|
|
|
subscription_id = req.params.subscription_id
|
2015-05-27 15:57:54 -04:00
|
|
|
licence = SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)
|
|
|
|
if !SubscriptionDomainHandler.findDomainLicenceBySubscriptionId(subscription_id)?
|
2015-05-27 15:50:16 -04:00
|
|
|
return ErrorsController.notFound(req, res)
|
2015-05-26 10:25:46 -04:00
|
|
|
res.render "subscriptions/group/successful_join",
|
|
|
|
title: "Sucessfully joined group"
|
2016-09-05 10:58:31 -04:00
|
|
|
licenceName:licence.name
|
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"
|
|
|
|
SubscriptionLocator.getUsersSubscription user_id, (err, subscription)->
|
|
|
|
if !subscription.groupPlan
|
|
|
|
return res.redirect("/")
|
|
|
|
SubscriptionGroupHandler.getPopulatedListOfMembers user_id, (err, users)->
|
|
|
|
groupCsv = ""
|
|
|
|
for user in users
|
|
|
|
groupCsv += user.email + "\n"
|
|
|
|
res.header(
|
|
|
|
"Content-Disposition",
|
|
|
|
"attachment; filename=Group.csv"
|
|
|
|
)
|
|
|
|
res.contentType('text/csv')
|
|
|
|
res.send(groupCsv)
|