UserAffiliationsReconfirmController.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import _ from 'lodash'
  2. import App from '../../../base'
  3. import getMeta from '../../../utils/meta'
  4. export default App.controller(
  5. 'UserAffiliationsReconfirmController',
  6. function ($scope, $http, $window) {
  7. const samlInitPath = ExposedSettings.samlInitPath
  8. $scope.reconfirm = {}
  9. $scope.ui = $scope.ui || {} // $scope.ui inherited on settings page
  10. $scope.userEmails = getMeta('ol-userEmails')
  11. $scope.reconfirmedViaSAML = getMeta('ol-reconfirmedViaSAML')
  12. // For portals:
  13. const portalAffiliation = getMeta('ol-portalAffiliation')
  14. if (portalAffiliation) {
  15. $scope.portalInReconfirmNotificationPeriod =
  16. portalAffiliation && portalAffiliation.inReconfirmNotificationPeriod
  17. $scope.userEmail = $scope.portalInReconfirmNotificationPeriod // mixin to show notification uses userEmail
  18. }
  19. // For settings page:
  20. $scope.reconfirmationRemoveEmail = getMeta('ol-reconfirmationRemoveEmail')
  21. // For dashboard:
  22. $scope.allInReconfirmNotificationPeriods = getMeta(
  23. 'ol-allInReconfirmNotificationPeriods'
  24. )
  25. function sendReconfirmEmail(email) {
  26. $scope.ui.hasError = false
  27. $scope.ui.isMakingRequest = true
  28. $http
  29. .post('/user/emails/send-reconfirmation', {
  30. email,
  31. _csrf: window.csrfToken,
  32. })
  33. .then(() => {
  34. $scope.reconfirm[email].reconfirmationSent = true
  35. })
  36. .catch(_ => {
  37. $scope.ui.hasError = true
  38. })
  39. .finally(() => ($scope.ui.isMakingRequest = false))
  40. }
  41. $scope.requestReconfirmation = function (obj, userEmail) {
  42. const email = userEmail.email
  43. // For the settings page, disable other parts of affiliation UI
  44. $scope.ui.isMakingRequest = true
  45. $scope.ui.isProcessing = true
  46. // create UI scope for requested email
  47. $scope.reconfirm[email] = $scope.reconfirm[email] || {} // keep existing scope for resend email requests
  48. const location = obj.currentTarget.getAttribute('data-location')
  49. const institutionId = _.get(userEmail, [
  50. 'affiliation',
  51. 'institution',
  52. 'id',
  53. ])
  54. const ssoEnabled = _.get(userEmail, [
  55. 'affiliation',
  56. 'institution',
  57. 'ssoEnabled',
  58. ])
  59. if (ssoEnabled) {
  60. $window.location.href = `${samlInitPath}?university_id=${institutionId}&reconfirm=${location}`
  61. } else {
  62. sendReconfirmEmail(email)
  63. }
  64. }
  65. }
  66. )