LaunchpadController.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* eslint-disable
  2. n/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. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let LaunchpadController
  14. const OError = require('@overleaf/o-error')
  15. const Settings = require('@overleaf/settings')
  16. const Path = require('path')
  17. const Url = require('url')
  18. const logger = require('@overleaf/logger')
  19. const metrics = require('@overleaf/metrics')
  20. const UserRegistrationHandler = require('../../../../app/src/Features/User/UserRegistrationHandler')
  21. const EmailHandler = require('../../../../app/src/Features/Email/EmailHandler')
  22. const _ = require('lodash')
  23. const UserGetter = require('../../../../app/src/Features/User/UserGetter')
  24. const { User } = require('../../../../app/src/models/User')
  25. const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager')
  26. const AuthenticationController = require('../../../../app/src/Features/Authentication/AuthenticationController')
  27. const SessionManager = require('../../../../app/src/Features/Authentication/SessionManager')
  28. const {
  29. hasAdminAccess,
  30. } = require('../../../../app/src/Features/Helpers/AdminAuthorizationHelper')
  31. module.exports = LaunchpadController = {
  32. _getAuthMethod() {
  33. if (Settings.ldap) {
  34. return 'ldap'
  35. } else if (Settings.saml) {
  36. return 'saml'
  37. } else {
  38. return 'local'
  39. }
  40. },
  41. launchpadPage(req, res, next) {
  42. // TODO: check if we're using external auth?
  43. // * how does all this work with ldap and saml?
  44. const sessionUser = SessionManager.getSessionUser(req.session)
  45. const authMethod = LaunchpadController._getAuthMethod()
  46. LaunchpadController._atLeastOneAdminExists(function (err, adminUserExists) {
  47. if (err != null) {
  48. return next(err)
  49. }
  50. if (!sessionUser) {
  51. if (!adminUserExists) {
  52. res.render(Path.resolve(__dirname, '../views/launchpad'), {
  53. adminUserExists,
  54. authMethod,
  55. })
  56. } else {
  57. AuthenticationController.setRedirectInSession(req)
  58. res.redirect('/login')
  59. }
  60. } else {
  61. UserGetter.getUser(
  62. sessionUser._id,
  63. { isAdmin: 1 },
  64. function (err, user) {
  65. if (err != null) {
  66. return next(err)
  67. }
  68. if (hasAdminAccess(user)) {
  69. res.render(Path.resolve(__dirname, '../views/launchpad'), {
  70. wsUrl: Settings.wsUrl,
  71. adminUserExists,
  72. authMethod,
  73. })
  74. } else {
  75. res.redirect('/restricted')
  76. }
  77. }
  78. )
  79. }
  80. })
  81. },
  82. _atLeastOneAdminExists(callback) {
  83. if (callback == null) {
  84. callback = function () {}
  85. }
  86. UserGetter.getUser(
  87. { isAdmin: true },
  88. { _id: 1, isAdmin: 1 },
  89. function (err, user) {
  90. if (err != null) {
  91. return callback(err)
  92. }
  93. callback(null, user != null)
  94. }
  95. )
  96. },
  97. sendTestEmail(req, res, next) {
  98. const { email } = req.body
  99. if (!email) {
  100. logger.debug({}, 'no email address supplied')
  101. return res.status(400).json({
  102. message: 'no email address supplied',
  103. })
  104. }
  105. logger.debug({ email }, 'sending test email')
  106. const emailOptions = { to: email }
  107. 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.debug({ 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.debug(
  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.debug({ authMethod }, 'no email supplied, disallow')
  130. return res.sendStatus(400)
  131. }
  132. logger.debug({ email }, 'attempted register first admin user')
  133. LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  134. if (err != null) {
  135. return next(err)
  136. }
  137. if (exists) {
  138. logger.debug(
  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.debug(
  151. { body, authMethod },
  152. 'creating admin account for specified external-auth user'
  153. )
  154. UserRegistrationHandler.registerNewUser(body, function (err, user) {
  155. if (err != null) {
  156. OError.tag(err, 'error with registerNewUser', {
  157. email,
  158. authMethod,
  159. })
  160. return next(err)
  161. }
  162. User.updateOne(
  163. { _id: user._id },
  164. {
  165. $set: { isAdmin: true },
  166. emails: [{ email }],
  167. },
  168. function (err) {
  169. if (err != null) {
  170. OError.tag(err, 'error setting user to admin', {
  171. user_id: user._id,
  172. })
  173. return next(err)
  174. }
  175. AuthenticationController.setRedirectInSession(req, '/launchpad')
  176. logger.debug(
  177. { email, userId: user._id, authMethod },
  178. 'created first admin account'
  179. )
  180. res.json({ redir: '/launchpad', email })
  181. }
  182. )
  183. })
  184. })
  185. }
  186. },
  187. registerAdmin(req, res, next) {
  188. const { email } = req.body
  189. const { password } = req.body
  190. if (!email || !password) {
  191. logger.debug({}, 'must supply both email and password, disallow')
  192. return res.sendStatus(400)
  193. }
  194. logger.debug({ email }, 'attempted register first admin user')
  195. LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  196. if (err != null) {
  197. return next(err)
  198. }
  199. if (exists) {
  200. logger.debug(
  201. { email: req.body.email },
  202. 'already have at least one admin user, disallow'
  203. )
  204. return res.status(403).json({
  205. message: { type: 'error', text: 'admin user already exists' },
  206. })
  207. }
  208. const invalidEmail = AuthenticationManager.validateEmail(email)
  209. if (invalidEmail) {
  210. return res
  211. .status(400)
  212. .json({ message: { type: 'error', text: invalidEmail.message } })
  213. }
  214. const invalidPassword = AuthenticationManager.validatePassword(
  215. password,
  216. email
  217. )
  218. if (invalidPassword) {
  219. return res
  220. .status(400)
  221. .json({ message: { type: 'error', text: invalidPassword.message } })
  222. }
  223. const body = { email, password }
  224. UserRegistrationHandler.registerNewUser(body, function (err, user) {
  225. if (err != null) {
  226. return next(err)
  227. }
  228. logger.debug({ userId: user._id }, 'making user an admin')
  229. User.updateOne(
  230. { _id: user._id },
  231. {
  232. $set: {
  233. isAdmin: true,
  234. emails: [{ email }],
  235. },
  236. },
  237. function (err) {
  238. if (err != null) {
  239. OError.tag(err, 'error setting user to admin', {
  240. user_id: user._id,
  241. })
  242. return next(err)
  243. }
  244. logger.debug(
  245. { email, userId: user._id },
  246. 'created first admin account'
  247. )
  248. res.json({ redir: '/launchpad' })
  249. }
  250. )
  251. })
  252. })
  253. },
  254. }