LaunchpadController.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const OError = require('@overleaf/o-error')
  2. const { expressify } = require('@overleaf/promise-utils')
  3. const Settings = require('@overleaf/settings')
  4. const Path = require('path')
  5. const logger = require('@overleaf/logger')
  6. const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
  7. const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler')
  8. const UserGetter = require('../../../../app/src/Features/User/UserGetter')
  9. const { User } = require('../../../../app/src/models/User')
  10. const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager')
  11. const AuthenticationController = require('../../../../app/src/Features/Authentication/AuthenticationController')
  12. const SessionManager = require('../../../../app/src/Features/Authentication/SessionManager')
  13. const {
  14. hasAdminAccess,
  15. } = require('../../../../app/src/Features/Helpers/AdminAuthorizationHelper')
  16. const _LaunchpadController = {
  17. _getAuthMethod() {
  18. if (Settings.ldap) {
  19. return 'ldap'
  20. } else if (Settings.saml) {
  21. return 'saml'
  22. } else {
  23. return 'local'
  24. }
  25. },
  26. async launchpadPage(req, res) {
  27. // TODO: check if we're using external auth?
  28. // * how does all this work with ldap and saml?
  29. const sessionUser = SessionManager.getSessionUser(req.session)
  30. const authMethod = LaunchpadController._getAuthMethod()
  31. const adminUserExists = await LaunchpadController._atLeastOneAdminExists()
  32. if (!sessionUser) {
  33. if (!adminUserExists) {
  34. res.render(Path.resolve(__dirname, '../views/launchpad'), {
  35. adminUserExists,
  36. authMethod,
  37. })
  38. } else {
  39. AuthenticationController.setRedirectInSession(req)
  40. res.redirect('/login')
  41. }
  42. } else {
  43. const user = await UserGetter.promises.getUser(sessionUser._id, {
  44. isAdmin: 1,
  45. })
  46. if (hasAdminAccess(user)) {
  47. res.render(Path.resolve(__dirname, '../views/launchpad'), {
  48. wsUrl: Settings.wsUrl,
  49. adminUserExists,
  50. authMethod,
  51. })
  52. } else {
  53. res.redirect('/restricted')
  54. }
  55. }
  56. },
  57. async _atLeastOneAdminExists() {
  58. const user = await UserGetter.promises.getUser(
  59. { isAdmin: true },
  60. { _id: 1, isAdmin: 1 }
  61. )
  62. return Boolean(user)
  63. },
  64. async sendTestEmail(req, res) {
  65. const { email } = req.body
  66. if (!email) {
  67. logger.debug({}, 'no email address supplied')
  68. return res.status(400).json({
  69. message: 'no email address supplied',
  70. })
  71. }
  72. logger.debug({ email }, 'sending test email')
  73. const emailOptions = { to: email }
  74. try {
  75. await EmailHandler.promises.sendEmail('testEmail', emailOptions)
  76. logger.debug({ email }, 'sent test email')
  77. res.json({ message: res.locals.translate('email_sent') })
  78. } catch (err) {
  79. OError.tag(err, 'error sending test email', {
  80. email,
  81. })
  82. throw err
  83. }
  84. },
  85. registerExternalAuthAdmin(authMethod) {
  86. return expressify(async function (req, res) {
  87. if (LaunchpadController._getAuthMethod() !== authMethod) {
  88. logger.debug(
  89. { authMethod },
  90. 'trying to register external admin, but that auth service is not enabled, disallow'
  91. )
  92. return res.sendStatus(403)
  93. }
  94. const { email } = req.body
  95. if (!email) {
  96. logger.debug({ authMethod }, 'no email supplied, disallow')
  97. return res.sendStatus(400)
  98. }
  99. logger.debug({ email }, 'attempted register first admin user')
  100. const exists = await LaunchpadController._atLeastOneAdminExists()
  101. if (exists) {
  102. logger.debug(
  103. { email },
  104. 'already have at least one admin user, disallow'
  105. )
  106. return res.sendStatus(403)
  107. }
  108. const body = {
  109. email,
  110. password: 'password_here',
  111. first_name: email,
  112. last_name: '',
  113. }
  114. logger.debug(
  115. { body, authMethod },
  116. 'creating admin account for specified external-auth user'
  117. )
  118. let user
  119. try {
  120. user = await UserRegistrationHandler.promises.registerNewUser(body)
  121. } catch (err) {
  122. OError.tag(err, 'error with registerNewUser', {
  123. email,
  124. authMethod,
  125. })
  126. throw err
  127. }
  128. try {
  129. const reversedHostname = user.email
  130. .split('@')[1]
  131. .split('')
  132. .reverse()
  133. .join('')
  134. await User.updateOne(
  135. { _id: user._id },
  136. {
  137. $set: { isAdmin: true, emails: [{ email, reversedHostname }] },
  138. }
  139. ).exec()
  140. } catch (err) {
  141. OError.tag(err, 'error setting user to admin', {
  142. user_id: user._id,
  143. })
  144. throw err
  145. }
  146. AuthenticationController.setRedirectInSession(req, '/launchpad')
  147. logger.debug(
  148. { email, userId: user._id, authMethod },
  149. 'created first admin account'
  150. )
  151. res.json({ redir: '/launchpad', email })
  152. })
  153. },
  154. async registerAdmin(req, res) {
  155. const { email } = req.body
  156. const { password } = req.body
  157. if (!email || !password) {
  158. logger.debug({}, 'must supply both email and password, disallow')
  159. return res.sendStatus(400)
  160. }
  161. logger.debug({ email }, 'attempted register first admin user')
  162. const exists = await LaunchpadController._atLeastOneAdminExists()
  163. if (exists) {
  164. logger.debug(
  165. { email: req.body.email },
  166. 'already have at least one admin user, disallow'
  167. )
  168. return res.status(403).json({
  169. message: { type: 'error', text: 'admin user already exists' },
  170. })
  171. }
  172. const invalidEmail = AuthenticationManager.validateEmail(email)
  173. if (invalidEmail) {
  174. return res
  175. .status(400)
  176. .json({ message: { type: 'error', text: invalidEmail.message } })
  177. }
  178. const invalidPassword = AuthenticationManager.validatePassword(
  179. password,
  180. email
  181. )
  182. if (invalidPassword) {
  183. return res
  184. .status(400)
  185. .json({ message: { type: 'error', text: invalidPassword.message } })
  186. }
  187. const body = { email, password }
  188. const user = await UserRegistrationHandler.promises.registerNewUser(body)
  189. logger.debug({ userId: user._id }, 'making user an admin')
  190. try {
  191. const reversedHostname = user.email
  192. .split('@')[1]
  193. .split('')
  194. .reverse()
  195. .join('')
  196. await User.updateOne(
  197. { _id: user._id },
  198. {
  199. $set: {
  200. isAdmin: true,
  201. emails: [{ email, reversedHostname }],
  202. },
  203. }
  204. ).exec()
  205. } catch (err) {
  206. OError.tag(err, 'error setting user to admin', {
  207. user_id: user._id,
  208. })
  209. throw err
  210. }
  211. logger.debug({ email, userId: user._id }, 'created first admin account')
  212. res.json({ redir: '/launchpad' })
  213. },
  214. }
  215. const LaunchpadController = {
  216. launchpadPage: expressify(_LaunchpadController.launchpadPage),
  217. registerAdmin: expressify(_LaunchpadController.registerAdmin),
  218. registerExternalAuthAdmin: _LaunchpadController.registerExternalAuthAdmin,
  219. sendTestEmail: expressify(_LaunchpadController.sendTestEmail),
  220. _atLeastOneAdminExists: _LaunchpadController._atLeastOneAdminExists,
  221. _getAuthMethod: _LaunchpadController._getAuthMethod,
  222. }
  223. module.exports = LaunchpadController