2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
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-06-01 04:37:56 -04:00
|
|
|
UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function(
|
|
|
|
error,
|
|
|
|
users
|
|
|
|
) {
|
|
|
|
if (error) {
|
|
|
|
logger.warn({ error }, 'problem fetching users by hostname')
|
|
|
|
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,
|
|
|
|
(user, innerCallback) =>
|
|
|
|
affiliateUserByReversedHostname(user, reversedHostname, innerCallback),
|
|
|
|
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,
|
|
|
|
(email, innerCallback) =>
|
|
|
|
addAffiliation(
|
|
|
|
user._id,
|
|
|
|
email.email,
|
|
|
|
{ confirmedAt: email.confirmedAt },
|
|
|
|
error => {
|
2020-06-01 04:37:56 -04:00
|
|
|
if (error) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ error },
|
|
|
|
'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
|
|
|
}
|
|
|
|
),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|