config.js 18 KB

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