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'
|
2021-04-14 05:00:33 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
export default App.controller(
|
|
|
|
'UserAffiliationsController',
|
|
|
|
function ($scope, $rootScope, UserAffiliationsDataService, $q, $window) {
|
|
|
|
$scope.userEmails = []
|
|
|
|
$scope.linkedInstitutionIds = []
|
|
|
|
$scope.hideInstitutionNotifications = {}
|
|
|
|
$scope.closeInstitutionNotification = type => {
|
|
|
|
$scope.hideInstitutionNotifications[type] = true
|
2020-04-22 10:12:40 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.samlBetaSession = ExposedSettings.hasSamlBeta
|
|
|
|
$scope.samlInitPath = ExposedSettings.samlInitPath
|
|
|
|
$scope.reconfirmationRemoveEmail = getMeta('ol-reconfirmationRemoveEmail')
|
|
|
|
$scope.reconfirmedViaSAML = getMeta('ol-reconfirmedViaSAML')
|
2019-12-09 10:10:07 -05:00
|
|
|
|
2021-04-14 09:17:21 -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
|
|
|
|
2021-04-14 09:17:21 -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 }
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -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
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
_resetAffiliationSuggestion()
|
|
|
|
}
|
|
|
|
return $q.resolve(
|
|
|
|
`${userInputLocalAndDomain.local}@${universityDomain.hostname}`
|
2019-07-16 05:13:18 -04:00
|
|
|
)
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
.catch(function () {
|
2020-01-07 06:03:54 -05:00
|
|
|
_resetAffiliationSuggestion()
|
2021-04-14 09:17:21 -04:00
|
|
|
return $q.reject(null)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
_resetAffiliationSuggestion()
|
|
|
|
return $q.reject(null)
|
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.linkInstitutionAcct = function (email, institutionId) {
|
|
|
|
_resetMakingRequestType()
|
|
|
|
$scope.ui.isMakingRequest = true
|
|
|
|
$scope.ui.isProcessing = true
|
|
|
|
$window.location.href = `${$scope.samlInitPath}?university_id=${institutionId}&auto=/user/settings&email=${email}`
|
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.selectUniversityManually = function () {
|
|
|
|
_resetAffiliationSuggestion()
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = true
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.changeAffiliation = function (userEmail) {
|
|
|
|
if (_.get(userEmail, ['affiliation', 'institution', 'id'])) {
|
|
|
|
UserAffiliationsDataService.getUniversityDetails(
|
|
|
|
userEmail.affiliation.institution.id
|
|
|
|
).then(
|
|
|
|
universityDetails =>
|
|
|
|
($scope.affiliationToChange.university = universityDetails)
|
|
|
|
)
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.affiliationToChange.email = userEmail.email
|
|
|
|
$scope.affiliationToChange.role = userEmail.affiliation.role
|
|
|
|
$scope.affiliationToChange.department = userEmail.affiliation.department
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$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
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.cancelAffiliationChange = email => _resetAffiliationToChange()
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.isChangingAffiliation = email =>
|
|
|
|
$scope.affiliationToChange.email === email
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.showAddEmailForm = () => ($scope.ui.showAddEmailUI = true)
|
|
|
|
|
|
|
|
$scope.addNewEmail = function () {
|
|
|
|
let addEmailPromise
|
|
|
|
if (!$scope.newAffiliation.university) {
|
|
|
|
addEmailPromise = UserAffiliationsDataService.addUserEmail(
|
|
|
|
$scope.newAffiliation.email
|
2019-07-16 05:13:18 -04:00
|
|
|
)
|
|
|
|
} else {
|
2021-04-14 09:17:21 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
addEmailPromise = UserAffiliationsDataService.addUserAffiliation(
|
|
|
|
$scope.newAffiliation.email,
|
|
|
|
$scope.newAffiliation.university.id,
|
|
|
|
$scope.newAffiliation.role,
|
|
|
|
$scope.newAffiliation.department
|
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
|
|
|
|
$scope.ui.isAddingNewEmail = true
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
return _monitorRequest(addEmailPromise)
|
|
|
|
.then(function () {
|
|
|
|
_resetNewAffiliation()
|
|
|
|
_resetAddingEmail()
|
|
|
|
setTimeout(() => _getUserEmails())
|
|
|
|
})
|
|
|
|
.finally(() => ($scope.ui.isAddingNewEmail = false))
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.setDefaultUserEmail = userEmail =>
|
|
|
|
_monitorRequest(
|
|
|
|
UserAffiliationsDataService.setDefaultUserEmail(userEmail.email)
|
|
|
|
).then(function () {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const email of $scope.userEmails || []) {
|
2021-04-14 09:17:21 -04:00
|
|
|
email.default = false
|
|
|
|
}
|
|
|
|
userEmail.default = true
|
|
|
|
window.usersEmail = userEmail.email
|
|
|
|
$rootScope.usersEmail = userEmail.email
|
2019-07-16 05:13:18 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -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
|
|
|
|
2021-04-14 09:17:21 -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
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.acknowledgeError = function () {
|
|
|
|
_reset()
|
|
|
|
return _getUserEmails()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _resetAffiliationToChange = () =>
|
|
|
|
($scope.affiliationToChange = {
|
|
|
|
email: '',
|
|
|
|
university: null,
|
|
|
|
role: null,
|
2021-04-27 03:52:58 -04:00
|
|
|
department: null,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
2020-01-07 06:03:54 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _resetNewAffiliation = () =>
|
|
|
|
($scope.newAffiliation = {
|
|
|
|
email: '',
|
|
|
|
country: null,
|
|
|
|
university: null,
|
|
|
|
role: null,
|
2021-04-27 03:52:58 -04:00
|
|
|
department: null,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
2019-10-02 10:06:23 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _resetAddingEmail = function () {
|
|
|
|
$scope.ui.showAddEmailUI = false
|
|
|
|
$scope.ui.isValidEmail = false
|
|
|
|
$scope.ui.isBlacklistedEmail = false
|
|
|
|
$scope.ui.showManualUniversitySelectionUI = false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _resetAffiliationSuggestion = () => {
|
|
|
|
$scope.newAffiliation = {
|
2021-04-27 03:52:58 -04:00
|
|
|
email: $scope.newAffiliation.email,
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
2019-07-16 05:13:18 -04:00
|
|
|
}
|
2019-09-30 09:21:31 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _resetMakingRequestType = function () {
|
|
|
|
$scope.ui.isLoadingEmails = false
|
|
|
|
$scope.ui.isProcessing = false
|
|
|
|
$scope.ui.isResendingConfirmation = false
|
|
|
|
}
|
2021-03-08 09:53:13 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var _reset = function () {
|
|
|
|
$scope.ui = {
|
|
|
|
hasError: false,
|
|
|
|
errorMessage: '',
|
|
|
|
showChangeAffiliationUI: false,
|
|
|
|
isMakingRequest: false,
|
|
|
|
isLoadingEmails: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
isAddingNewEmail: false,
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
|
|
|
_resetAffiliationToChange()
|
|
|
|
_resetNewAffiliation()
|
|
|
|
return _resetAddingEmail()
|
|
|
|
}
|
|
|
|
_reset()
|
2020-05-19 05:02:56 -04:00
|
|
|
|
2021-04-14 09:17:21 -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'])
|
2019-09-30 09:21:31 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
.finally(() => ($scope.ui.isMakingRequest = false))
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.inReconfirmNotificationPeriod = function (emailData) {
|
|
|
|
return _.get(emailData, ['affiliation', 'inReconfirmNotificationPeriod'])
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
$scope.linkedInstitutionIds = emails
|
|
|
|
.filter(email => {
|
2021-05-13 06:21:35 -04:00
|
|
|
return !!email.samlProviderId
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
.map(email => email.samlProviderId)
|
|
|
|
})
|
|
|
|
.finally(() => ($scope.ui.isLoadingEmails = false))
|
|
|
|
}
|
|
|
|
_getUserEmails()
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|