config.js 19 KB

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