LaunchpadController.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let LaunchpadController
  15. const OError = require('@overleaf/o-error')
  16. const Settings = require('@overleaf/settings')
  17. const Path = require('path')
  18. const Url = require('url')
  19. const logger = require('@overleaf/logger')
  20. const metrics = require('@overleaf/metrics')
  21. const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
  22. const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler')
  23. const _ = require('underscore')
  24. const UserGetter = require('../../../../app/src/Features/User/UserGetter')
  25. const { User } = require('../../../../app/src/models/User')
  26. const AuthenticationController = require('../../../../app/src/Features/Authentication/AuthenticationController')
  27. const SessionManager = require('../../../../app/src/Features/Authentication/SessionManager')
  28. module.exports = LaunchpadController = {
  29. _getAuthMethod() {
  30. if (Settings.ldap) {
  31. return 'ldap'
  32. } else if (Settings.saml) {
  33. return 'saml'
  34. } else {
  35. return 'local'
  36. }
  37. },
  38. launchpadPage(req, res, next) {
  39. // TODO: check if we're using external auth?
  40. // * how does all this work with ldap and saml?
  41. const sessionUser = SessionManager.getSessionUser(req.session)
  42. const authMethod = LaunchpadController._getAuthMethod()
  43. return LaunchpadController._atLeastOneAdminExists(function (
  44. err,
  45. adminUserExists
  46. ) {
  47. if (err != null) {
  48. return next(err)
  49. }
  50. if (!sessionUser) {
  51. if (!adminUserExists) {
  52. return res.render(Path.resolve(__dirname, '../views/launchpad'), {
  53. adminUserExists,
  54. authMethod,
  55. })
  56. } else {
  57. AuthenticationController.setRedirectInSession(req)
  58. return res.redirect('/login')
  59. }
  60. } else {
  61. return UserGetter.getUser(
  62. sessionUser._id,
  63. { isAdmin: 1 },
  64. function (err, user) {
  65. if (err != null) {
  66. return next(err)
  67. }
  68. if (user && user.isAdmin) {
  69. return res.render(Path.resolve(__dirname, '../views/launchpad'), {
  70. wsUrl: Settings.wsUrl,
  71. adminUserExists,
  72. authMethod,
  73. })
  74. } else {
  75. return res.redirect('/restricted')
  76. }
  77. }
  78. )
  79. }
  80. })
  81. },
  82. _atLeastOneAdminExists(callback) {
  83. if (callback == null) {
  84. callback = function () {}
  85. }
  86. return UserGetter.getUser(
  87. { isAdmin: true },
  88. { _id: 1, isAdmin: 1 },
  89. function (err, user) {
  90. if (err != null) {
  91. return callback(err)
  92. }
  93. return callback(null, user != null)
  94. }
  95. )
  96. },
  97. sendTestEmail(req, res, next) {
  98. const { email } = req.body
  99. if (!email) {
  100. logger.log({}, 'no email address supplied')
  101. return res.status(400).json({
  102. message: 'no email address supplied',
  103. })
  104. }
  105. logger.log({ email }, 'sending test email')
  106. const emailOptions = { to: email }
  107. return EmailHandler.sendEmail('testEmail', emailOptions, function (err) {
  108. if (err != null) {
  109. OError.tag(err, 'error sending test email', {
  110. email,
  111. })
  112. return next(err)
  113. }
  114. logger.log({ email }, 'sent test email')
  115. res.json({ message: res.locals.translate('email_sent') })
  116. })
  117. },
  118. registerExternalAuthAdmin(authMethod) {
  119. return function (req, res, next) {
  120. if (LaunchpadController._getAuthMethod() !== authMethod) {
  121. logger.log(
  122. { authMethod },
  123. 'trying to register external admin, but that auth service is not enabled, disallow'
  124. )
  125. return res.sendStatus(403)
  126. }
  127. const { email } = req.body
  128. if (!email) {
  129. logger.log({ authMethod }, 'no email supplied, disallow')
  130. return res.sendStatus(400)
  131. }
  132. logger.log({ email }, 'attempted register first admin user')
  133. return LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  134. if (err != null) {
  135. return next(err)
  136. }
  137. if (exists) {
  138. logger.log(
  139. { email },
  140. 'already have at least one admin user, disallow'
  141. )
  142. return res.sendStatus(403)
  143. }
  144. const body = {
  145. email,
  146. password: 'password_here',
  147. first_name: email,
  148. last_name: '',
  149. }
  150. logger.log(
  151. { body, authMethod },
  152. 'creating admin account for specified external-auth user'
  153. )
  154. return UserRegistrationHandler.registerNewUser(
  155. body,
  156. function (err, user) {
  157. if (err != null) {
  158. OError.tag(err, 'error with registerNewUser', {
  159. email,
  160. authMethod,
  161. })
  162. return next(err)
  163. }
  164. return User.updateOne(
  165. { _id: user._id },
  166. {
  167. $set: { isAdmin: true },
  168. emails: [{ email }],
  169. },
  170. function (err) {
  171. if (err != null) {
  172. OError.tag(err, 'error setting user to admin', {
  173. user_id: user._id,
  174. })
  175. return next(err)
  176. }
  177. AuthenticationController.setRedirectInSession(req, '/launchpad')
  178. logger.log(
  179. { email, user_id: user._id, authMethod },
  180. 'created first admin account'
  181. )
  182. return res.json({ redir: '/launchpad', email })
  183. }
  184. )
  185. }
  186. )
  187. })
  188. }
  189. },
  190. registerAdmin(req, res, next) {
  191. const { email } = req.body
  192. const { password } = req.body
  193. if (!email || !password) {
  194. logger.log({}, 'must supply both email and password, disallow')
  195. return res.sendStatus(400)
  196. }
  197. logger.log({ email }, 'attempted register first admin user')
  198. return LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  199. if (err != null) {
  200. return next(err)
  201. }
  202. if (exists) {
  203. logger.log(
  204. { email: req.body.email },
  205. 'already have at least one admin user, disallow'
  206. )
  207. return res.sendStatus(403)
  208. }
  209. const body = { email, password }
  210. return UserRegistrationHandler.registerNewUser(
  211. body,
  212. function (err, user) {
  213. if (err != null) {
  214. return next(err)
  215. }
  216. logger.log({ user_id: user._id }, 'making user an admin')
  217. User.updateOne(
  218. { _id: user._id },
  219. {
  220. $set: {
  221. isAdmin: true,
  222. emails: [{ email }],
  223. },
  224. },
  225. function (err) {
  226. if (err != null) {
  227. OError.tag(err, 'error setting user to admin', {
  228. user_id: user._id,
  229. })
  230. return next(err)
  231. }
  232. logger.log(
  233. { email, user_id: user._id },
  234. 'created first admin account'
  235. )
  236. res.json({ redir: '/launchpad' })
  237. }
  238. )
  239. }
  240. )
  241. })
  242. },
  243. }