LaunchpadController.js 8.0 KB

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