config.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 true if puclic wiki mode
  279. if (Config.isPublicWikiOnly(config)) {
  280. return true;
  281. }
  282. // return false if undefined
  283. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  284. return false;
  285. }
  286. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  287. };
  288. configSchema.statics.isEnabledPlugins = function(config) {
  289. const key = 'plugin:isEnabledPlugins';
  290. return getValueForCrowiNS(config, key);
  291. };
  292. configSchema.statics.isEnabledLinebreaks = function(config) {
  293. const key = 'markdown:isEnabledLinebreaks';
  294. return getValueForMarkdownNS(config, key);
  295. };
  296. configSchema.statics.isEnabledLinebreaksInComments = function(config) {
  297. const key = 'markdown:isEnabledLinebreaksInComments';
  298. return getValueForMarkdownNS(config, key);
  299. };
  300. configSchema.statics.isPublicWikiOnly = function(config) {
  301. const publicWikiOnly = process.env.PUBLIC_WIKI_ONLY;
  302. if ( publicWikiOnly === 'true' || publicWikiOnly == 1) {
  303. return true;
  304. }
  305. return false;
  306. };
  307. configSchema.statics.pageBreakSeparator = function(config) {
  308. const key = 'markdown:presentation:pageBreakSeparator';
  309. return getValueForMarkdownNS(config, key);
  310. };
  311. configSchema.statics.pageBreakCustomSeparator = function(config) {
  312. const key = 'markdown:presentation:pageBreakCustomSeparator';
  313. return getValueForMarkdownNS(config, key);
  314. };
  315. configSchema.statics.isEnabledXssPrevention = function(config) {
  316. const key = 'markdown:xss:isEnabledPrevention';
  317. return getValueForMarkdownNS(config, key);
  318. };
  319. configSchema.statics.xssOption = function(config) {
  320. const key = 'markdown:xss:option';
  321. return getValueForMarkdownNS(config, key);
  322. };
  323. configSchema.statics.tagWhiteList = function(config) {
  324. const key = 'markdown:xss:tagWhiteList';
  325. if (this.isEnabledXssPrevention(config)) {
  326. switch (this.xssOption(config)) {
  327. case 1: // ignore all: use default option
  328. return [];
  329. case 2: // recommended
  330. return recommendedXssWhiteList.tags;
  331. case 3: // custom white list
  332. return config.markdown[key];
  333. default:
  334. return [];
  335. }
  336. }
  337. else {
  338. return [];
  339. }
  340. };
  341. configSchema.statics.attrWhiteList = function(config) {
  342. const key = 'markdown:xss:attrWhiteList';
  343. if (this.isEnabledXssPrevention(config)) {
  344. switch (this.xssOption(config)) {
  345. case 1: // ignore all: use default option
  346. return [];
  347. case 2: // recommended
  348. return recommendedXssWhiteList.attrs;
  349. case 3: // custom white list
  350. return config.markdown[key];
  351. default:
  352. return [];
  353. }
  354. }
  355. else {
  356. return [];
  357. }
  358. };
  359. /**
  360. * initialize custom css strings
  361. */
  362. configSchema.statics.initCustomCss = function(config) {
  363. const key = 'customize:css';
  364. const rawCss = getValueForCrowiNS(config, key);
  365. // uglify and store
  366. this._customCss = uglifycss.processString(rawCss);
  367. };
  368. configSchema.statics.customCss = function(config) {
  369. return this._customCss;
  370. };
  371. configSchema.statics.initCustomScript = function(config) {
  372. const key = 'customize:script';
  373. const rawScript = getValueForCrowiNS(config, key);
  374. // store as is
  375. this._customScript = rawScript;
  376. };
  377. configSchema.statics.customScript = function(config) {
  378. return this._customScript;
  379. };
  380. configSchema.statics.customHeader = function(config) {
  381. const key = 'customize:header';
  382. return getValueForCrowiNS(config, key);
  383. };
  384. configSchema.statics.theme = function(config) {
  385. const key = 'customize:theme';
  386. return getValueForCrowiNS(config, key);
  387. };
  388. configSchema.statics.customTitle = function(config, page) {
  389. validateCrowi();
  390. const key = 'customize:title';
  391. let customTitle = getValueForCrowiNS(config, key);
  392. if (customTitle == null || customTitle.trim().length == 0) {
  393. customTitle = '{{page}} - {{sitename}}';
  394. }
  395. // replace
  396. customTitle = customTitle
  397. .replace('{{sitename}}', this.appTitle(config))
  398. .replace('{{page}}', page);
  399. return crowi.xss.process(customTitle);
  400. };
  401. configSchema.statics.behaviorType = function(config) {
  402. const key = 'customize:behavior';
  403. return getValueForCrowiNS(config, key);
  404. };
  405. configSchema.statics.layoutType = function(config) {
  406. const key = 'customize:layout';
  407. return getValueForCrowiNS(config, key);
  408. };
  409. configSchema.statics.highlightJsStyle = function(config) {
  410. const key = 'customize:highlightJsStyle';
  411. return getValueForCrowiNS(config, key);
  412. };
  413. configSchema.statics.highlightJsStyleBorder = function(config) {
  414. const key = 'customize:highlightJsStyleBorder';
  415. return getValueForCrowiNS(config, key);
  416. };
  417. configSchema.statics.isEnabledTimeline = function(config) {
  418. const key = 'customize:isEnabledTimeline';
  419. return getValueForCrowiNS(config, key);
  420. };
  421. configSchema.statics.isSavedStatesOfTabChanges = function(config) {
  422. const key = 'customize:isSavedStatesOfTabChanges';
  423. return getValueForCrowiNS(config, key);
  424. };
  425. configSchema.statics.isEnabledAttachTitleHeader = function(config) {
  426. const key = 'customize:isEnabledAttachTitleHeader';
  427. return getValueForCrowiNS(config, key);
  428. };
  429. configSchema.statics.showRecentCreatedNumber = function(config) {
  430. const key = 'customize:showRecentCreatedNumber';
  431. return getValueForCrowiNS(config, key);
  432. };
  433. configSchema.statics.fileUploadEnabled = function(config) {
  434. const Config = this;
  435. if (!Config.isUploadable(config)) {
  436. return false;
  437. }
  438. // convert to boolean
  439. return !!config.crowi['app:fileUpload'];
  440. };
  441. configSchema.statics.hasSlackConfig = function(config) {
  442. return Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config);
  443. };
  444. /**
  445. * for Slack Incoming Webhooks
  446. */
  447. configSchema.statics.hasSlackIwhUrl = function(config) {
  448. if (!config.notification) {
  449. return false;
  450. }
  451. return (config.notification['slack:incomingWebhookUrl'] ? true : false);
  452. };
  453. configSchema.statics.isIncomingWebhookPrioritized = function(config) {
  454. if (!config.notification) {
  455. return false;
  456. }
  457. return (config.notification['slack:isIncomingWebhookPrioritized'] ? true : false);
  458. };
  459. configSchema.statics.hasSlackToken = function(config) {
  460. if (!config.notification) {
  461. return false;
  462. }
  463. return (config.notification['slack:token'] ? true : false);
  464. };
  465. configSchema.statics.getLocalconfig = function(config) {
  466. const Config = this;
  467. const env = process.env;
  468. const local_config = {
  469. crowi: {
  470. title: Config.appTitle(crowi),
  471. url: config.crowi['app:siteUrl:fixed'] || '',
  472. },
  473. upload: {
  474. image: Config.isUploadable(config),
  475. file: Config.fileUploadEnabled(config),
  476. },
  477. behaviorType: Config.behaviorType(config),
  478. layoutType: Config.layoutType(config),
  479. isEnabledLinebreaks: Config.isEnabledLinebreaks(config),
  480. isEnabledLinebreaksInComments: Config.isEnabledLinebreaksInComments(config),
  481. isEnabledXssPrevention: Config.isEnabledXssPrevention(config),
  482. xssOption: Config.xssOption(config),
  483. tagWhiteList: Config.tagWhiteList(config),
  484. attrWhiteList: Config.attrWhiteList(config),
  485. highlightJsStyleBorder: Config.highlightJsStyleBorder(config),
  486. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  487. hasSlackConfig: Config.hasSlackConfig(config),
  488. env: {
  489. PLANTUML_URI: env.PLANTUML_URI || null,
  490. BLOCKDIAG_URI: env.BLOCKDIAG_URI || null,
  491. HACKMD_URI: env.HACKMD_URI || null,
  492. MATHJAX: env.MATHJAX || null,
  493. },
  494. recentCreatedLimit: Config.showRecentCreatedNumber(config),
  495. isAclEnabled: !Config.isPublicWikiOnly(config),
  496. };
  497. return local_config;
  498. };
  499. configSchema.statics.userUpperLimit = function(crowi) {
  500. const key = 'USER_UPPER_LIMIT';
  501. const env = crowi.env[key];
  502. if (undefined === crowi.env || undefined === crowi.env[key]) {
  503. return 0;
  504. }
  505. return Number(env);
  506. };
  507. /*
  508. configSchema.statics.isInstalled = function(config)
  509. {
  510. if (!config.crowi) {
  511. return false;
  512. }
  513. if (config.crowi['app:installed']
  514. && config.crowi['app:installed'] !== '0.0.0') {
  515. return true;
  516. }
  517. return false;
  518. }
  519. */
  520. Config = mongoose.model('Config', configSchema);
  521. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  522. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  523. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  524. return Config;
  525. };