2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2018-11-05 05:06:39 -05:00
|
|
|
define(['base'], App =>
|
2019-07-16 05:13:18 -04:00
|
|
|
App.controller('UserAffiliationsController', function(
|
|
|
|
$scope,
|
|
|
|
UserAffiliationsDataService,
|
|
|
|
$q,
|
2019-09-30 09:21:31 -04:00
|
|
|
$window,
|
2019-07-16 05:13:18 -04:00
|
|
|
_
|
|
|
|
) {
|
|
|
|
$scope.userEmails = []
|
2019-09-30 09:21:31 -04:00
|
|
|
$scope.linkedInstitutionIds = []
|
|
|
|
$scope.hideInstitutionNotifications = {}
|
|
|
|
$scope.closeInstitutionNotification = type => {
|
|
|
|
$scope.hideInstitutionNotifications[type] = true
|
|
|
|
}
|
2019-12-09 10:10:07 -05:00
|
|
|
$scope.samlBetaSession = ExposedSettings.hasSamlBeta
|
2019-10-02 17:37:19 -04:00
|
|
|
$scope.samlInitPath = ExposedSettings.samlInitPath
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
const LOCAL_AND_DOMAIN_REGEX = /([^@]+)@(.+)/
|
|
|
|
const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
const _matchLocalAndDomain = function(userEmailInput) {
|
|
|
|
const match =
|
|
|
|
userEmailInput != null
|
|
|
|
? userEmailInput.match(LOCAL_AND_DOMAIN_REGEX)
|
|
|
|
: undefined
|
|
|
|
if (match != null) {
|
|
|
|
return { local: match[1], domain: match[2] }
|
|
|
|
} else {
|
|
|
|
return { local: null, domain: null }
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2019-12-09 10:10:07 -05:00
|
|
|
const _ssoAvailable = affiliation => {
|
|
|
|
if (!affiliation) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// university via v1 for new affiliations
|
|
|
|
const institution = affiliation.university || affiliation.institution
|
|
|
|
if (!institution) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (institution && institution.ssoEnabled) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($scope.samlBetaSession && institution && institution.ssoBeta) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.getEmailSuggestion = function(userInput) {
|
|
|
|
const userInputLocalAndDomain = _matchLocalAndDomain(userInput)
|
|
|
|
$scope.ui.isValidEmail = EMAIL_REGEX.test(userInput)
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
|
|
|
if (userInputLocalAndDomain.domain != null) {
|
|
|
|
$scope.ui.isBlacklistedEmail = UserAffiliationsDataService.isDomainBlacklisted(
|
|
|
|
userInputLocalAndDomain.domain
|
|
|
|
)
|
|
|
|
return UserAffiliationsDataService.getUniversityDomainFromPartialDomainInput(
|
|
|
|
userInputLocalAndDomain.domain
|
|
|
|
)
|
|
|
|
.then(function(universityDomain) {
|
|
|
|
const currentUserInputLocalAndDomain = _matchLocalAndDomain(
|
|
|
|
$scope.newAffiliation.email
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
currentUserInputLocalAndDomain.domain ===
|
|
|
|
universityDomain.hostname
|
|
|
|
) {
|
|
|
|
$scope.newAffiliation.university = universityDomain.university
|
|
|
|
$scope.newAffiliation.department = universityDomain.department
|
2019-12-09 10:10:07 -05:00
|
|
|
$scope.newAffiliation.ssoAvailable = _ssoAvailable(
|
|
|
|
universityDomain
|
|
|
|
)
|
2019-07-16 05:13:18 -04:00
|
|
|
} else {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
|
|
|
return $q.resolve(
|
|
|
|
`${userInputLocalAndDomain.local}@${universityDomain.hostname}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.catch(function() {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2019-07-16 05:13:18 -04:00
|
|
|
return $q.reject(null)
|
|
|
|
})
|
|
|
|
} else {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2019-07-16 05:13:18 -04:00
|
|
|
return $q.reject(null)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-09-30 09:21:31 -04:00
|
|
|
$scope.linkInstitutionAcct = function(email, institutionId) {
|
2019-10-02 10:06:23 -04:00
|
|
|
_resetMakingRequestType()
|
2019-09-30 09:21:31 -04:00
|
|
|
$scope.ui.isMakingRequest = true
|
2019-10-02 10:06:23 -04:00
|
|
|
$scope.ui.isProcessing = true
|
2019-09-30 09:21:31 -04:00
|
|
|
$window.location.href = `${
|
2019-10-02 17:37:19 -04:00
|
|
|
$scope.samlInitPath
|
2019-11-21 10:55:05 -05:00
|
|
|
}?university_id=${institutionId}&auto=/user/settings&email=${email}`
|
2019-09-30 09:21:31 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.selectUniversityManually = function() {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2019-07-16 05:13:18 -04:00
|
|
|
return ($scope.ui.showManualUniversitySelectionUI = true)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.changeAffiliation = function(userEmail) {
|
|
|
|
if (
|
|
|
|
__guard__(
|
|
|
|
userEmail.affiliation != null
|
|
|
|
? userEmail.affiliation.institution
|
|
|
|
: undefined,
|
|
|
|
x => x.id
|
|
|
|
) != null
|
|
|
|
) {
|
|
|
|
UserAffiliationsDataService.getUniversityDetails(
|
|
|
|
userEmail.affiliation.institution.id
|
|
|
|
).then(
|
|
|
|
universityDetails =>
|
|
|
|
($scope.affiliationToChange.university = universityDetails)
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.affiliationToChange.email = userEmail.email
|
|
|
|
$scope.affiliationToChange.role = userEmail.affiliation.role
|
|
|
|
return ($scope.affiliationToChange.department =
|
|
|
|
userEmail.affiliation.department)
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.saveAffiliationChange = function(userEmail) {
|
|
|
|
userEmail.affiliation.role = $scope.affiliationToChange.role
|
|
|
|
userEmail.affiliation.department = $scope.affiliationToChange.department
|
|
|
|
_resetAffiliationToChange()
|
|
|
|
return _monitorRequest(
|
|
|
|
UserAffiliationsDataService.addRoleAndDepartment(
|
|
|
|
userEmail.email,
|
|
|
|
userEmail.affiliation.role,
|
|
|
|
userEmail.affiliation.department
|
|
|
|
)
|
|
|
|
).then(() => setTimeout(() => _getUserEmails()))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.cancelAffiliationChange = email => _resetAffiliationToChange()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.isChangingAffiliation = email =>
|
|
|
|
$scope.affiliationToChange.email === email
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.showAddEmailForm = () => ($scope.ui.showAddEmailUI = true)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.addNewEmail = function() {
|
|
|
|
let addEmailPromise
|
|
|
|
if ($scope.newAffiliation.university == null) {
|
|
|
|
addEmailPromise = UserAffiliationsDataService.addUserEmail(
|
|
|
|
$scope.newAffiliation.email
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
if ($scope.newAffiliation.university.isUserSuggested) {
|
|
|
|
addEmailPromise = UserAffiliationsDataService.addUserAffiliationWithUnknownUniversity(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.name,
|
|
|
|
$scope.newAffiliation.country.code,
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
2018-11-05 05:06:39 -05:00
|
|
|
)
|
|
|
|
} else {
|
2019-07-16 05:13:18 -04:00
|
|
|
addEmailPromise = UserAffiliationsDataService.addUserAffiliation(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.id,
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.ui.isAddingNewEmail = true
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
return _monitorRequest(addEmailPromise)
|
|
|
|
.then(function() {
|
|
|
|
_resetNewAffiliation()
|
|
|
|
_resetAddingEmail()
|
|
|
|
return setTimeout(() => _getUserEmails())
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2019-07-16 05:13:18 -04:00
|
|
|
.finally(() => ($scope.ui.isAddingNewEmail = false))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.setDefaultUserEmail = userEmail =>
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService.setDefaultUserEmail(userEmail.email)
|
|
|
|
).then(function() {
|
|
|
|
for (let email of Array.from($scope.userEmails || [])) {
|
|
|
|
email.default = false
|
|
|
|
}
|
|
|
|
return (userEmail.default = true)
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.removeUserEmail = function(userEmail) {
|
|
|
|
$scope.userEmails = $scope.userEmails.filter(ue => ue !== userEmail)
|
|
|
|
return _monitorRequest(
|
|
|
|
UserAffiliationsDataService.removeUserEmail(userEmail.email)
|
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.resendConfirmationEmail = function(userEmail) {
|
2019-10-02 10:06:23 -04:00
|
|
|
_resetMakingRequestType()
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.ui.isResendingConfirmation = true
|
|
|
|
return _monitorRequest(
|
|
|
|
UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
|
|
|
|
).finally(() => ($scope.ui.isResendingConfirmation = false))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.acknowledgeError = function() {
|
|
|
|
_reset()
|
|
|
|
return _getUserEmails()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
var _resetAffiliationToChange = () =>
|
|
|
|
($scope.affiliationToChange = {
|
|
|
|
email: '',
|
|
|
|
university: null,
|
|
|
|
role: null,
|
|
|
|
department: null
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
var _resetNewAffiliation = () =>
|
|
|
|
($scope.newAffiliation = {
|
|
|
|
email: '',
|
|
|
|
country: null,
|
|
|
|
university: null,
|
|
|
|
role: null,
|
|
|
|
department: null
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
var _resetAddingEmail = function() {
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
$scope.ui.isValidEmail = false
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
return ($scope.ui.showManualUniversitySelectionUI = false)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-01-07 06:03:54 -05:00
|
|
|
var _resetAffiliationSuggestion = () => {
|
|
|
|
$scope.newAffiliation = {
|
|
|
|
email: $scope.newAffiliation.email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 10:06:23 -04:00
|
|
|
var _resetMakingRequestType = function() {
|
|
|
|
$scope.ui.isLoadingEmails = false
|
|
|
|
$scope.ui.isProcessing = false
|
|
|
|
$scope.ui.isResendingConfirmation = false
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
var _reset = function() {
|
|
|
|
$scope.ui = {
|
|
|
|
hasError: false,
|
|
|
|
errorMessage: '',
|
|
|
|
showChangeAffiliationUI: false,
|
|
|
|
isMakingRequest: false,
|
|
|
|
isLoadingEmails: false,
|
2019-10-02 10:06:23 -04:00
|
|
|
isAddingNewEmail: false
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
_resetAffiliationToChange()
|
|
|
|
_resetNewAffiliation()
|
|
|
|
return _resetAddingEmail()
|
|
|
|
}
|
|
|
|
_reset()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
var _monitorRequest = function(promise) {
|
|
|
|
$scope.ui.hasError = false
|
|
|
|
$scope.ui.isMakingRequest = true
|
|
|
|
promise
|
|
|
|
.catch(function(response) {
|
|
|
|
$scope.ui.hasError = true
|
|
|
|
return ($scope.ui.errorMessage = __guard__(
|
|
|
|
response != null ? response.data : undefined,
|
|
|
|
x => x.message
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.finally(() => ($scope.ui.isMakingRequest = false))
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2019-09-30 09:21:31 -04:00
|
|
|
$scope.institutionAlreadyLinked = function(emailData) {
|
|
|
|
const institutionId =
|
|
|
|
emailData.affiliation &&
|
|
|
|
emailData.affiliation.institution &&
|
|
|
|
emailData.affiliation.institution &&
|
|
|
|
emailData.affiliation.institution.id
|
|
|
|
? emailData.affiliation.institution.id.toString()
|
|
|
|
: undefined
|
|
|
|
return $scope.linkedInstitutionIds.indexOf(institutionId) !== -1
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:13:18 -04:00
|
|
|
// Populates the emails table
|
|
|
|
var _getUserEmails = function() {
|
2019-10-02 10:06:23 -04:00
|
|
|
_resetMakingRequestType()
|
2019-07-16 05:13:18 -04:00
|
|
|
$scope.ui.isLoadingEmails = true
|
2019-12-09 10:10:07 -05:00
|
|
|
_monitorRequest(UserAffiliationsDataService.getUserEmails())
|
2019-09-30 09:21:31 -04:00
|
|
|
.then(emails => {
|
2019-12-09 10:10:07 -05:00
|
|
|
$scope.userEmails = emails.map(email => {
|
|
|
|
email.ssoAvailable = _ssoAvailable(email.affiliation)
|
|
|
|
return email
|
|
|
|
})
|
2019-09-30 09:21:31 -04:00
|
|
|
$scope.linkedInstitutionIds = emails
|
|
|
|
.filter(email => {
|
|
|
|
if (email.samlProviderId) {
|
|
|
|
return email.samlProviderId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(email => email.samlProviderId)
|
|
|
|
})
|
2019-07-16 05:13:18 -04:00
|
|
|
.finally(() => ($scope.ui.isLoadingEmails = false))
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
return _getUserEmails()
|
|
|
|
}))
|
2018-11-05 05:06:39 -05:00
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|