config.js 19 KB

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