config.js 18 KB

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