config.js 18 KB

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