config.js 18 KB

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