config.js 18 KB

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