config.js 15 KB

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