2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let InstitutionsController
|
|
|
|
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
|
|
|
|
|
|
|
module.exports = InstitutionsController = {
|
|
|
|
confirmDomain(req, res, next) {
|
|
|
|
const { hostname } = req.body
|
|
|
|
return affiliateUsers(hostname, function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return res.sendStatus(200)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUsers = function(hostname, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error) {}
|
|
|
|
}
|
|
|
|
const reversedHostname = hostname
|
|
|
|
.trim()
|
|
|
|
.split('')
|
|
|
|
.reverse()
|
|
|
|
.join('')
|
|
|
|
return UserGetter.getUsersByHostname(
|
|
|
|
hostname,
|
|
|
|
{ _id: 1, emails: 1 },
|
|
|
|
function(error, users) {
|
|
|
|
if (error != null) {
|
|
|
|
logger.err({ error }, 'problem fetching users by hostname')
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2019-06-11 09:15:04 -04:00
|
|
|
return async.mapLimit(
|
2019-05-29 05:21:06 -04:00
|
|
|
users,
|
2019-06-11 09:15:04 -04:00
|
|
|
ASYNC_AFFILIATIONS_LIMIT,
|
2019-05-29 05:21:06 -04:00
|
|
|
(user, innerCallback) =>
|
|
|
|
affiliateUserByReversedHostname(
|
|
|
|
user,
|
|
|
|
reversedHostname,
|
|
|
|
innerCallback
|
|
|
|
),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var affiliateUserByReversedHostname = function(
|
|
|
|
user,
|
|
|
|
reversedHostname,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const matchingEmails = user.emails.filter(
|
|
|
|
email => email.reversedHostname === reversedHostname
|
|
|
|
)
|
2019-06-11 09:15:04 -04:00
|
|
|
return async.mapSeries(
|
2019-05-29 05:21:06 -04:00
|
|
|
matchingEmails,
|
|
|
|
(email, innerCallback) =>
|
|
|
|
addAffiliation(
|
|
|
|
user._id,
|
|
|
|
email.email,
|
|
|
|
{ confirmedAt: email.confirmedAt },
|
|
|
|
error => {
|
|
|
|
if (error != null) {
|
|
|
|
logger.err(
|
|
|
|
{ error },
|
|
|
|
'problem adding affiliation while confirming hostname'
|
|
|
|
)
|
|
|
|
return innerCallback(error)
|
|
|
|
}
|
2019-06-11 09:14:38 -04:00
|
|
|
return FeaturesUpdater.refreshFeatures(user._id, innerCallback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|