config.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // disable no-return-await for model functions
  2. /* eslint-disable no-return-await */
  3. /* eslint-disable no-use-before-define */
  4. module.exports = function(crowi) {
  5. const mongoose = require('mongoose');
  6. const debug = require('debug')('growi:models:config');
  7. const uglifycss = require('uglifycss');
  8. const recommendedWhitelist = require('@commons/service/xss/recommended-whitelist');
  9. const SECURITY_RESTRICT_GUEST_MODE_DENY = 'Deny';
  10. const SECURITY_RESTRICT_GUEST_MODE_READONLY = 'Readonly';
  11. const SECURITY_REGISTRATION_MODE_OPEN = 'Open';
  12. const SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted';
  13. const SECURITY_REGISTRATION_MODE_CLOSED = 'Closed';
  14. let Config;
  15. const configSchema = new mongoose.Schema({
  16. ns: { type: String, required: true, index: true },
  17. key: { type: String, required: true, index: true },
  18. value: { type: String, required: true },
  19. });
  20. function validateCrowi() {
  21. if (crowi == null) {
  22. throw new Error('"crowi" is null. Init Config model with "crowi" argument first.');
  23. }
  24. }
  25. /**
  26. * default values when GROWI is cleanly installed
  27. */
  28. function getConfigsForInstalling() {
  29. const config = getDefaultCrowiConfigs();
  30. // overwrite
  31. config['app:installed'] = true;
  32. config['app:fileUpload'] = true;
  33. config['customize:behavior'] = 'growi';
  34. config['customize:layout'] = 'growi';
  35. config['customize:isSavedStatesOfTabChanges'] = false;
  36. return config;
  37. }
  38. /**
  39. * default values when migrated from Official Crowi
  40. */
  41. function getDefaultCrowiConfigs() {
  42. /* eslint-disable key-spacing */
  43. return {
  44. 'app:installed' : false,
  45. 'app:confidential' : '',
  46. 'app:fileUpload' : false,
  47. 'app:globalLang' : 'en-US',
  48. 'security:restrictGuestMode' : 'Deny',
  49. 'security:registrationMode' : 'Open',
  50. 'security:registrationWhiteList' : [],
  51. 'security:list-policy:hideRestrictedByOwner' : false,
  52. 'security:list-policy:hideRestrictedByGroup' : false,
  53. 'security:isEnabledPassport' : true,
  54. 'security:passport-ldap:isEnabled' : false,
  55. 'security:passport-ldap:serverUrl' : undefined,
  56. 'security:passport-ldap:isUserBind' : undefined,
  57. 'security:passport-ldap:bindDN' : undefined,
  58. 'security:passport-ldap:bindDNPassword' : undefined,
  59. 'security:passport-ldap:searchFilter' : undefined,
  60. 'security:passport-ldap:attrMapUsername' : undefined,
  61. 'security:passport-ldap:attrMapName' : undefined,
  62. 'security:passport-ldap:attrMapMail' : undefined,
  63. 'security:passport-ldap:groupSearchBase' : undefined,
  64. 'security:passport-ldap:groupSearchFilter' : undefined,
  65. 'security:passport-ldap:groupDnProperty' : undefined,
  66. 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': false,
  67. 'security:passport-saml:isEnabled' : false,
  68. 'security:passport-saml:isSameEmailTreatedAsIdenticalUser': false,
  69. 'security:passport-google:isEnabled' : false,
  70. 'security:passport-github:isEnabled' : false,
  71. 'security:passport-twitter:isEnabled' : false,
  72. 'security:passport-oidc:isEnabled' : false,
  73. 'aws:bucket' : 'growi',
  74. 'aws:region' : 'ap-northeast-1',
  75. 'aws:accessKeyId' : '',
  76. 'aws:secretAccessKey' : '',
  77. 'mail:from' : '',
  78. 'mail:smtpHost' : '',
  79. 'mail:smtpPort' : '',
  80. 'mail:smtpUser' : '',
  81. 'mail:smtpPassword' : '',
  82. 'google:clientId' : '',
  83. 'google:clientSecret' : '',
  84. 'plugin:isEnabledPlugins' : true,
  85. 'customize:css' : '',
  86. 'customize:script' : '',
  87. 'customize:header' : '',
  88. 'customize:title' : '',
  89. 'customize:highlightJsStyle' : 'github',
  90. 'customize:highlightJsStyleBorder' : false,
  91. 'customize:theme' : 'default',
  92. 'customize:behavior' : 'crowi',
  93. 'customize:layout' : 'crowi',
  94. 'customize:isEnabledTimeline' : true,
  95. 'customize:isSavedStatesOfTabChanges' : true,
  96. 'customize:isEnabledAttachTitleHeader' : false,
  97. 'customize:showRecentCreatedNumber' : 10,
  98. 'importer:esa:team_name': '',
  99. 'importer:esa:access_token': '',
  100. 'importer:qiita:team_name': '',
  101. 'importer:qiita:access_token': '',
  102. };
  103. /* eslint-enable key-spacing */
  104. }
  105. function getDefaultMarkdownConfigs() {
  106. return {
  107. 'markdown:xss:isEnabledPrevention': true,
  108. 'markdown:xss:option': 2,
  109. 'markdown:xss:tagWhiteList': [],
  110. 'markdown:xss:attrWhiteList': [],
  111. 'markdown:isEnabledLinebreaks': false,
  112. 'markdown:isEnabledLinebreaksInComments': true,
  113. 'markdown:presentation:pageBreakSeparator': 1,
  114. 'markdown:presentation:pageBreakCustomSeparator': '',
  115. };
  116. }
  117. function getDefaultNotificationConfigs() {
  118. return {
  119. 'slack:isIncomingWebhookPrioritized': false,
  120. 'slack:incomingWebhookUrl': '',
  121. 'slack:token': '',
  122. };
  123. }
  124. function getValueForCrowiNS(config, key) {
  125. crowi.configManager.getConfig('crowi', key);
  126. // // return the default value if undefined
  127. // if (undefined === config.crowi || undefined === config.crowi[key]) {
  128. // return getDefaultCrowiConfigs()[key];
  129. // }
  130. // return config.crowi[key];
  131. }
  132. function getValueForMarkdownNS(config, key) {
  133. crowi.configManager.getConfig('markdown', key);
  134. // // return the default value if undefined
  135. // if (undefined === config.markdown || undefined === config.markdown[key]) {
  136. // return getDefaultMarkdownConfigs()[key];
  137. // }
  138. // return config.markdown[key];
  139. }
  140. /**
  141. * It is deprecated to use this for anything other than ConfigManager#isDBInitialized.
  142. */
  143. configSchema.statics.getConfigsObjectForInstalling = function() {
  144. return getConfigsForInstalling();
  145. };
  146. /**
  147. * It is deprecated to use this for anything other than ConfigLoader#load.
  148. */
  149. configSchema.statics.getDefaultCrowiConfigsObject = function() {
  150. return getDefaultCrowiConfigs();
  151. };
  152. /**
  153. * It is deprecated to use this for anything other than ConfigLoader#load.
  154. */
  155. configSchema.statics.getDefaultMarkdownConfigsObject = function() {
  156. return getDefaultMarkdownConfigs();
  157. };
  158. /**
  159. * It is deprecated to use this for anything other than ConfigLoader#load.
  160. */
  161. configSchema.statics.getDefaultNotificationConfigsObject = function() {
  162. return getDefaultNotificationConfigs();
  163. };
  164. configSchema.statics.getRestrictGuestModeLabels = function() {
  165. const labels = {};
  166. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'security_setting.guest_mode.deny';
  167. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = 'security_setting.guest_mode.readonly';
  168. return labels;
  169. };
  170. configSchema.statics.getRegistrationModeLabels = function() {
  171. const labels = {};
  172. labels[SECURITY_REGISTRATION_MODE_OPEN] = 'security_setting.registration_mode.open';
  173. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = 'security_setting.registration_mode.restricted';
  174. labels[SECURITY_REGISTRATION_MODE_CLOSED] = 'security_setting.registration_mode.closed';
  175. return labels;
  176. };
  177. configSchema.statics.updateConfigCache = function(ns, config) {
  178. validateCrowi();
  179. // const originalConfig = crowi.getConfig();
  180. // const newNSConfig = originalConfig[ns] || {};
  181. // Object.keys(config).forEach((key) => {
  182. // if (config[key] || config[key] === '' || config[key] === false) {
  183. // newNSConfig[key] = config[key];
  184. // }
  185. // });
  186. // originalConfig[ns] = newNSConfig;
  187. // crowi.setConfig(originalConfig);
  188. // // initialize custom css/script
  189. // Config.initCustomCss(originalConfig);
  190. // Config.initCustomScript(originalConfig);
  191. };
  192. // Execute only once for installing application
  193. // configSchema.statics.applicationInstall = function(callback) {
  194. // const Config = this;
  195. // Config.count({ ns: 'crowi' }, (err, count) => {
  196. // if (count > 0) {
  197. // return callback(new Error('Application already installed'), null);
  198. // }
  199. // Config.updateNamespaceByArray('crowi', getArrayForInstalling(), (err, configs) => {
  200. // Config.updateConfigCache('crowi', configs);
  201. // return callback(err, configs);
  202. // });
  203. // });
  204. // };
  205. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback) {
  206. const Config = this;
  207. if (configs.length < 0) {
  208. return callback(new Error('Argument #1 is not array.'), null);
  209. }
  210. Object.keys(configs).forEach((key) => {
  211. const value = configs[key];
  212. Config.findOneAndUpdate(
  213. { ns, key },
  214. { ns, key, value: JSON.stringify(value) },
  215. { upsert: true },
  216. (err, config) => {
  217. debug('Config.findAndUpdate', err, config);
  218. },
  219. );
  220. });
  221. return callback(null, configs);
  222. };
  223. configSchema.statics.findOneAndUpdateByNsAndKey = async function(ns, key, value) {
  224. return this.findOneAndUpdate(
  225. { ns, key },
  226. { ns, key, value: JSON.stringify(value) },
  227. { upsert: true },
  228. );
  229. };
  230. configSchema.statics.getConfig = function(callback) {
  231. };
  232. // configSchema.statics.loadAllConfig = function(callback) {
  233. // const Config = this;
  234. // const config = {};
  235. // config.crowi = {}; // crowi namespace
  236. // Config.find()
  237. // .sort({ ns: 1, key: 1 })
  238. // .exec((err, doc) => {
  239. // doc.forEach((el) => {
  240. // if (!config[el.ns]) {
  241. // config[el.ns] = {};
  242. // }
  243. // config[el.ns][el.key] = JSON.parse(el.value);
  244. // });
  245. // debug('Config loaded', config);
  246. // // initialize custom css/script
  247. // Config.initCustomCss(config);
  248. // Config.initCustomScript(config);
  249. // return callback(null, config);
  250. // });
  251. // };
  252. configSchema.statics.appTitle = function() {
  253. const key = 'app:title';
  254. return getValueForCrowiNS(null, key) || 'GROWI';
  255. };
  256. configSchema.statics.globalLang = function(config) {
  257. const key = 'app:globalLang';
  258. return getValueForCrowiNS(config, key);
  259. };
  260. configSchema.statics.isUploadable = function(config) {
  261. const method = process.env.FILE_UPLOAD || 'aws';
  262. if (method === 'aws' && (
  263. !config.crowi['aws:accessKeyId']
  264. || !config.crowi['aws:secretAccessKey']
  265. || !config.crowi['aws:region']
  266. || !config.crowi['aws:bucket'])) {
  267. return false;
  268. }
  269. return method !== 'none';
  270. };
  271. configSchema.statics.isGuestAllowedToRead = function(config) {
  272. // return true if puclic wiki mode
  273. if (crowi.aclService.getIsPublicWikiOnly()) {
  274. return true;
  275. }
  276. const restrictGuestMode = crowi.configManager.getConfig('crowi', 'security:restrictGuestMode');
  277. // return false if undefined
  278. if (undefined === config.crowi || undefined === restrictGuestMode) {
  279. return false;
  280. }
  281. return SECURITY_RESTRICT_GUEST_MODE_READONLY === restrictGuestMode;
  282. };
  283. configSchema.statics.isEnabledPlugins = function(config) {
  284. const key = 'plugin:isEnabledPlugins';
  285. return getValueForCrowiNS(config, key);
  286. };
  287. configSchema.statics.isEnabledLinebreaks = function(config) {
  288. const key = 'markdown:isEnabledLinebreaks';
  289. return getValueForMarkdownNS(config, key);
  290. };
  291. configSchema.statics.isEnabledLinebreaksInComments = function(config) {
  292. const key = 'markdown:isEnabledLinebreaksInComments';
  293. return getValueForMarkdownNS(config, key);
  294. };
  295. configSchema.statics.isPublicWikiOnly = function(config) {
  296. const publicWikiOnly = process.env.PUBLIC_WIKI_ONLY;
  297. if (publicWikiOnly === 'true' || publicWikiOnly === 1) {
  298. return true;
  299. }
  300. return false;
  301. };
  302. configSchema.statics.pageBreakSeparator = function(config) {
  303. const key = 'markdown:presentation:pageBreakSeparator';
  304. return getValueForMarkdownNS(config, key);
  305. };
  306. configSchema.statics.pageBreakCustomSeparator = function(config) {
  307. const key = 'markdown:presentation:pageBreakCustomSeparator';
  308. return getValueForMarkdownNS(config, key);
  309. };
  310. configSchema.statics.isEnabledXssPrevention = function(config) {
  311. const key = 'markdown:xss:isEnabledPrevention';
  312. return getValueForMarkdownNS(config, key);
  313. };
  314. configSchema.statics.xssOption = function(config) {
  315. const key = 'markdown:xss:option';
  316. return getValueForMarkdownNS(config, key);
  317. };
  318. configSchema.statics.tagWhiteList = function(config) {
  319. const key = 'markdown:xss:tagWhiteList';
  320. if (this.isEnabledXssPrevention(config)) {
  321. switch (this.xssOption(config)) {
  322. case 1: // ignore all: use default option
  323. return [];
  324. case 2: // recommended
  325. return recommendedWhitelist.tags;
  326. case 3: // custom white list
  327. return config.markdown[key];
  328. default:
  329. return [];
  330. }
  331. }
  332. else {
  333. return [];
  334. }
  335. };
  336. configSchema.statics.attrWhiteList = function(config) {
  337. const key = 'markdown:xss:attrWhiteList';
  338. if (this.isEnabledXssPrevention(config)) {
  339. switch (this.xssOption(config)) {
  340. case 1: // ignore all: use default option
  341. return [];
  342. case 2: // recommended
  343. return recommendedWhitelist.attrs;
  344. case 3: // custom white list
  345. return config.markdown[key];
  346. default:
  347. return [];
  348. }
  349. }
  350. else {
  351. return [];
  352. }
  353. };
  354. /**
  355. * initialize custom css strings
  356. */
  357. configSchema.statics.initCustomCss = function(config) {
  358. const key = 'customize:css';
  359. const rawCss = getValueForCrowiNS(config, key);
  360. // uglify and store
  361. this._customCss = uglifycss.processString(rawCss);
  362. };
  363. configSchema.statics.customCss = function(config) {
  364. return this._customCss;
  365. };
  366. configSchema.statics.initCustomScript = function(config) {
  367. const key = 'customize:script';
  368. const rawScript = getValueForCrowiNS(config, key);
  369. // store as is
  370. this._customScript = rawScript;
  371. };
  372. configSchema.statics.customScript = function(config) {
  373. return this._customScript;
  374. };
  375. configSchema.statics.customHeader = function(config) {
  376. const key = 'customize:header';
  377. return getValueForCrowiNS(config, key);
  378. };
  379. configSchema.statics.customTitle = function(config, page) {
  380. validateCrowi();
  381. const key = 'customize:title';
  382. let customTitle = getValueForCrowiNS(config, key);
  383. if (customTitle == null || customTitle.trim().length === 0) {
  384. customTitle = '{{page}} - {{sitename}}';
  385. }
  386. // replace
  387. customTitle = customTitle
  388. .replace('{{sitename}}', this.appTitle(config))
  389. .replace('{{page}}', page);
  390. return crowi.xss.process(customTitle);
  391. };
  392. configSchema.statics.behaviorType = function(config) {
  393. const key = 'customize:behavior';
  394. return getValueForCrowiNS(config, key);
  395. };
  396. configSchema.statics.layoutType = function(config) {
  397. const key = 'customize:layout';
  398. return getValueForCrowiNS(config, key);
  399. };
  400. configSchema.statics.highlightJsStyle = function(config) {
  401. const key = 'customize:highlightJsStyle';
  402. return getValueForCrowiNS(config, key);
  403. };
  404. configSchema.statics.highlightJsStyleBorder = function(config) {
  405. const key = 'customize:highlightJsStyleBorder';
  406. return getValueForCrowiNS(config, key);
  407. };
  408. configSchema.statics.isEnabledTimeline = function(config) {
  409. const key = 'customize:isEnabledTimeline';
  410. return getValueForCrowiNS(config, key);
  411. };
  412. configSchema.statics.isSavedStatesOfTabChanges = function(config) {
  413. const key = 'customize:isSavedStatesOfTabChanges';
  414. return getValueForCrowiNS(config, key);
  415. };
  416. configSchema.statics.isEnabledAttachTitleHeader = function(config) {
  417. const key = 'customize:isEnabledAttachTitleHeader';
  418. return getValueForCrowiNS(config, key);
  419. };
  420. configSchema.statics.showRecentCreatedNumber = function(config) {
  421. const key = 'customize:showRecentCreatedNumber';
  422. return getValueForCrowiNS(config, key);
  423. };
  424. configSchema.statics.fileUploadEnabled = function(config) {
  425. const Config = this;
  426. if (!Config.isUploadable(config)) {
  427. return false;
  428. }
  429. // convert to boolean
  430. return !!config.crowi['app:fileUpload'];
  431. };
  432. configSchema.statics.hasSlackConfig = function(config) {
  433. return Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config);
  434. };
  435. /**
  436. * for Slack Incoming Webhooks
  437. */
  438. configSchema.statics.hasSlackIwhUrl = function(config) {
  439. if (!config.notification) {
  440. return false;
  441. }
  442. return (!!config.notification['slack:incomingWebhookUrl']);
  443. };
  444. configSchema.statics.isIncomingWebhookPrioritized = function(config) {
  445. if (!config.notification) {
  446. return false;
  447. }
  448. return (!!config.notification['slack:isIncomingWebhookPrioritized']);
  449. };
  450. configSchema.statics.hasSlackToken = function(config) {
  451. if (!config.notification) {
  452. return false;
  453. }
  454. return (!!config.notification['slack:token']);
  455. };
  456. configSchema.statics.getLocalconfig = function() { // CONF.RF: これも別のメソッドにする
  457. const Config = this;
  458. const env = process.env;
  459. const localConfig = {
  460. crowi: {
  461. title: Config.appTitle(crowi),
  462. url: crowi.appService.getSiteUrl(),
  463. },
  464. upload: {
  465. image: crowi.configManager.getIsUploadable(),
  466. file: crowi.configManager.getConfig('crowi', 'app:fileUpload'),
  467. },
  468. behaviorType: crowi.configManager.getConfig('crowi', 'customize:behavior'),
  469. layoutType: crowi.configManager.getConfig('crowi', 'customize:layout'),
  470. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  471. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  472. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  473. xssOption: crowi.configManager.getConfig('markdown', 'markdown:xss:option'),
  474. tagWhiteList: crowi.xssService.getTagWhiteList(),
  475. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  476. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  477. isSavedStatesOfTabChanges: crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  478. hasSlackConfig: crowi.configManager.getConfig('crowi', 'customize:behavior'),
  479. env: {
  480. PLANTUML_URI: env.PLANTUML_URI || null,
  481. BLOCKDIAG_URI: env.BLOCKDIAG_URI || null,
  482. HACKMD_URI: env.HACKMD_URI || null,
  483. MATHJAX: env.MATHJAX || null,
  484. NO_CDN: env.NO_CDN || null,
  485. },
  486. recentCreatedLimit: crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  487. isAclEnabled: !crowi.aclService.getIsPublicWikiOnly(),
  488. globalLang: crowi.configManager.getConfig('crowi', 'app:globalLang'),
  489. };
  490. return localConfig;
  491. };
  492. configSchema.statics.userUpperLimit = function(crowi) {
  493. const key = 'USER_UPPER_LIMIT';
  494. const env = crowi.env[key];
  495. if (undefined === crowi.env || undefined === crowi.env[key]) {
  496. return 0;
  497. }
  498. return Number(env);
  499. };
  500. /*
  501. configSchema.statics.isInstalled = function(config)
  502. {
  503. if (!config.crowi) {
  504. return false;
  505. }
  506. if (config.crowi['app:installed']
  507. && config.crowi['app:installed'] !== '0.0.0') {
  508. return true;
  509. }
  510. return false;
  511. }
  512. */
  513. Config = mongoose.model('Config', configSchema);
  514. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  515. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  516. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  517. return Config;
  518. };