config.js 18 KB

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