config.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 getDefaultEnv() {
  112. return {
  113. 'USER_UPPER_LIMIT': 0,
  114. };
  115. }
  116. function getValueForCrowiNS(config, key) {
  117. // return the default value if undefined
  118. if (undefined === config.crowi || undefined === config.crowi[key]) {
  119. return getDefaultCrowiConfigs()[key];
  120. }
  121. return config.crowi[key];
  122. }
  123. function getValueForMarkdownNS(config, key) {
  124. // return the default value if undefined
  125. if (undefined === config.markdown || undefined === config.markdown[key]) {
  126. return getDefaultMarkdownConfigs()[key];
  127. }
  128. return config.markdown[key];
  129. }
  130. function getValueEnv(crowi, key) {
  131. if (null == crowi.env || null == crowi.env[key]) {
  132. return getDefaultEnv()[key];
  133. }
  134. return crowi.env[key];
  135. }
  136. configSchema.statics.getRestrictGuestModeLabels = function() {
  137. var labels = {};
  138. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'security_setting.guest_mode.deny';
  139. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = 'security_setting.guest_mode.readonly';
  140. return labels;
  141. };
  142. configSchema.statics.getRegistrationModeLabels = function() {
  143. var labels = {};
  144. labels[SECURITY_REGISTRATION_MODE_OPEN] = 'security_setting.registration_mode.open';
  145. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = 'security_setting.registration_mode.restricted';
  146. labels[SECURITY_REGISTRATION_MODE_CLOSED] = 'security_setting.registration_mode.closed';
  147. return labels;
  148. };
  149. configSchema.statics.updateConfigCache = function(ns, config) {
  150. validateCrowi();
  151. const originalConfig = crowi.getConfig();
  152. const newNSConfig = originalConfig[ns] || {};
  153. Object.keys(config).forEach(function(key) {
  154. if (config[key] || config[key] === '' || config[key] === false) {
  155. newNSConfig[key] = config[key];
  156. }
  157. });
  158. originalConfig[ns] = newNSConfig;
  159. crowi.setConfig(originalConfig);
  160. // initialize custom css/script
  161. Config.initCustomCss(originalConfig);
  162. Config.initCustomScript(originalConfig);
  163. };
  164. // Execute only once for installing application
  165. configSchema.statics.applicationInstall = function(callback) {
  166. var Config = this;
  167. Config.count({ ns: 'crowi' }, function(err, count) {
  168. if (count > 0) {
  169. return callback(new Error('Application already installed'), null);
  170. }
  171. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  172. Config.updateConfigCache('crowi', configs);
  173. return callback(err, configs);
  174. });
  175. });
  176. };
  177. configSchema.statics.setupCofigFormData = function(ns, config) {
  178. var defaultConfig = {};
  179. // set Default Settings
  180. if (ns === 'crowi') {
  181. defaultConfig = getDefaultCrowiConfigs();
  182. }
  183. else if (ns === 'markdown') {
  184. defaultConfig = getDefaultMarkdownConfigs();
  185. }
  186. if (!defaultConfig[ns]) {
  187. defaultConfig[ns] = {};
  188. }
  189. Object.keys(config[ns] || {}).forEach(function(key) {
  190. if (config[ns][key] !== undefined) {
  191. defaultConfig[key] = config[ns][key];
  192. }
  193. });
  194. return defaultConfig;
  195. };
  196. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback) {
  197. var Config = this;
  198. if (configs.length < 0) {
  199. return callback(new Error('Argument #1 is not array.'), null);
  200. }
  201. Object.keys(configs).forEach(function(key) {
  202. var value = configs[key];
  203. Config.findOneAndUpdate(
  204. { ns: ns, key: key },
  205. { ns: ns, key: key, value: JSON.stringify(value) },
  206. { upsert: true, },
  207. function(err, config) {
  208. debug('Config.findAndUpdate', err, config);
  209. });
  210. });
  211. return callback(null, configs);
  212. };
  213. configSchema.statics.findOneAndUpdateByNsAndKey = async function(ns, key, value) {
  214. return this.findOneAndUpdate(
  215. { ns: ns, key: key },
  216. { ns: ns, key: key, value: JSON.stringify(value) },
  217. { upsert: true, });
  218. };
  219. configSchema.statics.getConfig = function(callback) {
  220. };
  221. configSchema.statics.loadAllConfig = function(callback) {
  222. var Config = this
  223. , config = {};
  224. config.crowi = {}; // crowi namespace
  225. Config.find()
  226. .sort({ns: 1, key: 1})
  227. .exec(function(err, doc) {
  228. doc.forEach(function(el) {
  229. if (!config[el.ns]) {
  230. config[el.ns] = {};
  231. }
  232. config[el.ns][el.key] = JSON.parse(el.value);
  233. });
  234. debug('Config loaded', config);
  235. // initialize custom css/script
  236. Config.initCustomCss(config);
  237. Config.initCustomScript(config);
  238. return callback(null, config);
  239. });
  240. };
  241. configSchema.statics.appTitle = function(config) {
  242. const key = 'app:title';
  243. return getValueForCrowiNS(config, key) || 'GROWI';
  244. };
  245. configSchema.statics.isEnabledPassport = function(config) {
  246. // always true if growi installed cleanly
  247. if (Object.keys(config.crowi).length == 0) {
  248. return true;
  249. }
  250. const key = 'security:isEnabledPassport';
  251. return getValueForCrowiNS(config, key);
  252. };
  253. configSchema.statics.isEnabledPassportLdap = function(config) {
  254. const key = 'security:passport-ldap:isEnabled';
  255. return getValueForCrowiNS(config, key);
  256. };
  257. configSchema.statics.isEnabledPassportSaml = function(config) {
  258. const key = 'security:passport-saml:isEnabled';
  259. return getValueForCrowiNS(config, key);
  260. };
  261. configSchema.statics.isEnabledPassportGoogle = function(config) {
  262. const key = 'security:passport-google:isEnabled';
  263. return getValueForCrowiNS(config, key);
  264. };
  265. configSchema.statics.isEnabledPassportGitHub = function(config) {
  266. const key = 'security:passport-github:isEnabled';
  267. return getValueForCrowiNS(config, key);
  268. };
  269. configSchema.statics.isEnabledPassportTwitter = function(config) {
  270. const key = 'security:passport-twitter:isEnabled';
  271. return getValueForCrowiNS(config, key);
  272. };
  273. configSchema.statics.isSameUsernameTreatedAsIdenticalUser = function(config, providerType) {
  274. const key = `security:passport-${providerType}:isSameUsernameTreatedAsIdenticalUser`;
  275. return getValueForCrowiNS(config, key);
  276. };
  277. configSchema.statics.isUploadable = function(config) {
  278. const method = process.env.FILE_UPLOAD || 'aws';
  279. if (method == 'aws' && (
  280. !config.crowi['aws:accessKeyId'] ||
  281. !config.crowi['aws:secretAccessKey'] ||
  282. !config.crowi['aws:region'] ||
  283. !config.crowi['aws:bucket'])) {
  284. return false;
  285. }
  286. return method != 'none';
  287. };
  288. configSchema.statics.isGuesstAllowedToRead = function(config) {
  289. // return true if puclic wiki mode
  290. if (Config.isPublicWikiOnly(config)) {
  291. return true;
  292. }
  293. // return false if undefined
  294. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  295. return false;
  296. }
  297. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  298. };
  299. configSchema.statics.isEnabledPlugins = function(config) {
  300. const key = 'plugin:isEnabledPlugins';
  301. return getValueForCrowiNS(config, key);
  302. };
  303. configSchema.statics.isEnabledLinebreaks = function(config) {
  304. const key = 'markdown:isEnabledLinebreaks';
  305. return getValueForMarkdownNS(config, key);
  306. };
  307. configSchema.statics.isEnabledLinebreaksInComments = function(config) {
  308. const key = 'markdown:isEnabledLinebreaksInComments';
  309. return getValueForMarkdownNS(config, key);
  310. };
  311. configSchema.statics.isPublicWikiOnly = function(config) {
  312. const publicWikiOnly = process.env.PUBLIC_WIKI_ONLY;
  313. if ( publicWikiOnly === 'true' || publicWikiOnly == 1) {
  314. return true;
  315. }
  316. return false;
  317. };
  318. configSchema.statics.pageBreakSeparator = function(config) {
  319. const key = 'markdown:presentation:pageBreakSeparator';
  320. return getValueForMarkdownNS(config, key);
  321. };
  322. configSchema.statics.pageBreakCustomSeparator = function(config) {
  323. const key = 'markdown:presentation:pageBreakCustomSeparator';
  324. return getValueForMarkdownNS(config, key);
  325. };
  326. configSchema.statics.isEnabledXssPrevention = function(config) {
  327. const key = 'markdown:xss:isEnabledPrevention';
  328. return getValueForMarkdownNS(config, key);
  329. };
  330. configSchema.statics.xssOption = function(config) {
  331. const key = 'markdown:xss:option';
  332. return getValueForMarkdownNS(config, key);
  333. };
  334. configSchema.statics.tagWhiteList = function(config) {
  335. const key = 'markdown:xss:tagWhiteList';
  336. if (this.isEnabledXssPrevention(config)) {
  337. switch (this.xssOption(config)) {
  338. case 1: // ignore all: use default option
  339. return [];
  340. case 2: // recommended
  341. return recommendedXssWhiteList.tags;
  342. case 3: // custom white list
  343. return config.markdown[key];
  344. default:
  345. return [];
  346. }
  347. }
  348. else {
  349. return [];
  350. }
  351. };
  352. configSchema.statics.attrWhiteList = function(config) {
  353. const key = 'markdown:xss:attrWhiteList';
  354. if (this.isEnabledXssPrevention(config)) {
  355. switch (this.xssOption(config)) {
  356. case 1: // ignore all: use default option
  357. return [];
  358. case 2: // recommended
  359. return recommendedXssWhiteList.attrs;
  360. case 3: // custom white list
  361. return config.markdown[key];
  362. default:
  363. return [];
  364. }
  365. }
  366. else {
  367. return [];
  368. }
  369. };
  370. /**
  371. * initialize custom css strings
  372. */
  373. configSchema.statics.initCustomCss = function(config) {
  374. const key = 'customize:css';
  375. const rawCss = getValueForCrowiNS(config, key);
  376. // uglify and store
  377. this._customCss = uglifycss.processString(rawCss);
  378. };
  379. configSchema.statics.customCss = function(config) {
  380. return this._customCss;
  381. };
  382. configSchema.statics.initCustomScript = function(config) {
  383. const key = 'customize:script';
  384. const rawScript = getValueForCrowiNS(config, key);
  385. // store as is
  386. this._customScript = rawScript;
  387. };
  388. configSchema.statics.customScript = function(config) {
  389. return this._customScript;
  390. };
  391. configSchema.statics.customHeader = function(config) {
  392. const key = 'customize:header';
  393. return getValueForCrowiNS(config, key);
  394. };
  395. configSchema.statics.theme = function(config) {
  396. const key = 'customize:theme';
  397. return getValueForCrowiNS(config, key);
  398. };
  399. configSchema.statics.customTitle = function(config, page) {
  400. validateCrowi();
  401. const key = 'customize:title';
  402. let customTitle = getValueForCrowiNS(config, key);
  403. if (customTitle == null || customTitle.trim().length == 0) {
  404. customTitle = '{{page}} - {{sitename}}';
  405. }
  406. // replace
  407. customTitle = customTitle
  408. .replace('{{sitename}}', this.appTitle(config))
  409. .replace('{{page}}', page);
  410. return crowi.xss.process(customTitle);
  411. };
  412. configSchema.statics.behaviorType = function(config) {
  413. const key = 'customize:behavior';
  414. return getValueForCrowiNS(config, key);
  415. };
  416. configSchema.statics.layoutType = function(config) {
  417. const key = 'customize:layout';
  418. return getValueForCrowiNS(config, key);
  419. };
  420. configSchema.statics.highlightJsStyle = function(config) {
  421. const key = 'customize:highlightJsStyle';
  422. return getValueForCrowiNS(config, key);
  423. };
  424. configSchema.statics.highlightJsStyleBorder = function(config) {
  425. const key = 'customize:highlightJsStyleBorder';
  426. return getValueForCrowiNS(config, key);
  427. };
  428. configSchema.statics.isEnabledTimeline = function(config) {
  429. const key = 'customize:isEnabledTimeline';
  430. return getValueForCrowiNS(config, key);
  431. };
  432. configSchema.statics.isSavedStatesOfTabChanges = function(config) {
  433. const key = 'customize:isSavedStatesOfTabChanges';
  434. return getValueForCrowiNS(config, key);
  435. };
  436. configSchema.statics.isEnabledAttachTitleHeader = function(config) {
  437. const key = 'customize:isEnabledAttachTitleHeader';
  438. return getValueForCrowiNS(config, key);
  439. };
  440. configSchema.statics.showRecentCreatedNumber = function(config) {
  441. const key = 'customize:showRecentCreatedNumber';
  442. return getValueForCrowiNS(config, key);
  443. };
  444. configSchema.statics.fileUploadEnabled = function(config) {
  445. const Config = this;
  446. if (!Config.isUploadable(config)) {
  447. return false;
  448. }
  449. // convert to boolean
  450. return !!config.crowi['app:fileUpload'];
  451. };
  452. configSchema.statics.hasSlackConfig = function(config) {
  453. return Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config);
  454. };
  455. /**
  456. * for Slack Incoming Webhooks
  457. */
  458. configSchema.statics.hasSlackIwhUrl = function(config) {
  459. if (!config.notification) {
  460. return false;
  461. }
  462. return (config.notification['slack:incomingWebhookUrl'] ? true : false);
  463. };
  464. configSchema.statics.isIncomingWebhookPrioritized = function(config) {
  465. if (!config.notification) {
  466. return false;
  467. }
  468. return (config.notification['slack:isIncomingWebhookPrioritized'] ? true : false);
  469. };
  470. configSchema.statics.hasSlackToken = function(config) {
  471. if (!config.notification) {
  472. return false;
  473. }
  474. return (config.notification['slack:token'] ? true : false);
  475. };
  476. configSchema.statics.getLocalconfig = function(config) {
  477. const Config = this;
  478. const env = process.env;
  479. const local_config = {
  480. crowi: {
  481. title: Config.appTitle(crowi),
  482. url: config.crowi['app:siteUrl:fixed'] || '',
  483. },
  484. upload: {
  485. image: Config.isUploadable(config),
  486. file: Config.fileUploadEnabled(config),
  487. },
  488. behaviorType: Config.behaviorType(config),
  489. layoutType: Config.layoutType(config),
  490. isEnabledLinebreaks: Config.isEnabledLinebreaks(config),
  491. isEnabledLinebreaksInComments: Config.isEnabledLinebreaksInComments(config),
  492. isEnabledXssPrevention: Config.isEnabledXssPrevention(config),
  493. xssOption: Config.xssOption(config),
  494. tagWhiteList: Config.tagWhiteList(config),
  495. attrWhiteList: Config.attrWhiteList(config),
  496. highlightJsStyleBorder: Config.highlightJsStyleBorder(config),
  497. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  498. hasSlackConfig: Config.hasSlackConfig(config),
  499. env: {
  500. PLANTUML_URI: env.PLANTUML_URI || null,
  501. BLOCKDIAG_URI: env.BLOCKDIAG_URI || null,
  502. HACKMD_URI: env.HACKMD_URI || null,
  503. MATHJAX: env.MATHJAX || null,
  504. },
  505. recentCreatedLimit: Config.showRecentCreatedNumber(config),
  506. isAclEnabled: !Config.isPublicWikiOnly(config),
  507. };
  508. return local_config;
  509. };
  510. configSchema.statics.userUpperLimit = function(crowi) {
  511. const key = 'USER_UPPER_LIMIT';
  512. return Number(getValueEnv(crowi, key));
  513. };
  514. /*
  515. configSchema.statics.isInstalled = function(config)
  516. {
  517. if (!config.crowi) {
  518. return false;
  519. }
  520. if (config.crowi['app:installed']
  521. && config.crowi['app:installed'] !== '0.0.0') {
  522. return true;
  523. }
  524. return false;
  525. }
  526. */
  527. Config = mongoose.model('Config', configSchema);
  528. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  529. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  530. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  531. return Config;
  532. };