LaunchpadController.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // Ignore spurious floating promises warning until we promisify
  163. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  164. 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.debug(
  179. { email, userId: user._id, authMethod },
  180. 'created first admin account'
  181. )
  182. res.json({ redir: '/launchpad', email })
  183. }
  184. )
  185. })
  186. })
  187. }
  188. },
  189. registerAdmin(req, res, next) {
  190. const { email } = req.body
  191. const { password } = req.body
  192. if (!email || !password) {
  193. logger.debug({}, 'must supply both email and password, disallow')
  194. return res.sendStatus(400)
  195. }
  196. logger.debug({ email }, 'attempted register first admin user')
  197. LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  198. if (err != null) {
  199. return next(err)
  200. }
  201. if (exists) {
  202. logger.debug(
  203. { email: req.body.email },
  204. 'already have at least one admin user, disallow'
  205. )
  206. return res.status(403).json({
  207. message: { type: 'error', text: 'admin user already exists' },
  208. })
  209. }
  210. const invalidEmail = AuthenticationManager.validateEmail(email)
  211. if (invalidEmail) {
  212. return res
  213. .status(400)
  214. .json({ message: { type: 'error', text: invalidEmail.message } })
  215. }
  216. const invalidPassword = AuthenticationManager.validatePassword(
  217. password,
  218. email
  219. )
  220. if (invalidPassword) {
  221. return res
  222. .status(400)
  223. .json({ message: { type: 'error', text: invalidPassword.message } })
  224. }
  225. const body = { email, password }
  226. UserRegistrationHandler.registerNewUser(body, function (err, user) {
  227. if (err != null) {
  228. return next(err)
  229. }
  230. logger.debug({ userId: user._id }, 'making user an admin')
  231. // Ignore spurious floating promises warning until we promisify
  232. // eslint-disable-next-line @typescript-eslint/no-floating-promises
  233. User.updateOne(
  234. { _id: user._id },
  235. {
  236. $set: {
  237. isAdmin: true,
  238. emails: [{ email }],
  239. },
  240. },
  241. function (err) {
  242. if (err != null) {
  243. OError.tag(err, 'error setting user to admin', {
  244. user_id: user._id,
  245. })
  246. return next(err)
  247. }
  248. logger.debug(
  249. { email, userId: user._id },
  250. 'created first admin account'
  251. )
  252. res.json({ redir: '/launchpad' })
  253. }
  254. )
  255. })
  256. })
  257. },
  258. }