LaunchpadController.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. {
  159. $set: { isAdmin: true },
  160. emails: [{ email }]
  161. },
  162. function(err) {
  163. if (err != null) {
  164. logger.warn(
  165. { user_id: user._id, err },
  166. 'error setting user to admin'
  167. )
  168. return next(err)
  169. }
  170. AuthenticationController.setRedirectInSession(req, '/launchpad')
  171. logger.log(
  172. { email, user_id: user._id, authMethod },
  173. 'created first admin account'
  174. )
  175. return res.json({ redir: '/launchpad', email })
  176. }
  177. )
  178. })
  179. })
  180. }
  181. },
  182. registerAdmin(req, res, next) {
  183. const { email } = req.body
  184. const { password } = req.body
  185. if (!email || !password) {
  186. logger.log({}, 'must supply both email and password, disallow')
  187. return res.sendStatus(400)
  188. }
  189. logger.log({ email }, 'attempted register first admin user')
  190. return LaunchpadController._atLeastOneAdminExists(function(err, exists) {
  191. if (err != null) {
  192. return next(err)
  193. }
  194. if (exists) {
  195. logger.log(
  196. { email: req.body.email },
  197. 'already have at least one admin user, disallow'
  198. )
  199. return res.sendStatus(403)
  200. }
  201. const body = { email, password }
  202. return UserRegistrationHandler.registerNewUser(body, function(err, user) {
  203. if (err != null) {
  204. return next(err)
  205. }
  206. logger.log({ user_id: user._id }, 'making user an admin')
  207. const proceed = () =>
  208. User.update(
  209. { _id: user._id },
  210. {
  211. $set: {
  212. isAdmin: true,
  213. emails: [{ email }]
  214. }
  215. },
  216. function(err) {
  217. if (err != null) {
  218. logger.err(
  219. { user_id: user._id, err },
  220. 'error setting user to admin'
  221. )
  222. return next(err)
  223. }
  224. AuthenticationController.setRedirectInSession(req, '/launchpad')
  225. logger.log(
  226. { email, user_id: user._id },
  227. 'created first admin account'
  228. )
  229. return res.json({
  230. redir: '',
  231. id: user._id.toString(),
  232. first_name: user.first_name,
  233. last_name: user.last_name,
  234. email: user.email,
  235. created: Date.now()
  236. })
  237. }
  238. )
  239. if (
  240. Settings.overleaf != null &&
  241. Settings.createV1AccountOnLogin != null
  242. ) {
  243. logger.log(
  244. { user_id: user._id },
  245. 'Creating backing account in v1 for new admin user'
  246. )
  247. const SharelatexAuthController = require('../../../overleaf-integration/app/src/SharelatexAuth/SharelatexAuthController')
  248. return UserGetter.getUser(user._id, function(err, user) {
  249. if (err != null) {
  250. return next(err)
  251. }
  252. return SharelatexAuthController._createBackingAccountIfNeeded(
  253. user,
  254. req,
  255. function(err) {
  256. if (err != null) {
  257. return next(err)
  258. }
  259. return proceed()
  260. }
  261. )
  262. })
  263. } else {
  264. return proceed()
  265. }
  266. })
  267. })
  268. }
  269. }