LaunchpadController.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. wsUrl: Settings.wsUrl,
  68. adminUserExists,
  69. authMethod
  70. })
  71. } else {
  72. return res.redirect('/restricted')
  73. }
  74. })
  75. }
  76. })
  77. },
  78. _atLeastOneAdminExists(callback) {
  79. if (callback == null) {
  80. callback = function(err, exists) {}
  81. }
  82. return UserGetter.getUser(
  83. { isAdmin: true },
  84. { _id: 1, isAdmin: 1 },
  85. function(err, user) {
  86. if (err != null) {
  87. return callback(err)
  88. }
  89. return callback(null, user != null)
  90. }
  91. )
  92. },
  93. sendTestEmail(req, res, next) {
  94. const { email } = req.body
  95. if (!email) {
  96. logger.log({}, 'no email address supplied')
  97. return res.sendStatus(400)
  98. }
  99. logger.log({ email }, 'sending test email')
  100. const emailOptions = { to: email }
  101. return EmailHandler.sendEmail('testEmail', emailOptions, function(err) {
  102. if (err != null) {
  103. logger.warn({ email }, 'error sending test email')
  104. return next(err)
  105. }
  106. logger.log({ email }, 'sent test email')
  107. return res.sendStatus(201)
  108. })
  109. },
  110. registerExternalAuthAdmin(authMethod) {
  111. return function(req, res, next) {
  112. if (LaunchpadController._getAuthMethod() !== authMethod) {
  113. logger.log(
  114. { authMethod },
  115. 'trying to register external admin, but that auth service is not enabled, disallow'
  116. )
  117. return res.sendStatus(403)
  118. }
  119. const { email } = req.body
  120. if (!email) {
  121. logger.log({ authMethod }, 'no email supplied, disallow')
  122. return res.sendStatus(400)
  123. }
  124. logger.log({ email }, 'attempted register first admin user')
  125. return LaunchpadController._atLeastOneAdminExists(function(err, exists) {
  126. if (err != null) {
  127. return next(err)
  128. }
  129. if (exists) {
  130. logger.log(
  131. { email },
  132. 'already have at least one admin user, disallow'
  133. )
  134. return res.sendStatus(403)
  135. }
  136. const body = {
  137. email,
  138. password: 'password_here',
  139. first_name: email,
  140. last_name: ''
  141. }
  142. logger.log(
  143. { body, authMethod },
  144. 'creating admin account for specified external-auth user'
  145. )
  146. return UserRegistrationHandler.registerNewUser(body, function(
  147. err,
  148. user
  149. ) {
  150. if (err != null) {
  151. logger.warn(
  152. { err, email, authMethod },
  153. 'error with registerNewUser'
  154. )
  155. return next(err)
  156. }
  157. return User.update(
  158. { _id: user._id },
  159. {
  160. $set: { isAdmin: true },
  161. emails: [{ email }]
  162. },
  163. function(err) {
  164. if (err != null) {
  165. logger.warn(
  166. { user_id: user._id, err },
  167. 'error setting user to admin'
  168. )
  169. return next(err)
  170. }
  171. AuthenticationController.setRedirectInSession(req, '/launchpad')
  172. logger.log(
  173. { email, user_id: user._id, authMethod },
  174. 'created first admin account'
  175. )
  176. return res.json({ redir: '/launchpad', email })
  177. }
  178. )
  179. })
  180. })
  181. }
  182. },
  183. registerAdmin(req, res, next) {
  184. const { email } = req.body
  185. const { password } = req.body
  186. if (!email || !password) {
  187. logger.log({}, 'must supply both email and password, disallow')
  188. return res.sendStatus(400)
  189. }
  190. logger.log({ email }, 'attempted register first admin user')
  191. return LaunchpadController._atLeastOneAdminExists(function(err, exists) {
  192. if (err != null) {
  193. return next(err)
  194. }
  195. if (exists) {
  196. logger.log(
  197. { email: req.body.email },
  198. 'already have at least one admin user, disallow'
  199. )
  200. return res.sendStatus(403)
  201. }
  202. const body = { email, password }
  203. return UserRegistrationHandler.registerNewUser(body, function(err, user) {
  204. if (err != null) {
  205. return next(err)
  206. }
  207. logger.log({ user_id: user._id }, 'making user an admin')
  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. })
  240. })
  241. }
  242. }