|
@@ -20,6 +20,7 @@ const _ = require('lodash')
|
|
|
const Modules = require('../../infrastructure/Modules')
|
|
const Modules = require('../../infrastructure/Modules')
|
|
|
const UserSessionsManager = require('./UserSessionsManager')
|
|
const UserSessionsManager = require('./UserSessionsManager')
|
|
|
const ThirdPartyIdentityManager = require('./ThirdPartyIdentityManager')
|
|
const ThirdPartyIdentityManager = require('./ThirdPartyIdentityManager')
|
|
|
|
|
+const AsyncLocalStorage = require('../../infrastructure/AsyncLocalStorage')
|
|
|
|
|
|
|
|
async function _sendSecurityAlertPrimaryEmailChanged(
|
|
async function _sendSecurityAlertPrimaryEmailChanged(
|
|
|
userId,
|
|
userId,
|
|
@@ -81,6 +82,7 @@ async function _sendSecurityAlertPrimaryEmailChanged(
|
|
|
* or any other user
|
|
* or any other user
|
|
|
*/
|
|
*/
|
|
|
async function addEmailAddress(userId, newEmail, affiliationOptions, auditLog) {
|
|
async function addEmailAddress(userId, newEmail, affiliationOptions, auditLog) {
|
|
|
|
|
+ AsyncLocalStorage.removeItem('userFullEmails')
|
|
|
newEmail = EmailHelper.parseEmail(newEmail)
|
|
newEmail = EmailHelper.parseEmail(newEmail)
|
|
|
if (!newEmail) {
|
|
if (!newEmail) {
|
|
|
throw new Error('invalid email')
|
|
throw new Error('invalid email')
|
|
@@ -134,15 +136,17 @@ async function addEmailAddress(userId, newEmail, affiliationOptions, auditLog) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- EmailChangeHelper.registerEmailCreation(userId, newEmail, {
|
|
|
|
|
- createdAt: new Date(),
|
|
|
|
|
- emailCreatedAt: createdAt,
|
|
|
|
|
- }).catch(error => {
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await EmailChangeHelper.registerEmailCreation(userId, newEmail, {
|
|
|
|
|
+ createdAt: new Date(),
|
|
|
|
|
+ emailCreatedAt: createdAt,
|
|
|
|
|
+ })
|
|
|
|
|
+ } catch (error) {
|
|
|
logger.warn(
|
|
logger.warn(
|
|
|
{ error, userId, newEmail },
|
|
{ error, userId, newEmail },
|
|
|
'Error registering email creation with analytics'
|
|
'Error registering email creation with analytics'
|
|
|
)
|
|
)
|
|
|
- })
|
|
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function clearSAMLData(userId, auditLog, sendEmail) {
|
|
async function clearSAMLData(userId, auditLog, sendEmail) {
|
|
@@ -215,6 +219,7 @@ async function setDefaultEmailAddress(
|
|
|
sendSecurityAlert,
|
|
sendSecurityAlert,
|
|
|
deleteOldEmail = false
|
|
deleteOldEmail = false
|
|
|
) {
|
|
) {
|
|
|
|
|
+ AsyncLocalStorage.removeItem('userFullEmails')
|
|
|
email = EmailHelper.parseEmail(email)
|
|
email = EmailHelper.parseEmail(email)
|
|
|
if (email == null) {
|
|
if (email == null) {
|
|
|
throw new Error('invalid email')
|
|
throw new Error('invalid email')
|
|
@@ -262,16 +267,18 @@ async function setDefaultEmailAddress(
|
|
|
'primary-email-address-updated'
|
|
'primary-email-address-updated'
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- EmailChangeHelper.registerEmailUpdate(userId, email, {
|
|
|
|
|
- isPrimary: true,
|
|
|
|
|
- action: 'updated',
|
|
|
|
|
- createdAt: new Date(),
|
|
|
|
|
- }).catch(err => {
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await EmailChangeHelper.registerEmailUpdate(userId, email, {
|
|
|
|
|
+ isPrimary: true,
|
|
|
|
|
+ action: 'updated',
|
|
|
|
|
+ createdAt: new Date(),
|
|
|
|
|
+ })
|
|
|
|
|
+ } catch (err) {
|
|
|
logger.warn(
|
|
logger.warn(
|
|
|
{ err, userId, email },
|
|
{ err, userId, email },
|
|
|
'Error registering email change with analytics'
|
|
'Error registering email change with analytics'
|
|
|
)
|
|
)
|
|
|
- })
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
if (sendSecurityAlert) {
|
|
if (sendSecurityAlert) {
|
|
|
// no need to wait, errors are logged and not passed back
|
|
// no need to wait, errors are logged and not passed back
|
|
@@ -366,6 +373,7 @@ async function migrateDefaultEmailAddress(
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function confirmEmail(userId, email, affiliationOptions) {
|
|
async function confirmEmail(userId, email, affiliationOptions) {
|
|
|
|
|
+ AsyncLocalStorage.removeItem('userFullEmails')
|
|
|
// used for initial email confirmation (non-SSO and SSO)
|
|
// used for initial email confirmation (non-SSO and SSO)
|
|
|
// also used for reconfirmation of non-SSO emails
|
|
// also used for reconfirmation of non-SSO emails
|
|
|
const confirmedAt = new Date()
|
|
const confirmedAt = new Date()
|
|
@@ -415,16 +423,18 @@ async function confirmEmail(userId, email, affiliationOptions) {
|
|
|
}
|
|
}
|
|
|
await FeaturesUpdater.promises.refreshFeatures(userId, 'confirm-email')
|
|
await FeaturesUpdater.promises.refreshFeatures(userId, 'confirm-email')
|
|
|
|
|
|
|
|
- EmailChangeHelper.registerEmailUpdate(userId, email, {
|
|
|
|
|
- emailConfirmedAt: confirmedAt,
|
|
|
|
|
- action: 'updated',
|
|
|
|
|
- isPrimary: false,
|
|
|
|
|
- }).catch(error =>
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await EmailChangeHelper.registerEmailUpdate(userId, email, {
|
|
|
|
|
+ emailConfirmedAt: confirmedAt,
|
|
|
|
|
+ action: 'updated',
|
|
|
|
|
+ isPrimary: false,
|
|
|
|
|
+ })
|
|
|
|
|
+ } catch (error) {
|
|
|
logger.warn(
|
|
logger.warn(
|
|
|
{ error, userId, email },
|
|
{ error, userId, email },
|
|
|
'Error registering email confirmation with analytics'
|
|
'Error registering email confirmation with analytics'
|
|
|
)
|
|
)
|
|
|
- )
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
await maybeCreateRedundantSubscriptionNotification(userId, email)
|
|
await maybeCreateRedundantSubscriptionNotification(userId, email)
|
|
@@ -467,6 +477,7 @@ async function removeEmailAddress(
|
|
|
auditLog,
|
|
auditLog,
|
|
|
skipParseEmail = false
|
|
skipParseEmail = false
|
|
|
) {
|
|
) {
|
|
|
|
|
+ AsyncLocalStorage.removeItem('userFullEmails')
|
|
|
// remove one of the user's email addresses. The email cannot be the user's
|
|
// remove one of the user's email addresses. The email cannot be the user's
|
|
|
// default email address
|
|
// default email address
|
|
|
if (!skipParseEmail) {
|
|
if (!skipParseEmail) {
|
|
@@ -520,15 +531,17 @@ async function removeEmailAddress(
|
|
|
throw new Error('Cannot remove email')
|
|
throw new Error('Cannot remove email')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- EmailChangeHelper.registerEmailDeletion(userId, email, {
|
|
|
|
|
- isPrimary: false,
|
|
|
|
|
- emailDeletedAt: new Date(),
|
|
|
|
|
- }).catch(error =>
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await EmailChangeHelper.registerEmailDeletion(userId, email, {
|
|
|
|
|
+ isPrimary: false,
|
|
|
|
|
+ emailDeletedAt: new Date(),
|
|
|
|
|
+ })
|
|
|
|
|
+ } catch (error) {
|
|
|
logger.warn(
|
|
logger.warn(
|
|
|
{ error, userId, email },
|
|
{ error, userId, email },
|
|
|
'Error registering email deletion with analytics'
|
|
'Error registering email deletion with analytics'
|
|
|
)
|
|
)
|
|
|
- )
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
await FeaturesUpdater.promises.refreshFeatures(userId, 'remove-email')
|
|
await FeaturesUpdater.promises.refreshFeatures(userId, 'remove-email')
|
|
|
}
|
|
}
|
|
@@ -538,6 +551,7 @@ async function addAffiliationForNewUser(
|
|
|
email,
|
|
email,
|
|
|
affiliationOptions = {}
|
|
affiliationOptions = {}
|
|
|
) {
|
|
) {
|
|
|
|
|
+ AsyncLocalStorage.removeItem('userFullEmails')
|
|
|
await InstitutionsAPI.promises.addAffiliation(
|
|
await InstitutionsAPI.promises.addAffiliation(
|
|
|
userId,
|
|
userId,
|
|
|
email,
|
|
email,
|