LaunchpadController.js 8.3 KB

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