Features.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const _ = require('lodash')
  2. const Settings = require('@overleaf/settings')
  3. const publicRegistrationModuleAvailable =
  4. Settings.moduleImportSequence.includes('public-registration')
  5. const supportModuleAvailable = Settings.moduleImportSequence.includes('support')
  6. const historyV1ModuleAvailable =
  7. Settings.moduleImportSequence.includes('history-v1')
  8. const trackChangesModuleAvailable =
  9. Settings.moduleImportSequence.includes('track-changes')
  10. /**
  11. * @typedef {Object} Settings
  12. * @property {Object | undefined} apis
  13. * @property {Object | undefined} apis.linkedUrlProxy
  14. * @property {string | undefined} apis.linkedUrlProxy.url
  15. * @property {Object | undefined} apis.references
  16. * @property {string | undefined} apis.references.url
  17. * @property {boolean | undefined} enableGithubSync
  18. * @property {boolean | undefined} enableGitBridge
  19. * @property {boolean | undefined} enableHomepage
  20. * @property {boolean | undefined} enableSaml
  21. * @property {boolean | undefined} ldap
  22. * @property {boolean | undefined} oauth
  23. * @property {Object | undefined} overleaf
  24. * @property {Object | undefined} overleaf.oauth
  25. * @property {boolean | undefined} saml
  26. */
  27. const Features = {
  28. /**
  29. * @returns {boolean}
  30. */
  31. externalAuthenticationSystemUsed() {
  32. return (
  33. (Boolean(Settings.ldap) && Boolean(Settings.ldap.enable)) ||
  34. (Boolean(Settings.saml) && Boolean(Settings.saml.enable)) ||
  35. Boolean(_.get(Settings, ['overleaf', 'oauth']))
  36. )
  37. },
  38. /**
  39. * Whether a feature is enabled in the appliation's configuration
  40. *
  41. * @param {string} feature
  42. * @returns {boolean}
  43. */
  44. hasFeature(feature) {
  45. switch (feature) {
  46. case 'saas':
  47. return Boolean(Settings.overleaf)
  48. case 'homepage':
  49. return Boolean(Settings.enableHomepage)
  50. case 'registration-page':
  51. return (
  52. !Features.externalAuthenticationSystemUsed() ||
  53. Boolean(Settings.overleaf)
  54. )
  55. case 'registration':
  56. return publicRegistrationModuleAvailable || Boolean(Settings.overleaf)
  57. case 'github-sync':
  58. return Boolean(Settings.enableGithubSync)
  59. case 'git-bridge':
  60. return Boolean(Settings.enableGitBridge)
  61. case 'custom-togglers':
  62. return Boolean(Settings.overleaf)
  63. case 'oauth':
  64. return Boolean(Settings.oauth)
  65. case 'templates-server-pro':
  66. return !Settings.overleaf
  67. case 'history-v1':
  68. return historyV1ModuleAvailable
  69. case 'affiliations':
  70. case 'analytics':
  71. return Boolean(_.get(Settings, ['apis', 'v1', 'url']))
  72. case 'overleaf-integration':
  73. return Boolean(Settings.overleaf)
  74. case 'references':
  75. return Boolean(_.get(Settings, ['apis', 'references', 'url']))
  76. case 'saml':
  77. return Boolean(Settings.enableSaml)
  78. case 'linked-project-file':
  79. return Boolean(Settings.enabledLinkedFileTypes.includes('project_file'))
  80. case 'linked-project-output-file':
  81. return Boolean(
  82. Settings.enabledLinkedFileTypes.includes('project_output_file')
  83. )
  84. case 'link-url':
  85. return Boolean(
  86. _.get(Settings, ['apis', 'linkedUrlProxy', 'url']) &&
  87. Settings.enabledLinkedFileTypes.includes('url')
  88. )
  89. case 'public-registration':
  90. return publicRegistrationModuleAvailable
  91. case 'support':
  92. return supportModuleAvailable
  93. case 'track-changes':
  94. return trackChangesModuleAvailable
  95. default:
  96. throw new Error(`unknown feature: ${feature}`)
  97. }
  98. },
  99. }
  100. module.exports = Features