2020-08-19 05:11:32 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
const { addAffiliation } = require('../Institutions/InstitutionsAPI')
|
|
|
|
const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
|
|
|
|
const async = require('async')
|
2019-06-11 09:15:04 -04:00
|
|
|
const ASYNC_AFFILIATIONS_LIMIT = 10
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-01 04:37:56 -04:00
|
|
|
module.exports = {
|
2019-05-29 05:21:06 -04:00
|
|
|
confirmDomain(req, res, next) {
|
|
|
|
const { hostname } = req.body
|
2020-06-01 04:37:56 -04:00
|
|
|
affiliateUsers(hostname, function(error) {
|
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
res.sendStatus(200)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUsers = function(hostname, callback) {
|
|
|
|
const reversedHostname = hostname
|
|
|
|
.trim()
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
2020-09-15 08:49:42 -04:00
|
|
|
UserGetter.getUsersByHostname(hostname, { _id: 1 }, function(error, users) {
|
2020-06-01 04:37:56 -04:00
|
|
|
if (error) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(error, 'problem fetching users by hostname')
|
2020-06-01 04:37:56 -04:00
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
|
|
|
|
async.mapLimit(
|
|
|
|
users,
|
|
|
|
ASYNC_AFFILIATIONS_LIMIT,
|
2020-09-15 08:49:42 -04:00
|
|
|
(user, innerCallback) => {
|
|
|
|
UserGetter.getUserFullEmails(user._id, (error, emails) => {
|
|
|
|
if (error) return innerCallback(error)
|
|
|
|
user.emails = emails
|
|
|
|
affiliateUserByReversedHostname(user, reversedHostname, innerCallback)
|
|
|
|
})
|
|
|
|
},
|
2020-06-01 04:37:56 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUserByReversedHostname = function(
|
|
|
|
user,
|
|
|
|
reversedHostname,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const matchingEmails = user.emails.filter(
|
|
|
|
email => email.reversedHostname === reversedHostname
|
|
|
|
)
|
2020-06-01 04:37:56 -04:00
|
|
|
async.mapSeries(
|
2019-05-29 05:21:06 -04:00
|
|
|
matchingEmails,
|
2020-09-15 08:49:42 -04:00
|
|
|
(email, innerCallback) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
addAffiliation(
|
|
|
|
user._id,
|
|
|
|
email.email,
|
2020-09-15 08:49:42 -04:00
|
|
|
{
|
|
|
|
confirmedAt: email.confirmedAt,
|
|
|
|
entitlement:
|
|
|
|
email.samlIdentifier && email.samlIdentifier.hasEntitlement
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
error => {
|
2020-06-01 04:37:56 -04:00
|
|
|
if (error) {
|
2020-08-19 05:11:32 -04:00
|
|
|
OError.tag(
|
|
|
|
error,
|
2019-05-29 05:21:06 -04:00
|
|
|
'problem adding affiliation while confirming hostname'
|
|
|
|
)
|
|
|
|
return innerCallback(error)
|
|
|
|
}
|
2020-06-01 04:37:56 -04:00
|
|
|
FeaturesUpdater.refreshFeatures(user._id, innerCallback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-09-15 08:49:42 -04:00
|
|
|
)
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|