UserPagesController.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const UserGetter = require('./UserGetter')
  2. const OError = require('@overleaf/o-error')
  3. const UserSessionsManager = require('./UserSessionsManager')
  4. const logger = require('logger-sharelatex')
  5. const Settings = require('@overleaf/settings')
  6. const AuthenticationController = require('../Authentication/AuthenticationController')
  7. const SessionManager = require('../Authentication/SessionManager')
  8. const _ = require('lodash')
  9. const UserPagesController = {
  10. registerPage(req, res) {
  11. const sharedProjectData = {
  12. project_name: req.query.project_name,
  13. user_first_name: req.query.user_first_name,
  14. }
  15. const newTemplateData = {}
  16. if (req.session.templateData != null) {
  17. newTemplateData.templateName = req.session.templateData.templateName
  18. }
  19. res.render('user/register', {
  20. title: 'register',
  21. sharedProjectData,
  22. newTemplateData,
  23. samlBeta: req.session.samlBeta,
  24. })
  25. },
  26. loginPage(req, res) {
  27. // if user is being sent to /login with explicit redirect (redir=/foo),
  28. // such as being sent from the editor to /login, then set the redirect explicitly
  29. if (
  30. req.query.redir != null &&
  31. AuthenticationController._getRedirectFromSession(req) == null
  32. ) {
  33. AuthenticationController.setRedirectInSession(req, req.query.redir)
  34. }
  35. res.render('user/login', {
  36. title: 'login',
  37. })
  38. },
  39. /**
  40. * Landing page for users who may have received one-time login
  41. * tokens from the read-only maintenance site.
  42. *
  43. * We tell them that Overleaf is back up and that they can login normally.
  44. */
  45. oneTimeLoginPage(req, res, next) {
  46. res.render('user/one_time_login')
  47. },
  48. logoutPage(req, res) {
  49. res.render('user/logout')
  50. },
  51. renderReconfirmAccountPage(req, res) {
  52. const pageData = {
  53. reconfirm_email: req.session.reconfirm_email,
  54. }
  55. // when a user must reconfirm their account
  56. res.render('user/reconfirm', pageData)
  57. },
  58. settingsPage(req, res, next) {
  59. const userId = SessionManager.getLoggedInUserId(req.session)
  60. const reconfirmationRemoveEmail = req.query.remove
  61. // SSO
  62. const ssoError = req.session.ssoError
  63. if (ssoError) {
  64. delete req.session.ssoError
  65. }
  66. // Institution SSO
  67. let institutionLinked = _.get(req.session, ['saml', 'linked'])
  68. if (institutionLinked) {
  69. // copy object if exists because _.get does not
  70. institutionLinked = Object.assign(
  71. {
  72. hasEntitlement: _.get(req.session, ['saml', 'hasEntitlement']),
  73. },
  74. institutionLinked
  75. )
  76. }
  77. const samlError = _.get(req.session, ['saml', 'error'])
  78. const institutionEmailNonCanonical = _.get(req.session, [
  79. 'saml',
  80. 'emailNonCanonical',
  81. ])
  82. const institutionRequestedEmail = _.get(req.session, [
  83. 'saml',
  84. 'requestedEmail',
  85. ])
  86. const reconfirmedViaSAML = _.get(req.session, ['saml', 'reconfirmed'])
  87. delete req.session.saml
  88. let shouldAllowEditingDetails = true
  89. if (Settings.ldap && Settings.ldap.updateUserDetailsOnLogin) {
  90. shouldAllowEditingDetails = false
  91. }
  92. if (Settings.saml && Settings.saml.updateUserDetailsOnLogin) {
  93. shouldAllowEditingDetails = false
  94. }
  95. const oauthProviders = Settings.oauthProviders || {}
  96. UserGetter.getUser(userId, (err, user) => {
  97. if (err != null) {
  98. return next(err)
  99. }
  100. res.render('user/settings', {
  101. title: 'account_settings',
  102. user,
  103. hasPassword: !!user.hashedPassword,
  104. shouldAllowEditingDetails,
  105. languages: Settings.languages,
  106. accountSettingsTabActive: true,
  107. oauthProviders: UserPagesController._translateProviderDescriptions(
  108. oauthProviders,
  109. req
  110. ),
  111. oauthUseV2: Settings.oauthUseV2 || false,
  112. institutionLinked,
  113. samlError,
  114. institutionEmailNonCanonical:
  115. institutionEmailNonCanonical && institutionRequestedEmail
  116. ? institutionEmailNonCanonical
  117. : undefined,
  118. reconfirmedViaSAML,
  119. reconfirmationRemoveEmail,
  120. samlBeta: req.session.samlBeta,
  121. ssoError: ssoError,
  122. thirdPartyIds: UserPagesController._restructureThirdPartyIds(user),
  123. })
  124. })
  125. },
  126. sessionsPage(req, res, next) {
  127. const user = SessionManager.getSessionUser(req.session)
  128. logger.log({ userId: user._id }, 'loading sessions page')
  129. UserSessionsManager.getAllUserSessions(user, (err, sessions) => {
  130. if (err != null) {
  131. OError.tag(err, 'error getting all user sessions', {
  132. userId: user._id,
  133. })
  134. return next(err)
  135. }
  136. res.render('user/sessions', {
  137. title: 'sessions',
  138. sessions: sessions.filter(session => session.id !== req.sessionID),
  139. currentSession: sessions.find(session => session.id === req.sessionID),
  140. })
  141. })
  142. },
  143. _restructureThirdPartyIds(user) {
  144. // 3rd party identifiers are an array of objects
  145. // this turn them into a single object, which
  146. // makes data easier to use in template
  147. if (
  148. !user.thirdPartyIdentifiers ||
  149. user.thirdPartyIdentifiers.length === 0
  150. ) {
  151. return null
  152. }
  153. return user.thirdPartyIdentifiers.reduce((obj, identifier) => {
  154. obj[identifier.providerId] = identifier.externalUserId
  155. return obj
  156. }, {})
  157. },
  158. _translateProviderDescriptions(providers, req) {
  159. const result = {}
  160. if (providers) {
  161. for (const provider in providers) {
  162. const data = providers[provider]
  163. data.description = req.i18n.translate(
  164. data.descriptionKey,
  165. Object.assign({}, data.descriptionOptions)
  166. )
  167. result[provider] = data
  168. }
  169. }
  170. return result
  171. },
  172. }
  173. module.exports = UserPagesController