overleaf/services/web/app/coffee/Features/Institutions/InstitutionsController.coffee
Timothée Alby 5de25032d8 Merge pull request #1634 from sharelatex/ta-no-institution-autocreate
Don't Auto-Create Institutions

GitOrigin-RevId: 8be3a0d9f3f3814396a58d2fd131a62c9f196861
2019-03-19 23:51:53 +00:00

33 lines
1.3 KiB
CoffeeScript

logger = require("logger-sharelatex")
UserGetter = require("../User/UserGetter")
{ addAffiliation } = require("../Institutions/InstitutionsAPI")
FeaturesUpdater = require('../Subscription/FeaturesUpdater')
async = require('async')
module.exports = InstitutionsController =
confirmDomain: (req, res, next) ->
hostname = req.body.hostname
affiliateUsers hostname, (error) ->
return next(error) if error?
res.sendStatus 200
affiliateUsers = (hostname, callback = (error)->) ->
reversedHostname = hostname.trim().split('').reverse().join('')
UserGetter.getUsersByHostname hostname, {_id:1, emails:1}, (error, users) ->
if error?
logger.err error: error, 'problem fetching users by hostname'
return callback(error)
async.map users, ((user, innerCallback) ->
affiliateUserByReversedHostname user, reversedHostname, innerCallback
), callback
affiliateUserByReversedHostname = (user, reversedHostname, callback) ->
matchingEmails = user.emails.filter (email) -> email.reversedHostname == reversedHostname
async.map matchingEmails, ((email, innerCallback) ->
addAffiliation user._id, email.email, {confirmedAt: email.confirmedAt}, (error) =>
if error?
logger.err error: error, 'problem adding affiliation while confirming hostname'
return innerCallback(error)
FeaturesUpdater.refreshFeatures user._id, true, innerCallback
), callback