2014-02-12 05:23:40 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
|
|
|
SubscriptionUpdater = require("./SubscriptionUpdater")
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter = require("../User/UserGetter")
|
2014-02-12 05:23:40 -05:00
|
|
|
LimitationsManager = require("./LimitationsManager")
|
2015-05-27 11:33:47 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2015-05-27 15:50:16 -04:00
|
|
|
OneTimeTokenHandler = require("../Security/OneTimeTokenHandler")
|
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
settings = require("settings-sharelatex")
|
2016-02-18 06:43:43 -05:00
|
|
|
NotificationsBuilder = require("../Notifications/NotificationsBuilder")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-05-27 15:50:16 -04:00
|
|
|
module.exports = SubscriptionGroupHandler =
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-02-22 11:50:41 -05:00
|
|
|
addUserToGroup: (adminUserId, newEmail, callback)->
|
2016-03-09 06:17:03 -05:00
|
|
|
logger.log adminUserId:adminUserId, newEmail:newEmail, "adding user to group"
|
2017-06-08 07:12:08 -04:00
|
|
|
LimitationsManager.hasGroupMembersLimitReached adminUserId, (err, limitReached, subscription)->
|
2016-02-22 11:50:41 -05:00
|
|
|
if err?
|
2017-06-08 07:12:08 -04:00
|
|
|
logger.err err:err, adminUserId:adminUserId, newEmail:newEmail, "error checking if limit reached for group plan"
|
2016-02-22 11:50:41 -05:00
|
|
|
return callback(err)
|
2017-06-08 07:12:08 -04:00
|
|
|
if limitReached
|
|
|
|
logger.err adminUserId:adminUserId, newEmail:newEmail, "group subscription limit reached not adding user to group"
|
|
|
|
return callback(limitReached:limitReached)
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter.getUserByMainEmail newEmail, (err, user)->
|
2017-06-08 07:12:08 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
if user?
|
|
|
|
SubscriptionUpdater.addUserToGroup adminUserId, user._id, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, "error adding user to group"
|
|
|
|
return callback(err)
|
|
|
|
NotificationsBuilder.groupPlan(user, {subscription_id:subscription._id}).read()
|
|
|
|
userViewModel = buildUserViewModel(user)
|
|
|
|
callback(err, userViewModel)
|
|
|
|
else
|
|
|
|
SubscriptionUpdater.addEmailInviteToGroup adminUserId, newEmail, (err) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
userViewModel = buildEmailInviteViewModel(newEmail)
|
|
|
|
callback(err, userViewModel)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
removeUserFromGroup: (adminUser_id, userToRemove_id, callback)->
|
|
|
|
SubscriptionUpdater.removeUserFromGroup adminUser_id, userToRemove_id, callback
|
2017-06-08 07:12:08 -04:00
|
|
|
|
|
|
|
removeEmailInviteFromGroup: (adminUser_id, email, callback) ->
|
|
|
|
SubscriptionUpdater.removeEmailInviteFromGroup adminUser_id, email, callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
getPopulatedListOfMembers: (adminUser_id, callback)->
|
|
|
|
SubscriptionLocator.getUsersSubscription adminUser_id, (err, subscription)->
|
|
|
|
users = []
|
2017-06-08 07:12:08 -04:00
|
|
|
for email in subscription.invited_emails or []
|
|
|
|
users.push buildEmailInviteViewModel(email)
|
2014-02-12 05:23:40 -05:00
|
|
|
jobs = _.map subscription.member_ids, (user_id)->
|
|
|
|
return (cb)->
|
2018-05-24 09:55:12 -04:00
|
|
|
UserGetter.getUser user_id, (err, user)->
|
2014-03-25 12:19:30 -04:00
|
|
|
if err? or !user?
|
|
|
|
users.push _id:user_id
|
|
|
|
return cb()
|
2014-02-12 05:23:40 -05:00
|
|
|
userViewModel = buildUserViewModel(user)
|
|
|
|
users.push(userViewModel)
|
|
|
|
cb()
|
|
|
|
async.series jobs, (err)->
|
|
|
|
callback(err, users)
|
|
|
|
|
2015-05-27 11:33:47 -04:00
|
|
|
isUserPartOfGroup: (user_id, subscription_id, callback=(err, partOfGroup)->)->
|
|
|
|
SubscriptionLocator.getSubscriptionByMemberIdAndId user_id, subscription_id, (err, subscription)->
|
|
|
|
if subscription?
|
|
|
|
partOfGroup = true
|
|
|
|
else
|
|
|
|
partOfGroup = false
|
|
|
|
logger.log user_id:user_id, subscription_id:subscription_id, partOfGroup:partOfGroup, "checking if user is part of a group"
|
|
|
|
callback(err, partOfGroup)
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2015-05-27 15:50:16 -04:00
|
|
|
sendVerificationEmail: (subscription_id, licenceName, email, callback)->
|
2015-06-01 07:22:46 -04:00
|
|
|
ONE_DAY_IN_S = 1000 * 60 * 60 * 24
|
|
|
|
OneTimeTokenHandler.getNewToken subscription_id, {expiresIn:ONE_DAY_IN_S}, (err, token)->
|
2015-05-27 15:50:16 -04:00
|
|
|
opts =
|
|
|
|
to : email
|
|
|
|
group_name: licenceName
|
|
|
|
completeJoinUrl: "#{settings.siteUrl}/user/subscription/#{subscription_id}/group/complete-join?token=#{token}"
|
|
|
|
EmailHandler.sendEmail "completeJoinGroupAccount", opts, callback
|
|
|
|
|
|
|
|
processGroupVerification: (userEmail, subscription_id, token, callback)->
|
2016-03-09 06:17:03 -05:00
|
|
|
logger.log userEmail:userEmail, subscription_id:subscription_id, "processing group verification for user"
|
2015-05-27 15:50:16 -04:00
|
|
|
OneTimeTokenHandler.getValueFromTokenAndExpire token, (err, token_subscription_id)->
|
|
|
|
if err? or subscription_id != token_subscription_id
|
|
|
|
logger.err userEmail:userEmail, token:token, "token value not found for processing group verification"
|
2015-06-01 07:43:42 -04:00
|
|
|
return callback("token_not_found")
|
2015-05-27 15:50:16 -04:00
|
|
|
SubscriptionLocator.getSubscription subscription_id, (err, subscription)->
|
2016-02-22 11:50:41 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, subscription:subscription, userEmail:userEmail, subscription_id:subscription_id, "error getting subscription"
|
|
|
|
return callback(err)
|
|
|
|
if !subscription?
|
2016-03-09 06:17:03 -05:00
|
|
|
logger.warn subscription_id:subscription_id, userEmail:userEmail, "no subscription found"
|
2016-02-22 11:50:41 -05:00
|
|
|
return callback()
|
|
|
|
SubscriptionGroupHandler.addUserToGroup subscription?.admin_id, userEmail, callback
|
|
|
|
|
2017-06-08 07:12:08 -04:00
|
|
|
convertEmailInvitesToMemberships: (email, user_id, callback = (err) ->) ->
|
|
|
|
SubscriptionLocator.getGroupsWithEmailInvite email, (err, groups = []) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
logger.log {email, user_id, groups}, "found groups to convert from email invite to member"
|
|
|
|
jobs = []
|
|
|
|
for group in groups
|
|
|
|
do (group) ->
|
|
|
|
jobs.push (cb) ->
|
|
|
|
SubscriptionUpdater.removeEmailInviteFromGroup group.admin_id, email, (err) ->
|
|
|
|
return cb(err) if err?
|
|
|
|
SubscriptionUpdater.addUserToGroup group.admin_id, user_id, (err) ->
|
|
|
|
return cb(err) if err?
|
|
|
|
logger.log {group_id: group._id, user_id, email}, "converted email invite to group membership"
|
|
|
|
return cb()
|
|
|
|
async.series jobs, callback
|
2015-05-27 15:50:16 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
buildUserViewModel = (user)->
|
|
|
|
u =
|
|
|
|
email: user.email
|
|
|
|
first_name: user.first_name
|
|
|
|
last_name: user.last_name
|
|
|
|
holdingAccount: user.holdingAccount
|
|
|
|
_id: user._id
|
|
|
|
return u
|
2017-06-08 07:12:08 -04:00
|
|
|
|
|
|
|
buildEmailInviteViewModel = (email) ->
|
|
|
|
return {
|
|
|
|
email: email
|
|
|
|
holdingAccount: true
|
|
|
|
}
|