config.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. module.exports = function(crowi) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('growi:models:config')
  4. , uglifycss = require('uglifycss')
  5. , configSchema
  6. , Config
  7. , SECURITY_RESTRICT_GUEST_MODE_DENY = 'Deny'
  8. , SECURITY_RESTRICT_GUEST_MODE_READONLY = 'Readonly'
  9. , SECURITY_REGISTRATION_MODE_OPEN = 'Open'
  10. , SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted'
  11. , SECURITY_REGISTRATION_MODE_CLOSED = 'Closed'
  12. ;
  13. configSchema = new mongoose.Schema({
  14. ns: { type: String, required: true, index: true },
  15. key: { type: String, required: true, index: true },
  16. value: { type: String, required: true }
  17. });
  18. /**
  19. * default values when GROWI is cleanly installed
  20. */
  21. function getArrayForInstalling() {
  22. let config = getDefaultCrowiConfigs();
  23. // overwrite
  24. config['app:fileUpload'] = true;
  25. config['security:isEnabledPassport'] = true;
  26. config['customize:behavior'] = 'growi';
  27. config['customize:layout'] = 'growi';
  28. config['customize:isSavedStatesOfTabChanges'] = false;
  29. return config;
  30. }
  31. /**
  32. * default values when migrated from Official Crowi
  33. */
  34. function getDefaultCrowiConfigs() {
  35. /* eslint-disable key-spacing */
  36. return {
  37. //'app:installed' : "0.0.0",
  38. 'app:confidential' : '',
  39. 'app:fileUpload' : false,
  40. 'security:restrictGuestMode' : 'Deny',
  41. 'security:registrationMode' : 'Open',
  42. 'security:registrationWhiteList' : [],
  43. 'security:isEnabledPassport' : false,
  44. 'security:passport-ldap:isEnabled' : false,
  45. 'security:passport-ldap:serverUrl' : undefined,
  46. 'security:passport-ldap:isUserBind' : undefined,
  47. 'security:passport-ldap:bindDN' : undefined,
  48. 'security:passport-ldap:bindDNPassword' : undefined,
  49. 'security:passport-ldap:searchFilter' : undefined,
  50. 'security:passport-ldap:attrMapUsername' : undefined,
  51. 'security:passport-ldap:attrMapName' : undefined,
  52. 'security:passport-ldap:groupSearchBase' : undefined,
  53. 'security:passport-ldap:groupSearchFilter' : undefined,
  54. 'security:passport-ldap:groupDnProperty' : undefined,
  55. 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': false,
  56. 'security:passport-google:isEnabled' : false,
  57. 'security:passport-github:isEnabled' : false,
  58. 'aws:bucket' : 'growi',
  59. 'aws:region' : 'ap-northeast-1',
  60. 'aws:accessKeyId' : '',
  61. 'aws:secretAccessKey' : '',
  62. 'mail:from' : '',
  63. 'mail:smtpHost' : '',
  64. 'mail:smtpPort' : '',
  65. 'mail:smtpUser' : '',
  66. 'mail:smtpPassword' : '',
  67. 'google:clientId' : '',
  68. 'google:clientSecret' : '',
  69. 'plugin:isEnabledPlugins' : true,
  70. 'customize:css' : '',
  71. 'customize:script' : '',
  72. 'customize:header' : '',
  73. 'customize:title' : '',
  74. 'customize:highlightJsStyle' : 'github',
  75. 'customize:highlightJsStyleBorder' : false,
  76. 'customize:theme' : 'default',
  77. 'customize:behavior' : 'crowi',
  78. 'customize:layout' : 'crowi',
  79. 'customize:isEnabledTimeline' : true,
  80. 'customize:isSavedStatesOfTabChanges' : true,
  81. 'customize:isEnabledAttachTitleHeader' : false,
  82. };
  83. /* eslint-enable */
  84. }
  85. function getDefaultMarkdownConfigs() {
  86. return {
  87. 'markdown:isEnabledLinebreaks': false,
  88. 'markdown:isEnabledPreventXSS': false,
  89. 'markdown:isEnabledLinebreaksInComments': true,
  90. };
  91. }
  92. function getValueForCrowiNS(config, key) {
  93. // return the default value if undefined
  94. if (undefined === config.crowi || undefined === config.crowi[key]) {
  95. return getDefaultCrowiConfigs()[key];
  96. }
  97. return config.crowi[key];
  98. }
  99. configSchema.statics.getRestrictGuestModeLabels = function() {
  100. var labels = {};
  101. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'security_setting.guest_mode.deny';
  102. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = 'security_setting.guest_mode.readonly';
  103. return labels;
  104. };
  105. configSchema.statics.getRegistrationModeLabels = function() {
  106. var labels = {};
  107. labels[SECURITY_REGISTRATION_MODE_OPEN] = 'security_setting.registration_mode.open';
  108. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = 'security_setting.registration_mode.restricted';
  109. labels[SECURITY_REGISTRATION_MODE_CLOSED] = 'security_setting.registration_mode.closed';
  110. return labels;
  111. };
  112. configSchema.statics.updateConfigCache = function(ns, config) {
  113. var originalConfig = crowi.getConfig();
  114. var newNSConfig = originalConfig[ns] || {};
  115. Object.keys(config).forEach(function(key) {
  116. if (config[key] || config[key] === '' || config[key] === false) {
  117. newNSConfig[key] = config[key];
  118. }
  119. });
  120. originalConfig[ns] = newNSConfig;
  121. crowi.setConfig(originalConfig);
  122. // initialize custom css/script
  123. Config.initCustomCss(originalConfig);
  124. Config.initCustomScript(originalConfig);
  125. };
  126. // Execute only once for installing application
  127. configSchema.statics.applicationInstall = function(callback) {
  128. var Config = this;
  129. Config.count({ ns: 'crowi' }, function(err, count) {
  130. if (count > 0) {
  131. return callback(new Error('Application already installed'), null);
  132. }
  133. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  134. Config.updateConfigCache('crowi', configs);
  135. return callback(err, configs);
  136. });
  137. });
  138. };
  139. configSchema.statics.setupCofigFormData = function(ns, config) {
  140. var defaultConfig = {};
  141. // set Default Settings
  142. if (ns === 'crowi') {
  143. defaultConfig = getDefaultCrowiConfigs();
  144. }
  145. else if (ns === 'markdown') {
  146. defaultConfig = getDefaultMarkdownConfigs();
  147. }
  148. if (!defaultConfig[ns]) {
  149. defaultConfig[ns] = {};
  150. }
  151. Object.keys(config[ns] || {}).forEach(function(key) {
  152. if (config[ns][key] !== undefined) {
  153. defaultConfig[key] = config[ns][key];
  154. }
  155. });
  156. return defaultConfig;
  157. };
  158. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback) {
  159. var Config = this;
  160. if (configs.length < 0) {
  161. return callback(new Error('Argument #1 is not array.'), null);
  162. }
  163. Object.keys(configs).forEach(function(key) {
  164. var value = configs[key];
  165. Config.findOneAndUpdate(
  166. { ns: ns, key: key },
  167. { ns: ns, key: key, value: JSON.stringify(value) },
  168. { upsert: true, },
  169. function(err, config) {
  170. debug('Config.findAndUpdate', err, config);
  171. });
  172. });
  173. return callback(null, configs);
  174. };
  175. configSchema.statics.findAndUpdate = function(ns, key, value, callback) {
  176. var Config = this;
  177. Config.findOneAndUpdate(
  178. { ns: ns, key: key },
  179. { ns: ns, key: key, value: JSON.stringify(value) },
  180. { upsert: true, },
  181. function(err, config) {
  182. debug('Config.findAndUpdate', err, config);
  183. callback(err, config);
  184. });
  185. };
  186. configSchema.statics.getConfig = function(callback) {
  187. };
  188. configSchema.statics.loadAllConfig = function(callback) {
  189. var Config = this
  190. , config = {};
  191. config.crowi = {}; // crowi namespace
  192. Config.find()
  193. .sort({ns: 1, key: 1})
  194. .exec(function(err, doc) {
  195. doc.forEach(function(el) {
  196. if (!config[el.ns]) {
  197. config[el.ns] = {};
  198. }
  199. config[el.ns][el.key] = JSON.parse(el.value);
  200. });
  201. debug('Config loaded', config);
  202. // initialize custom css/script
  203. Config.initCustomCss(config);
  204. Config.initCustomScript(config);
  205. return callback(null, config);
  206. });
  207. };
  208. configSchema.statics.appTitle = function(config) {
  209. const key = 'app:title';
  210. return getValueForCrowiNS(config, key) || 'GROWI';
  211. };
  212. configSchema.statics.isEnabledPassport = function(config) {
  213. // always true if growi installed cleanly
  214. if (Object.keys(config.crowi).length == 0) {
  215. return true;
  216. }
  217. const key = 'security:isEnabledPassport';
  218. return getValueForCrowiNS(config, key);
  219. };
  220. configSchema.statics.isEnabledPassportLdap = function(config) {
  221. const key = 'security:passport-ldap:isEnabled';
  222. return getValueForCrowiNS(config, key);
  223. };
  224. configSchema.statics.isEnabledPassportGoogle = function(config) {
  225. const key = 'security:passport-google:isEnabled';
  226. return getValueForCrowiNS(config, key);
  227. };
  228. configSchema.statics.isEnabledPassportGitHub = function(config) {
  229. const key = 'security:passport-github:isEnabled';
  230. return getValueForCrowiNS(config, key);
  231. };
  232. configSchema.statics.isSameUsernameTreatedAsIdenticalUser = function(config, providerType) {
  233. const key = `security:passport-${providerType}:isSameUsernameTreatedAsIdenticalUser`;
  234. return getValueForCrowiNS(config, key);
  235. };
  236. configSchema.statics.isUploadable = function(config) {
  237. var method = crowi.env.FILE_UPLOAD || 'aws';
  238. if (method == 'aws' && (
  239. !config.crowi['aws:accessKeyId'] ||
  240. !config.crowi['aws:secretAccessKey'] ||
  241. !config.crowi['aws:region'] ||
  242. !config.crowi['aws:bucket'])) {
  243. return false;
  244. }
  245. return method != 'none';
  246. };
  247. configSchema.statics.isGuesstAllowedToRead = function(config) {
  248. // return false if undefined
  249. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  250. return false;
  251. }
  252. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  253. };
  254. configSchema.statics.isEnabledPlugins = function(config) {
  255. const key = 'plugin:isEnabledPlugins';
  256. return getValueForCrowiNS(config, key);
  257. };
  258. configSchema.statics.isEnabledLinebreaks = function(config) {
  259. const key = 'markdown:isEnabledLinebreaks';
  260. // return default value if undefined
  261. if (undefined === config.markdown || undefined === config.markdown[key]) {
  262. return getDefaultMarkdownConfigs()[key];
  263. }
  264. return config.markdown[key];
  265. };
  266. configSchema.statics.isEnabledLinebreaksInComments = function(config) {
  267. const key = 'markdown:isEnabledLinebreaksInComments';
  268. // return default value if undefined
  269. if (undefined === config.markdown || undefined === config.markdown[key]) {
  270. return getDefaultMarkdownConfigs()[key];
  271. }
  272. return config.markdown[key];
  273. };
  274. configSchema.statics.isEnabledPreventXSS = function(config) {
  275. const key = 'markdown:isEnabledPreventXSS';
  276. // return default value if undefined
  277. if (undefined === config.markdown || undefined === config.markdown[key]) {
  278. return getDefaultMarkdownConfigs[key];
  279. }
  280. return config.markdown[key];
  281. };
  282. /**
  283. * initialize custom css strings
  284. */
  285. configSchema.statics.initCustomCss = function(config) {
  286. const key = 'customize:css';
  287. const rawCss = getValueForCrowiNS(config, key);
  288. // uglify and store
  289. this._customCss = uglifycss.processString(rawCss);
  290. };
  291. configSchema.statics.customCss = function(config) {
  292. return this._customCss;
  293. };
  294. configSchema.statics.initCustomScript = function(config) {
  295. const key = 'customize:script';
  296. const rawScript = getValueForCrowiNS(config, key);
  297. // store as is
  298. this._customScript = rawScript;
  299. };
  300. configSchema.statics.customScript = function(config) {
  301. return this._customScript;
  302. };
  303. configSchema.statics.customHeader = function(config) {
  304. const key = 'customize:header';
  305. return getValueForCrowiNS(config, key);
  306. };
  307. configSchema.statics.theme = function(config) {
  308. const key = 'customize:theme';
  309. return getValueForCrowiNS(config, key);
  310. };
  311. configSchema.statics.customTitle = function(config, page) {
  312. const key = 'customize:title';
  313. let customTitle = getValueForCrowiNS(config, key);
  314. if (customTitle == null || customTitle.trim().length == 0) {
  315. customTitle = '{{page}} - {{sitename}}';
  316. }
  317. return customTitle
  318. .replace('{{sitename}}', this.appTitle(config))
  319. .replace('{{page}}', page);
  320. };
  321. configSchema.statics.behaviorType = function(config) {
  322. const key = 'customize:behavior';
  323. return getValueForCrowiNS(config, key);
  324. };
  325. configSchema.statics.layoutType = function(config) {
  326. const key = 'customize:layout';
  327. return getValueForCrowiNS(config, key);
  328. };
  329. configSchema.statics.highlightJsStyle = function(config) {
  330. const key = 'customize:highlightJsStyle';
  331. return getValueForCrowiNS(config, key);
  332. };
  333. configSchema.statics.highlightJsStyleBorder = function(config) {
  334. const key = 'customize:highlightJsStyleBorder';
  335. return getValueForCrowiNS(config, key);
  336. };
  337. configSchema.statics.isEnabledTimeline = function(config) {
  338. const key = 'customize:isEnabledTimeline';
  339. return getValueForCrowiNS(config, key);
  340. };
  341. configSchema.statics.isSavedStatesOfTabChanges = function(config) {
  342. const key = 'customize:isSavedStatesOfTabChanges';
  343. return getValueForCrowiNS(config, key);
  344. };
  345. configSchema.statics.isEnabledAttachTitleHeader = function(config) {
  346. const key = 'customize:isEnabledAttachTitleHeader';
  347. return getValueForCrowiNS(config, key);
  348. };
  349. configSchema.statics.fileUploadEnabled = function(config) {
  350. const Config = this;
  351. if (!Config.isUploadable(config)) {
  352. return false;
  353. }
  354. // convert to boolean
  355. return !!config.crowi['app:fileUpload'];
  356. };
  357. configSchema.statics.hasSlackConfig = function(config) {
  358. return Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config);
  359. };
  360. /**
  361. * for Slack Incoming Webhooks
  362. */
  363. configSchema.statics.hasSlackIwhUrl = function(config) {
  364. if (!config.notification) {
  365. return false;
  366. }
  367. return (config.notification['slack:incomingWebhookUrl'] ? true : false);
  368. };
  369. configSchema.statics.isIncomingWebhookPrioritized = function(config) {
  370. if (!config.notification) {
  371. return false;
  372. }
  373. return (config.notification['slack:isIncomingWebhookPrioritized'] ? true : false);
  374. };
  375. configSchema.statics.hasSlackToken = function(config) {
  376. if (!config.notification) {
  377. return false;
  378. }
  379. return (config.notification['slack:token'] ? true : false);
  380. };
  381. configSchema.statics.getLocalconfig = function(config) {
  382. const Config = this;
  383. const env = crowi.getEnv();
  384. const local_config = {
  385. crowi: {
  386. title: Config.appTitle(crowi),
  387. url: config.crowi['app:url'] || '',
  388. },
  389. upload: {
  390. image: Config.isUploadable(config),
  391. file: Config.fileUploadEnabled(config),
  392. },
  393. behaviorType: Config.behaviorType(config),
  394. layoutType: Config.layoutType(config),
  395. isEnabledLinebreaks: Config.isEnabledLinebreaks(config),
  396. isEnabledLinebreaksInComments: Config.isEnabledLinebreaksInComments(config),
  397. highlightJsStyleBorder: Config.highlightJsStyleBorder(config),
  398. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  399. env: {
  400. PLANTUML_URI: env.PLANTUML_URI || null,
  401. BLOCKDIAG_URI: env.BLOCKDIAG_URI || null,
  402. MATHJAX: env.MATHJAX || null,
  403. },
  404. };
  405. return local_config;
  406. };
  407. /*
  408. configSchema.statics.isInstalled = function(config)
  409. {
  410. if (!config.crowi) {
  411. return false;
  412. }
  413. if (config.crowi['app:installed']
  414. && config.crowi['app:installed'] !== '0.0.0') {
  415. return true;
  416. }
  417. return false;
  418. }
  419. */
  420. Config = mongoose.model('Config', configSchema);
  421. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  422. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  423. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  424. return Config;
  425. };