overleaf/services/web/app/coffee/Features/Subscription/TeamInvitesHandler.coffee

115 lines
4 KiB
CoffeeScript
Raw Normal View History

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
UserLocator = require("../User/UserLocator")
SubscriptionLocator = require("./SubscriptionLocator")
2018-05-31 06:54:50 -04:00
SubscriptionUpdater = require("./SubscriptionUpdater")
LimitationsManager = require("./LimitationsManager")
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) ->
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)
token = crypto.randomBytes(32).toString("hex")
2018-05-31 06:54:50 -04:00
invite = {
email: email,
token: token,
sentAt: new Date(),
2018-05-31 06:54:50 -04:00
}
subscription.teamInvites.push(invite)
subscription.save (error) ->
return callback(error) if error?
2018-05-31 06:54:50 -04:00
# TODO: use standard way to canonalise email addresses
opts =
2018-05-31 06:54:50 -04:00
to: email.trim().toLowerCase()
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-31 06:54:50 -04:00
TeamInvitesHandler.removeInviteFromTeam(subscription.id, invite.email, callback)
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