config.js 14 KB

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