LaunchpadController.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. * 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. 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. return LaunchpadController._atLeastOneAdminExists(function (
  47. err,
  48. adminUserExists
  49. ) {
  50. if (err != null) {
  51. return next(err)
  52. }
  53. if (!sessionUser) {
  54. if (!adminUserExists) {
  55. return res.render(Path.resolve(__dirname, '../views/launchpad'), {
  56. adminUserExists,
  57. authMethod,
  58. })
  59. } else {
  60. AuthenticationController.setRedirectInSession(req)
  61. return res.redirect('/login')
  62. }
  63. } else {
  64. return UserGetter.getUser(
  65. sessionUser._id,
  66. { isAdmin: 1 },
  67. function (err, user) {
  68. if (err != null) {
  69. return next(err)
  70. }
  71. if (hasAdminAccess(user)) {
  72. return res.render(Path.resolve(__dirname, '../views/launchpad'), {
  73. wsUrl: Settings.wsUrl,
  74. adminUserExists,
  75. authMethod,
  76. })
  77. } else {
  78. return res.redirect('/restricted')
  79. }
  80. }
  81. )
  82. }
  83. })
  84. },
  85. _atLeastOneAdminExists(callback) {
  86. if (callback == null) {
  87. callback = function () {}
  88. }
  89. return UserGetter.getUser(
  90. { isAdmin: true },
  91. { _id: 1, isAdmin: 1 },
  92. function (err, user) {
  93. if (err != null) {
  94. return callback(err)
  95. }
  96. return callback(null, user != null)
  97. }
  98. )
  99. },
  100. sendTestEmail(req, res, next) {
  101. const { email } = req.body
  102. if (!email) {
  103. logger.debug({}, 'no email address supplied')
  104. return res.status(400).json({
  105. message: 'no email address supplied',
  106. })
  107. }
  108. logger.debug({ email }, 'sending test email')
  109. const emailOptions = { to: email }
  110. return EmailHandler.sendEmail('testEmail', emailOptions, function (err) {
  111. if (err != null) {
  112. OError.tag(err, 'error sending test email', {
  113. email,
  114. })
  115. return next(err)
  116. }
  117. logger.debug({ email }, 'sent test email')
  118. res.json({ message: res.locals.translate('email_sent') })
  119. })
  120. },
  121. registerExternalAuthAdmin(authMethod) {
  122. return function (req, res, next) {
  123. if (LaunchpadController._getAuthMethod() !== authMethod) {
  124. logger.debug(
  125. { authMethod },
  126. 'trying to register external admin, but that auth service is not enabled, disallow'
  127. )
  128. return res.sendStatus(403)
  129. }
  130. const { email } = req.body
  131. if (!email) {
  132. logger.debug({ authMethod }, 'no email supplied, disallow')
  133. return res.sendStatus(400)
  134. }
  135. logger.debug({ email }, 'attempted register first admin user')
  136. return LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  137. if (err != null) {
  138. return next(err)
  139. }
  140. if (exists) {
  141. logger.debug(
  142. { email },
  143. 'already have at least one admin user, disallow'
  144. )
  145. return res.sendStatus(403)
  146. }
  147. const body = {
  148. email,
  149. password: 'password_here',
  150. first_name: email,
  151. last_name: '',
  152. }
  153. logger.debug(
  154. { body, authMethod },
  155. 'creating admin account for specified external-auth user'
  156. )
  157. return UserRegistrationHandler.registerNewUser(
  158. body,
  159. function (err, user) {
  160. if (err != null) {
  161. OError.tag(err, 'error with registerNewUser', {
  162. email,
  163. authMethod,
  164. })
  165. return next(err)
  166. }
  167. return User.updateOne(
  168. { _id: user._id },
  169. {
  170. $set: { isAdmin: true },
  171. emails: [{ email }],
  172. },
  173. function (err) {
  174. if (err != null) {
  175. OError.tag(err, 'error setting user to admin', {
  176. user_id: user._id,
  177. })
  178. return next(err)
  179. }
  180. AuthenticationController.setRedirectInSession(req, '/launchpad')
  181. logger.debug(
  182. { email, user_id: user._id, authMethod },
  183. 'created first admin account'
  184. )
  185. return res.json({ redir: '/launchpad', email })
  186. }
  187. )
  188. }
  189. )
  190. })
  191. }
  192. },
  193. registerAdmin(req, res, next) {
  194. const { email } = req.body
  195. const { password } = req.body
  196. if (!email || !password) {
  197. logger.debug({}, 'must supply both email and password, disallow')
  198. return res.sendStatus(400)
  199. }
  200. logger.debug({ email }, 'attempted register first admin user')
  201. return LaunchpadController._atLeastOneAdminExists(function (err, exists) {
  202. if (err != null) {
  203. return next(err)
  204. }
  205. if (exists) {
  206. logger.debug(
  207. { email: req.body.email },
  208. 'already have at least one admin user, disallow'
  209. )
  210. return res.sendStatus(403)
  211. }
  212. const body = { email, password }
  213. return UserRegistrationHandler.registerNewUser(
  214. body,
  215. function (err, user) {
  216. if (err != null) {
  217. return next(err)
  218. }
  219. logger.debug({ user_id: user._id }, 'making user an admin')
  220. User.updateOne(
  221. { _id: user._id },
  222. {
  223. $set: {
  224. isAdmin: true,
  225. emails: [{ email }],
  226. },
  227. },
  228. function (err) {
  229. if (err != null) {
  230. OError.tag(err, 'error setting user to admin', {
  231. user_id: user._id,
  232. })
  233. return next(err)
  234. }
  235. logger.debug(
  236. { email, user_id: user._id },
  237. 'created first admin account'
  238. )
  239. res.json({ redir: '/launchpad' })
  240. }
  241. )
  242. }
  243. )
  244. })
  245. },
  246. }