LaunchpadController.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. await User.updateOne(
  130. { _id: user._id },
  131. {
  132. $set: { isAdmin: true },
  133. emails: [{ email }],
  134. }
  135. ).exec()
  136. } catch (err) {
  137. OError.tag(err, 'error setting user to admin', {
  138. user_id: user._id,
  139. })
  140. throw err
  141. }
  142. AuthenticationController.setRedirectInSession(req, '/launchpad')
  143. logger.debug(
  144. { email, userId: user._id, authMethod },
  145. 'created first admin account'
  146. )
  147. res.json({ redir: '/launchpad', email })
  148. })
  149. },
  150. async registerAdmin(req, res) {
  151. const { email } = req.body
  152. const { password } = req.body
  153. if (!email || !password) {
  154. logger.debug({}, 'must supply both email and password, disallow')
  155. return res.sendStatus(400)
  156. }
  157. logger.debug({ email }, 'attempted register first admin user')
  158. const exists = await LaunchpadController._atLeastOneAdminExists()
  159. if (exists) {
  160. logger.debug(
  161. { email: req.body.email },
  162. 'already have at least one admin user, disallow'
  163. )
  164. return res.status(403).json({
  165. message: { type: 'error', text: 'admin user already exists' },
  166. })
  167. }
  168. const invalidEmail = AuthenticationManager.validateEmail(email)
  169. if (invalidEmail) {
  170. return res
  171. .status(400)
  172. .json({ message: { type: 'error', text: invalidEmail.message } })
  173. }
  174. const invalidPassword = AuthenticationManager.validatePassword(
  175. password,
  176. email
  177. )
  178. if (invalidPassword) {
  179. return res
  180. .status(400)
  181. .json({ message: { type: 'error', text: invalidPassword.message } })
  182. }
  183. const body = { email, password }
  184. const user = await UserRegistrationHandler.promises.registerNewUser(body)
  185. logger.debug({ userId: user._id }, 'making user an admin')
  186. try {
  187. await User.updateOne(
  188. { _id: user._id },
  189. {
  190. $set: {
  191. isAdmin: true,
  192. emails: [{ email }],
  193. },
  194. }
  195. ).exec()
  196. } catch (err) {
  197. OError.tag(err, 'error setting user to admin', {
  198. user_id: user._id,
  199. })
  200. throw err
  201. }
  202. logger.debug({ email, userId: user._id }, 'created first admin account')
  203. res.json({ redir: '/launchpad' })
  204. },
  205. }
  206. const LaunchpadController = {
  207. launchpadPage: expressify(_LaunchpadController.launchpadPage),
  208. registerAdmin: expressify(_LaunchpadController.registerAdmin),
  209. registerExternalAuthAdmin: _LaunchpadController.registerExternalAuthAdmin,
  210. sendTestEmail: expressify(_LaunchpadController.sendTestEmail),
  211. _atLeastOneAdminExists: _LaunchpadController._atLeastOneAdminExists,
  212. _getAuthMethod: _LaunchpadController._getAuthMethod,
  213. }
  214. module.exports = LaunchpadController