config.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. module.exports = function(crowi) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('crowi:models:config')
  4. , uglifycss = require('uglifycss')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  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. config['customize:isEnabledAttachTitleHeader'] = false;
  31. return config;
  32. }
  33. /**
  34. * default values when migrated from Official Crowi
  35. */
  36. function getDefaultCrowiConfigs()
  37. {
  38. return {
  39. //'app:installed' : "0.0.0",
  40. 'app:confidential' : '',
  41. 'app:fileUpload' : false,
  42. 'security:restrictGuestMode' : 'Deny',
  43. 'security:registrationMode' : 'Open',
  44. 'security:registrationWhiteList' : [],
  45. 'security:isEnabledPassport' : false,
  46. 'security:passport-ldap:isEnabled' : false,
  47. 'security:passport-ldap:serverUrl' : undefined,
  48. 'security:passport-ldap:isUserBind' : undefined,
  49. 'security:passport-ldap:bindDN' : undefined,
  50. 'security:passport-ldap:bindDNPassword' : undefined,
  51. 'security:passport-ldap:searchFilter' : undefined,
  52. 'security:passport-ldap:attrMapUsername' : undefined,
  53. 'security:passport-ldap:groupSearchBase' : undefined,
  54. 'security:passport-ldap:groupSearchFilter' : undefined,
  55. 'security:passport-ldap:groupDnProperty' : undefined,
  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' : true,
  80. };
  81. }
  82. function getDefaultMarkdownConfigs() {
  83. return {
  84. 'markdown:isEnabledLinebreaks': true,
  85. 'markdown:isEnabledLinebreaksInComments': true,
  86. }
  87. }
  88. function getValueForCrowiNS(config, key) {
  89. // return the default value if undefined
  90. if (undefined === config.crowi || undefined === config.crowi[key]) {
  91. return getDefaultCrowiConfigs()[key];
  92. }
  93. return config.crowi[key];
  94. }
  95. configSchema.statics.getRestrictGuestModeLabels = function()
  96. {
  97. var labels = {};
  98. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'アカウントを持たないユーザーはアクセス不可';
  99. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = '閲覧のみ許可';
  100. return labels;
  101. };
  102. configSchema.statics.getRegistrationModeLabels = function()
  103. {
  104. var labels = {};
  105. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  106. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  107. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  108. return labels;
  109. };
  110. configSchema.statics.updateConfigCache = function(ns, config)
  111. {
  112. var originalConfig = crowi.getConfig();
  113. var newNSConfig = originalConfig[ns] || {};
  114. Object.keys(config).forEach(function (key) {
  115. if (config[key] || config[key] === '' || config[key] === false) {
  116. newNSConfig[key] = config[key];
  117. }
  118. });
  119. originalConfig[ns] = newNSConfig;
  120. crowi.setConfig(originalConfig);
  121. // initialize custom css/script
  122. Config.initCustomCss(originalConfig);
  123. Config.initCustomScript(originalConfig);
  124. };
  125. // Execute only once for installing application
  126. configSchema.statics.applicationInstall = function(callback)
  127. {
  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. {
  141. var defaultConfig = {};
  142. // set Default Settings
  143. if (ns === 'crowi') {
  144. defaultConfig = getDefaultCrowiConfigs();
  145. }
  146. else if (ns === 'markdown') {
  147. defaultConfig = getDefaultMarkdownConfigs();
  148. }
  149. if (!defaultConfig[ns]) {
  150. defaultConfig[ns] = {};
  151. }
  152. Object.keys(config[ns] || {}).forEach(function (key) {
  153. if (config[ns][key] !== undefined) {
  154. defaultConfig[key] = config[ns][key];
  155. }
  156. });
  157. return defaultConfig;
  158. };
  159. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  160. {
  161. var Config = this;
  162. if (configs.length < 0) {
  163. return callback(new Error('Argument #1 is not array.'), null);
  164. }
  165. Object.keys(configs).forEach(function (key) {
  166. var value = configs[key];
  167. Config.findOneAndUpdate(
  168. { ns: ns, key: key },
  169. { ns: ns, key: key, value: JSON.stringify(value) },
  170. { upsert: true, },
  171. function (err, config) {
  172. debug('Config.findAndUpdate', err, config);
  173. });
  174. });
  175. return callback(null, configs);
  176. };
  177. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  178. {
  179. var Config = this;
  180. Config.findOneAndUpdate(
  181. { ns: ns, key: key },
  182. { ns: ns, key: key, value: JSON.stringify(value) },
  183. { upsert: true, },
  184. function (err, config) {
  185. debug('Config.findAndUpdate', err, config);
  186. callback(err, config);
  187. });
  188. };
  189. configSchema.statics.getConfig = function(callback)
  190. {
  191. };
  192. configSchema.statics.loadAllConfig = function(callback)
  193. {
  194. var Config = this
  195. , config = {};
  196. config.crowi = {}; // crowi namespace
  197. Config.find()
  198. .sort({ns: 1, key: 1})
  199. .exec(function(err, doc) {
  200. doc.forEach(function(el) {
  201. if (!config[el.ns]) {
  202. config[el.ns] = {};
  203. }
  204. config[el.ns][el.key] = JSON.parse(el.value);
  205. });
  206. debug('Config loaded', config);
  207. // initialize custom css/script
  208. Config.initCustomCss(config);
  209. Config.initCustomScript(config);
  210. return callback(null, config);
  211. });
  212. };
  213. configSchema.statics.appTitle = function(config)
  214. {
  215. const key = 'app:title';
  216. return getValueForCrowiNS(config, key) || 'GROWI';
  217. };
  218. configSchema.statics.isEnabledPassport = function(config)
  219. {
  220. // always true if growi installed cleanly
  221. if (Object.keys(config.crowi).length == 0) {
  222. return true;
  223. }
  224. const key = 'security:isEnabledPassport';
  225. return getValueForCrowiNS(config, key);
  226. };
  227. configSchema.statics.isEnabledPassportLdap = function(config)
  228. {
  229. const key = 'security:passport-ldap:isEnabled';
  230. return getValueForCrowiNS(config, key);
  231. };
  232. configSchema.statics.isUploadable = function(config)
  233. {
  234. var method = crowi.env.FILE_UPLOAD || 'aws';
  235. if (method == 'aws' && (
  236. !config.crowi['aws:accessKeyId'] ||
  237. !config.crowi['aws:secretAccessKey'] ||
  238. !config.crowi['aws:region'] ||
  239. !config.crowi['aws:bucket'])) {
  240. return false;
  241. }
  242. return method != 'none';
  243. };
  244. configSchema.statics.isGuesstAllowedToRead = function(config)
  245. {
  246. // return false if undefined
  247. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  248. return false;
  249. }
  250. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  251. };
  252. configSchema.statics.isEnabledPlugins = function(config)
  253. {
  254. const key = 'plugin:isEnabledPlugins';
  255. return getValueForCrowiNS(config, key);
  256. };
  257. configSchema.statics.isEnabledLinebreaks = function(config)
  258. {
  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. {
  268. const key = 'markdown:isEnabledLinebreaksInComments';
  269. // return default value if undefined
  270. if (undefined === config.markdown || undefined === config.markdown[key]) {
  271. return getDefaultMarkdownConfigs[key];
  272. }
  273. return config.markdown[key];
  274. };
  275. /**
  276. * initialize custom css strings
  277. */
  278. configSchema.statics.initCustomCss = function(config)
  279. {
  280. const key = 'customize:css';
  281. const rawCss = getValueForCrowiNS(config, key);
  282. // uglify and store
  283. this._customCss = uglifycss.processString(rawCss);
  284. }
  285. configSchema.statics.customCss = function(config)
  286. {
  287. return this._customCss;
  288. }
  289. configSchema.statics.initCustomScript = function(config)
  290. {
  291. const key = 'customize:script';
  292. const rawScript = getValueForCrowiNS(config, key);
  293. // store as is
  294. this._customScript = rawScript;
  295. }
  296. configSchema.statics.customScript = function(config)
  297. {
  298. return this._customScript;
  299. }
  300. configSchema.statics.customHeader = function(config)
  301. {
  302. const key = 'customize:header';
  303. return getValueForCrowiNS(config, key);
  304. }
  305. configSchema.statics.theme = function(config)
  306. {
  307. const key = 'customize:theme';
  308. return getValueForCrowiNS(config, key);
  309. }
  310. configSchema.statics.customTitle = function(config, page)
  311. {
  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. {
  323. const key = 'customize:behavior';
  324. return getValueForCrowiNS(config, key);
  325. }
  326. configSchema.statics.layoutType = function(config)
  327. {
  328. const key = 'customize:layout';
  329. return getValueForCrowiNS(config, key);
  330. }
  331. configSchema.statics.highlightJsStyle = function(config)
  332. {
  333. const key = 'customize:highlightJsStyle';
  334. return getValueForCrowiNS(config, key);
  335. }
  336. configSchema.statics.highlightJsStyleBorder = function(config)
  337. {
  338. const key = 'customize:highlightJsStyleBorder';
  339. return getValueForCrowiNS(config, key);
  340. }
  341. configSchema.statics.isEnabledTimeline = function(config)
  342. {
  343. const key = 'customize:isEnabledTimeline';
  344. return getValueForCrowiNS(config, key);
  345. };
  346. configSchema.statics.isSavedStatesOfTabChanges = function(config)
  347. {
  348. const key = 'customize:isSavedStatesOfTabChanges';
  349. return getValueForCrowiNS(config, key);
  350. };
  351. configSchema.statics.isEnabledAttachTitleHeader = function(config)
  352. {
  353. const key = 'customize:isEnabledAttachTitleHeader';
  354. return getValueForCrowiNS(config, key);
  355. };
  356. configSchema.statics.fileUploadEnabled = function(config)
  357. {
  358. const Config = this;
  359. if (!Config.isUploadable(config)) {
  360. return false;
  361. }
  362. // convert to boolean
  363. return !!config.crowi['app:fileUpload'];
  364. };
  365. configSchema.statics.hasSlackConfig = function(config)
  366. {
  367. return Config.hasSlackWebClientConfig(config) || Config.hasSlackIwhUrl(config);
  368. };
  369. configSchema.statics.hasSlackWebClientConfig = function(config)
  370. {
  371. if (!config.notification) {
  372. return false;
  373. }
  374. if (!config.notification['slack:clientId'] ||
  375. !config.notification['slack:clientSecret']) {
  376. return false;
  377. }
  378. return true;
  379. };
  380. /**
  381. * for Slack Incoming Webhooks
  382. */
  383. configSchema.statics.hasSlackIwhUrl = function(config)
  384. {
  385. if (!config.notification) {
  386. return false;
  387. }
  388. return config.notification['slack:incomingWebhookUrl'];
  389. };
  390. configSchema.statics.isIncomingWebhookPrioritized = function(config)
  391. {
  392. if (!config.notification) {
  393. return false;
  394. }
  395. return config.notification['slack:isIncomingWebhookPrioritized'];
  396. };
  397. configSchema.statics.hasSlackToken = function(config)
  398. {
  399. if (!this.hasSlackWebClientConfig(config)) {
  400. return false;
  401. }
  402. if (!config.notification['slack:token']) {
  403. return false;
  404. }
  405. return true;
  406. };
  407. configSchema.statics.getLocalconfig = function(config)
  408. {
  409. const Config = this;
  410. const env = crowi.getEnv();
  411. const local_config = {
  412. crowi: {
  413. title: Config.appTitle(crowi),
  414. url: config.crowi['app:url'] || '',
  415. },
  416. upload: {
  417. image: Config.isUploadable(config),
  418. file: Config.fileUploadEnabled(config),
  419. },
  420. behaviorType: Config.behaviorType(config),
  421. layoutType: Config.layoutType(config),
  422. isEnabledLineBreaks: Config.isEnabledLinebreaks(config),
  423. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  424. isEnabledAttachTitleHeader: Config.isEnabledAttachTitleHeader(config),
  425. env: {
  426. PLANTUML_URI: env.PLANTUML_URI || null,
  427. MATHJAX: env.MATHJAX || null,
  428. },
  429. };
  430. return local_config;
  431. }
  432. /*
  433. configSchema.statics.isInstalled = function(config)
  434. {
  435. if (!config.crowi) {
  436. return false;
  437. }
  438. if (config.crowi['app:installed']
  439. && config.crowi['app:installed'] !== '0.0.0') {
  440. return true;
  441. }
  442. return false;
  443. }
  444. */
  445. Config = mongoose.model('Config', configSchema);
  446. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  447. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  448. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  449. return Config;
  450. };