LaunchpadController.js 7.4 KB

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