2020-06-23 04:45:38 -04:00
|
|
|
import _ from 'lodash'
|
2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
export default App.controller('UserAffiliationsController', function(
|
|
|
|
$scope,
|
2020-10-30 08:33:25 -04:00
|
|
|
$rootScope,
|
2020-05-19 05:02:56 -04:00
|
|
|
UserAffiliationsDataService,
|
|
|
|
$q,
|
2020-06-23 04:45:38 -04:00
|
|
|
$window
|
2020-05-19 05:02:56 -04:00
|
|
|
) {
|
|
|
|
$scope.userEmails = []
|
|
|
|
$scope.linkedInstitutionIds = []
|
|
|
|
$scope.hideInstitutionNotifications = {}
|
|
|
|
$scope.closeInstitutionNotification = type => {
|
|
|
|
$scope.hideInstitutionNotifications[type] = true
|
|
|
|
}
|
|
|
|
$scope.samlBetaSession = ExposedSettings.hasSamlBeta
|
|
|
|
$scope.samlInitPath = ExposedSettings.samlInitPath
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -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,}))$/
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const _matchLocalAndDomain = function(userEmailInput) {
|
|
|
|
const match = userEmailInput
|
|
|
|
? userEmailInput.match(LOCAL_AND_DOMAIN_REGEX)
|
|
|
|
: undefined
|
|
|
|
if (match) {
|
|
|
|
return { local: match[1], domain: match[2] }
|
|
|
|
} else {
|
|
|
|
return { local: null, domain: null }
|
2020-04-22 10:12:40 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const _ssoAvailableForAffiliation = affiliation => {
|
|
|
|
if (!affiliation) return false
|
|
|
|
const institution = affiliation.institution
|
|
|
|
if (!_ssoAvailableForInstitution(institution)) return false
|
|
|
|
if (!institution.confirmed) return false // domain is confirmed, not the email
|
|
|
|
return true
|
|
|
|
}
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const _ssoAvailableForDomain = domain => {
|
|
|
|
if (!domain) return false
|
|
|
|
if (!domain.confirmed) return false // domain is confirmed, not the email
|
|
|
|
const institution = domain.university
|
|
|
|
if (!_ssoAvailableForInstitution(institution)) return false
|
|
|
|
return true
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const _ssoAvailableForInstitution = institution => {
|
|
|
|
if (!ExposedSettings.hasSamlFeature) return false
|
|
|
|
if (!institution) return false
|
|
|
|
if (institution.ssoEnabled) return true
|
|
|
|
if ($scope.samlBetaSession && institution.ssoBeta) return true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
$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
|
|
|
|
$scope.newAffiliation.ssoAvailable = _ssoAvailableForDomain(
|
|
|
|
universityDomain
|
2019-07-16 05:13:18 -04:00
|
|
|
)
|
2020-05-19 05:02:56 -04:00
|
|
|
} else {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
return $q.resolve(
|
|
|
|
`${userInputLocalAndDomain.local}@${universityDomain.hostname}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.catch(function() {
|
|
|
|
_resetAffiliationSuggestion()
|
|
|
|
return $q.reject(null)
|
|
|
|
})
|
|
|
|
} else {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2020-05-19 05:02:56 -04:00
|
|
|
return $q.reject(null)
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.linkInstitutionAcct = function(email, institutionId) {
|
|
|
|
_resetMakingRequestType()
|
|
|
|
$scope.ui.isMakingRequest = true
|
|
|
|
$scope.ui.isProcessing = true
|
2020-12-15 05:23:54 -05:00
|
|
|
$window.location.href = `${$scope.samlInitPath}?university_id=${institutionId}&auto=/user/settings&email=${email}`
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.selectUniversityManually = function() {
|
|
|
|
_resetAffiliationSuggestion()
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = true
|
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.changeAffiliation = function(userEmail) {
|
|
|
|
if (_.get(userEmail, ['affiliation', 'institution', 'id'])) {
|
|
|
|
UserAffiliationsDataService.getUniversityDetails(
|
|
|
|
userEmail.affiliation.institution.id
|
|
|
|
).then(
|
|
|
|
universityDetails =>
|
|
|
|
($scope.affiliationToChange.university = universityDetails)
|
|
|
|
)
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.affiliationToChange.email = userEmail.email
|
|
|
|
$scope.affiliationToChange.role = userEmail.affiliation.role
|
|
|
|
$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
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.cancelAffiliationChange = email => _resetAffiliationToChange()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.isChangingAffiliation = email =>
|
|
|
|
$scope.affiliationToChange.email === email
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.showAddEmailForm = () => ($scope.ui.showAddEmailUI = true)
|
|
|
|
|
|
|
|
$scope.addNewEmail = function() {
|
|
|
|
let addEmailPromise
|
|
|
|
if (!$scope.newAffiliation.university) {
|
|
|
|
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
|
2019-07-16 05:13:18 -04:00
|
|
|
)
|
|
|
|
} else {
|
2020-05-19 05:02:56 -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
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.ui.isAddingNewEmail = true
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
return _monitorRequest(addEmailPromise)
|
|
|
|
.then(function() {
|
|
|
|
_resetNewAffiliation()
|
|
|
|
_resetAddingEmail()
|
|
|
|
setTimeout(() => _getUserEmails())
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
.finally(() => ($scope.ui.isAddingNewEmail = false))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.setDefaultUserEmail = userEmail =>
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService.setDefaultUserEmail(userEmail.email)
|
|
|
|
).then(function() {
|
|
|
|
for (let email of $scope.userEmails || []) {
|
|
|
|
email.default = false
|
|
|
|
}
|
|
|
|
userEmail.default = true
|
2020-10-22 04:11:43 -04:00
|
|
|
window.usersEmail = userEmail.email
|
2020-10-30 08:33:25 -04:00
|
|
|
$rootScope.usersEmail = userEmail.email
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -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
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.resendConfirmationEmail = function(userEmail) {
|
|
|
|
_resetMakingRequestType()
|
|
|
|
$scope.ui.isResendingConfirmation = true
|
|
|
|
return _monitorRequest(
|
|
|
|
UserAffiliationsDataService.resendConfirmationEmail(userEmail.email)
|
|
|
|
).finally(() => ($scope.ui.isResendingConfirmation = false))
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.acknowledgeError = function() {
|
|
|
|
_reset()
|
|
|
|
return _getUserEmails()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _resetAffiliationToChange = () =>
|
|
|
|
($scope.affiliationToChange = {
|
|
|
|
email: '',
|
|
|
|
university: null,
|
|
|
|
role: null,
|
|
|
|
department: null
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _resetNewAffiliation = () =>
|
|
|
|
($scope.newAffiliation = {
|
|
|
|
email: '',
|
|
|
|
country: null,
|
|
|
|
university: null,
|
|
|
|
role: null,
|
|
|
|
department: null
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _resetAddingEmail = function() {
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
$scope.ui.isValidEmail = false
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
|
|
|
}
|
2020-01-07 06:03:54 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _resetAffiliationSuggestion = () => {
|
|
|
|
$scope.newAffiliation = {
|
|
|
|
email: $scope.newAffiliation.email
|
2019-10-02 10:06:23 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2019-10-02 10:06:23 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _resetMakingRequestType = function() {
|
|
|
|
$scope.ui.isLoadingEmails = false
|
|
|
|
$scope.ui.isProcessing = false
|
|
|
|
$scope.ui.isResendingConfirmation = false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _reset = function() {
|
|
|
|
$scope.ui = {
|
|
|
|
hasError: false,
|
|
|
|
errorMessage: '',
|
|
|
|
showChangeAffiliationUI: false,
|
|
|
|
isMakingRequest: false,
|
|
|
|
isLoadingEmails: false,
|
|
|
|
isAddingNewEmail: false
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
_resetAffiliationToChange()
|
|
|
|
_resetNewAffiliation()
|
|
|
|
return _resetAddingEmail()
|
|
|
|
}
|
|
|
|
_reset()
|
2019-07-16 05:13:18 -04:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
var _monitorRequest = function(promise) {
|
|
|
|
$scope.ui.hasError = false
|
|
|
|
$scope.ui.isMakingRequest = true
|
|
|
|
promise
|
|
|
|
.catch(function(response) {
|
|
|
|
$scope.ui.hasError = true
|
|
|
|
$scope.ui.errorMessage = _.get(response, ['data', 'message'])
|
|
|
|
})
|
|
|
|
.finally(() => ($scope.ui.isMakingRequest = false))
|
|
|
|
return promise
|
|
|
|
}
|
2019-09-30 09:21:31 -04:00
|
|
|
|
2020-05-19 05:02:56 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates the emails table
|
|
|
|
var _getUserEmails = function() {
|
|
|
|
_resetMakingRequestType()
|
|
|
|
$scope.ui.isLoadingEmails = true
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService.getUserEmailsEnsureAffiliation()
|
|
|
|
)
|
|
|
|
.then(emails => {
|
|
|
|
$scope.userEmails = emails.map(email => {
|
|
|
|
email.ssoAvailable = _ssoAvailableForAffiliation(email.affiliation)
|
|
|
|
return email
|
2019-09-30 09:21:31 -04:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
$scope.linkedInstitutionIds = emails
|
|
|
|
.filter(email => {
|
|
|
|
if (email.samlProviderId) {
|
|
|
|
return email.samlProviderId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(email => email.samlProviderId)
|
|
|
|
})
|
|
|
|
.finally(() => ($scope.ui.isLoadingEmails = false))
|
|
|
|
}
|
|
|
|
_getUserEmails()
|
|
|
|
})
|