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