2018-05-30 06:29:21 -04:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
crypto = require("crypto")
|
|
|
|
|
|
|
|
settings = require("settings-sharelatex")
|
2018-05-31 06:54:50 -04:00
|
|
|
ObjectId = require("mongojs").ObjectId
|
|
|
|
|
|
|
|
TeamInvite = require("../../models/TeamInvite").TeamInvite
|
|
|
|
Subscription = require("../../models/Subscription").Subscription
|
2018-05-30 06:29:21 -04:00
|
|
|
|
|
|
|
UserLocator = require("../User/UserLocator")
|
|
|
|
SubscriptionLocator = require("./SubscriptionLocator")
|
2018-05-31 06:54:50 -04:00
|
|
|
SubscriptionUpdater = require("./SubscriptionUpdater")
|
|
|
|
LimitationsManager = require("./LimitationsManager")
|
|
|
|
|
2018-05-30 06:29:21 -04:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
|
|
|
|
module.exports = TeamInvitesHandler =
|
|
|
|
|
|
|
|
getInvites: (subscriptionId, callback) ->
|
|
|
|
TeamInvite.find(subscriptionId: subscriptionId, callback)
|
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
createInvite: (teamManagerId, email, callback) ->
|
|
|
|
UserLocator.findById teamManagerId, (error, teamManager) ->
|
|
|
|
SubscriptionLocator.getUsersSubscription teamManagerId, (error, subscription) ->
|
2018-05-30 06:29:21 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
if LimitationsManager.teamHasReachedMemberLimit(subscription)
|
|
|
|
return callback(limitReached: true)
|
|
|
|
|
|
|
|
existingInvite = subscription.teamInvites.find (invite) -> invite.email == email
|
|
|
|
|
|
|
|
if existingInvite
|
|
|
|
return callback(alreadyInvited: true)
|
|
|
|
|
|
|
|
inviterName = TeamInvitesHandler.inviterName(teamManager)
|
2018-05-30 06:29:21 -04:00
|
|
|
token = crypto.randomBytes(32).toString("hex")
|
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
invite = {
|
2018-05-30 06:29:21 -04:00
|
|
|
email: email,
|
|
|
|
token: token,
|
|
|
|
sentAt: new Date(),
|
2018-05-31 06:54:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
subscription.teamInvites.push(invite)
|
|
|
|
|
|
|
|
subscription.save (error) ->
|
2018-05-30 06:29:21 -04:00
|
|
|
return callback(error) if error?
|
2018-05-31 06:54:50 -04:00
|
|
|
|
|
|
|
# TODO: use standard way to canonalise email addresses
|
2018-05-30 06:29:21 -04:00
|
|
|
opts =
|
2018-05-31 06:54:50 -04:00
|
|
|
to: email.trim().toLowerCase()
|
2018-05-30 06:29:21 -04:00
|
|
|
inviterName: inviterName
|
|
|
|
acceptInviteUrl: "#{settings.siteUrl}/subscription/invites/#{token}/"
|
|
|
|
EmailHandler.sendEmail "verifyEmailToJoinTeam", opts, (error) ->
|
|
|
|
return callback(error, invite)
|
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
acceptInvite: (token, userId, callback) ->
|
|
|
|
TeamInvitesHandler.getInviteAndManager token, (err, invite, subscription, teamManager) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
return callback(inviteNoLongerValid: true) unless invite? and teamManager?
|
|
|
|
|
|
|
|
SubscriptionUpdater.addUserToGroup teamManager, userId, (err) ->
|
|
|
|
return callback(err) if err?
|
2018-05-30 06:29:21 -04:00
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
TeamInvitesHandler.removeInviteFromTeam(subscription.id, invite.email, callback)
|
2018-05-30 06:29:21 -04:00
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
revokeInvite: (teamManagerId, email, callback) ->
|
|
|
|
SubscriptionLocator.getUsersSubscription teamManagerId, (err, teamSubscription) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
|
|
|
|
TeamInvitesHandler.removeInviteFromTeam(teamSubscription.id, email, callback)
|
2018-05-30 08:06:27 -04:00
|
|
|
|
|
|
|
getInviteDetails: (token, userId, callback) ->
|
2018-05-31 06:54:50 -04:00
|
|
|
TeamInvitesHandler.getInviteAndManager token, (err, invite, teamSubscription, teamManager) ->
|
|
|
|
return callback(err) if err?
|
2018-05-30 08:06:27 -04:00
|
|
|
|
|
|
|
SubscriptionLocator.getUsersSubscription userId, (err, personalSubscription) ->
|
2018-05-31 06:54:50 -04:00
|
|
|
return callback(err) if err?
|
|
|
|
|
|
|
|
return callback(null , {
|
|
|
|
invite: invite,
|
|
|
|
personalSubscription: personalSubscription,
|
|
|
|
team: teamSubscription,
|
|
|
|
inviterName: TeamInvitesHandler.inviterName(teamManager),
|
|
|
|
teamManager: teamManager
|
|
|
|
})
|
|
|
|
|
|
|
|
getInviteAndManager: (token, callback) ->
|
|
|
|
TeamInvitesHandler.getInvite token, (err, invite, teamSubscription) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
|
|
|
|
UserLocator.findById teamSubscription.admin_id, (err, teamManager) ->
|
|
|
|
return callback(err, invite, teamSubscription, teamManager)
|
|
|
|
|
|
|
|
getInvite: (token, callback) ->
|
|
|
|
Subscription.findOne 'teamInvites.token': token, (err, subscription) ->
|
|
|
|
return callback(err, subscription) if err?
|
|
|
|
return callback(teamNotFound: true) unless subscription?
|
|
|
|
|
|
|
|
invite = subscription.teamInvites.find (i) -> i.token == token
|
|
|
|
return callback(null, invite, subscription)
|
2018-05-30 08:06:27 -04:00
|
|
|
|
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
removeInviteFromTeam: (subscriptionId, email, callback) ->
|
|
|
|
searchConditions = { _id: new ObjectId(subscriptionId.toString()) }
|
|
|
|
updateOp = { $pull: { teamInvites: { email: email.trim().toLowerCase() } } }
|
2018-05-30 08:06:27 -04:00
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
Subscription.update(searchConditions, updateOp, callback)
|
2018-05-30 08:06:27 -04:00
|
|
|
|
2018-05-31 06:54:50 -04:00
|
|
|
inviterName: (teamManager) ->
|
|
|
|
if teamManager.first_name and teamManager.last_name
|
|
|
|
"#{teamManager.first_name} #{teamManager.last_name} (#{teamManager.email})"
|
2018-05-30 08:06:27 -04:00
|
|
|
else
|
2018-05-31 06:54:50 -04:00
|
|
|
teamManager.email
|