config.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 crowi-plus is cleanly installed
  21. */
  22. function getArrayForInstalling() {
  23. let config = getDefaultCrowiConfigs();
  24. // overwrite
  25. config['customize:behavior'] = 'crowi-plus';
  26. config['customize:layout'] = 'crowi-plus';
  27. config['customize:isSavedStatesOfTabChanges'] = false;
  28. return config;
  29. }
  30. /**
  31. * default values when migrated from Official Crowi
  32. */
  33. function getDefaultCrowiConfigs()
  34. {
  35. return {
  36. //'app:installed' : "0.0.0",
  37. 'app:title' : 'Crowi',
  38. 'app:confidential' : '',
  39. 'app:fileUpload' : false,
  40. 'security:restrictGuestMode' : 'Deny',
  41. 'security:registrationMode' : 'Open',
  42. 'security:registrationWhiteList' : [],
  43. 'aws:bucket' : 'crowi',
  44. 'aws:region' : 'ap-northeast-1',
  45. 'aws:accessKeyId' : '',
  46. 'aws:secretAccessKey' : '',
  47. 'mail:from' : '',
  48. 'mail:smtpHost' : '',
  49. 'mail:smtpPort' : '',
  50. 'mail:smtpUser' : '',
  51. 'mail:smtpPassword' : '',
  52. 'google:clientId' : '',
  53. 'google:clientSecret' : '',
  54. 'plugin:isEnabledPlugins' : true,
  55. 'customize:css' : '',
  56. 'customize:script' : '',
  57. 'customize:behavior' : 'crowi',
  58. 'customize:layout' : 'crowi',
  59. 'customize:isEnabledTimeline' : true,
  60. 'customize:isSavedStatesOfTabChanges' : true,
  61. };
  62. }
  63. function getDefaultMarkdownConfigs() {
  64. return {
  65. 'markdown:isEnabledLinebreaks': true,
  66. 'markdown:isEnabledLinebreaksInComments': true,
  67. }
  68. }
  69. function getValueForCrowiNS(config, key) {
  70. // return the default value if undefined
  71. if (undefined === config.crowi || undefined === config.crowi[key]) {
  72. return getDefaultCrowiConfigs()[key];
  73. }
  74. return config.crowi[key];
  75. }
  76. configSchema.statics.getRestrictGuestModeLabels = function()
  77. {
  78. var labels = {};
  79. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'アカウントを持たないユーザーはアクセス不可';
  80. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = '閲覧のみ許可';
  81. return labels;
  82. };
  83. configSchema.statics.getRegistrationModeLabels = function()
  84. {
  85. var labels = {};
  86. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  87. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  88. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  89. return labels;
  90. };
  91. configSchema.statics.updateConfigCache = function(ns, config)
  92. {
  93. var originalConfig = crowi.getConfig();
  94. var newNSConfig = originalConfig[ns] || {};
  95. Object.keys(config).forEach(function (key) {
  96. if (config[key] || config[key] === '' || config[key] === false) {
  97. newNSConfig[key] = config[key];
  98. }
  99. });
  100. originalConfig[ns] = newNSConfig;
  101. crowi.setConfig(originalConfig);
  102. // initialize custom css/script
  103. Config.initCustomCss(originalConfig);
  104. Config.initCustomScript(originalConfig);
  105. };
  106. // Execute only once for installing application
  107. configSchema.statics.applicationInstall = function(callback)
  108. {
  109. var Config = this;
  110. Config.count({ ns: 'crowi' }, function (err, count) {
  111. if (count > 0) {
  112. return callback(new Error('Application already installed'), null);
  113. }
  114. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  115. Config.updateConfigCache('crowi', configs);
  116. return callback(err, configs);
  117. });
  118. });
  119. };
  120. configSchema.statics.setupCofigFormData = function(ns, config)
  121. {
  122. var defaultConfig = {};
  123. // set Default Settings
  124. if (ns === 'crowi') {
  125. defaultConfig = getDefaultCrowiConfigs();
  126. }
  127. else if (ns === 'markdown') {
  128. defaultConfig = getDefaultMarkdownConfigs();
  129. }
  130. if (!defaultConfig[ns]) {
  131. defaultConfig[ns] = {};
  132. }
  133. Object.keys(config[ns] || {}).forEach(function (key) {
  134. if (config[ns][key] !== undefined) {
  135. defaultConfig[key] = config[ns][key];
  136. }
  137. });
  138. return defaultConfig;
  139. };
  140. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  141. {
  142. var Config = this;
  143. if (configs.length < 0) {
  144. return callback(new Error('Argument #1 is not array.'), null);
  145. }
  146. Object.keys(configs).forEach(function (key) {
  147. var value = configs[key];
  148. Config.findOneAndUpdate(
  149. { ns: ns, key: key },
  150. { ns: ns, key: key, value: JSON.stringify(value) },
  151. { upsert: true, },
  152. function (err, config) {
  153. debug('Config.findAndUpdate', err, config);
  154. });
  155. });
  156. return callback(null, configs);
  157. };
  158. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  159. {
  160. var Config = this;
  161. Config.findOneAndUpdate(
  162. { ns: ns, key: key },
  163. { ns: ns, key: key, value: JSON.stringify(value) },
  164. { upsert: true, },
  165. function (err, config) {
  166. debug('Config.findAndUpdate', err, config);
  167. callback(err, config);
  168. });
  169. };
  170. configSchema.statics.getConfig = function(callback)
  171. {
  172. };
  173. configSchema.statics.loadAllConfig = function(callback)
  174. {
  175. var Config = this
  176. , config = {};
  177. config.crowi = {}; // crowi namespace
  178. Config.find()
  179. .sort({ns: 1, key: 1})
  180. .exec(function(err, doc) {
  181. doc.forEach(function(el) {
  182. if (!config[el.ns]) {
  183. config[el.ns] = {};
  184. }
  185. config[el.ns][el.key] = JSON.parse(el.value);
  186. });
  187. debug('Config loaded', config);
  188. // initialize custom css/script
  189. Config.initCustomCss(config);
  190. Config.initCustomScript(config);
  191. return callback(null, config);
  192. });
  193. };
  194. configSchema.statics.isUploadable = function(config)
  195. {
  196. var method = crowi.env.FILE_UPLOAD || 'aws';
  197. if (method == 'aws' && (
  198. !config.crowi['aws:accessKeyId'] ||
  199. !config.crowi['aws:secretAccessKey'] ||
  200. !config.crowi['aws:region'] ||
  201. !config.crowi['aws:bucket'])) {
  202. return false;
  203. }
  204. return method != 'none';
  205. };
  206. configSchema.statics.isGuesstAllowedToRead = function(config)
  207. {
  208. // return false if undefined
  209. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  210. return false;
  211. }
  212. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  213. };
  214. configSchema.statics.isEnabledPlugins = function(config)
  215. {
  216. const key = 'plugin:isEnabledPlugins';
  217. return getValueForCrowiNS(config, key);
  218. };
  219. configSchema.statics.isEnabledLinebreaks = function(config)
  220. {
  221. const key = 'markdown:isEnabledLinebreaks';
  222. // return default value if undefined
  223. if (undefined === config.markdown || undefined === config.markdown[key]) {
  224. return getDefaultMarkdownConfigs[key];
  225. }
  226. return config.markdown[key];
  227. };
  228. configSchema.statics.isEnabledLinebreaksInComments = function(config)
  229. {
  230. const key = 'markdown:isEnabledLinebreaksInComments';
  231. // return default value if undefined
  232. if (undefined === config.markdown || undefined === config.markdown[key]) {
  233. return getDefaultMarkdownConfigs[key];
  234. }
  235. return config.markdown[key];
  236. };
  237. /**
  238. * initialize custom css strings
  239. */
  240. configSchema.statics.initCustomCss = function(config)
  241. {
  242. const key = 'customize:css';
  243. const rawCss = getValueForCrowiNS(config, key);
  244. // uglify and store
  245. this._customCss = uglifycss.processString(rawCss);
  246. }
  247. configSchema.statics.customCss = function(config)
  248. {
  249. return this._customCss;
  250. }
  251. configSchema.statics.initCustomScript = function(config)
  252. {
  253. const key = 'customize:script';
  254. const rawScript = getValueForCrowiNS(config, key);
  255. // store as is
  256. this._customScript = rawScript;
  257. }
  258. configSchema.statics.customScript = function(config)
  259. {
  260. return this._customScript;
  261. }
  262. configSchema.statics.behaviorType = function(config)
  263. {
  264. const key = 'customize:behavior';
  265. return getValueForCrowiNS(config, key);
  266. }
  267. configSchema.statics.layoutType = function(config)
  268. {
  269. const key = 'customize:layout';
  270. return getValueForCrowiNS(config, key);
  271. }
  272. configSchema.statics.isEnabledTimeline = function(config)
  273. {
  274. const key = 'customize:isEnabledTimeline';
  275. return getValueForCrowiNS(config, key);
  276. };
  277. configSchema.statics.isSavedStatesOfTabChanges = function(config)
  278. {
  279. const key = 'customize:isSavedStatesOfTabChanges';
  280. return getValueForCrowiNS(config, key);
  281. };
  282. configSchema.statics.fileUploadEnabled = function(config)
  283. {
  284. const Config = this;
  285. if (!Config.isUploadable(config)) {
  286. return false;
  287. }
  288. return config.crowi['app:fileUpload'] || false;
  289. };
  290. configSchema.statics.hasSlackConfig = function(config)
  291. {
  292. return Config.hasSlackAppConfig(config) || Config.hasSlackIwhUrl(config);
  293. };
  294. configSchema.statics.hasSlackAppConfig = function(config)
  295. {
  296. if (!config.notification) {
  297. return false;
  298. }
  299. if (!config.notification['slack:clientId'] ||
  300. !config.notification['slack:clientSecret']) {
  301. return false;
  302. }
  303. return true;
  304. };
  305. /**
  306. * for Slack Incoming Webhooks
  307. */
  308. configSchema.statics.hasSlackIwhUrl = function(config)
  309. {
  310. if (!config.notification) {
  311. return false;
  312. }
  313. return config.notification['slack:incomingWebhookUrl'];
  314. };
  315. configSchema.statics.isIncomingWebhookPrioritized = function(config)
  316. {
  317. if (!config.notification) {
  318. return false;
  319. }
  320. return config.notification['slack:isIncomingWebhookPrioritized'];
  321. };
  322. configSchema.statics.hasSlackToken = function(config)
  323. {
  324. if (!this.hasSlackAppConfig(config)) {
  325. return false;
  326. }
  327. if (!config.notification['slack:token']) {
  328. return false;
  329. }
  330. return true;
  331. };
  332. configSchema.statics.getLocalconfig = function(config)
  333. {
  334. const Config = this;
  335. const env = crowi.getEnv();
  336. const local_config = {
  337. crowi: {
  338. title: config.crowi['app:title'],
  339. url: config.crowi['app:url'] || '',
  340. },
  341. upload: {
  342. image: Config.isUploadable(config),
  343. file: Config.fileUploadEnabled(config),
  344. },
  345. behaviorType: Config.behaviorType(config),
  346. layoutType: Config.layoutType(config),
  347. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  348. env: {
  349. PLANTUML_URI: env.PLANTUML_URI || null,
  350. MATHJAX: env.MATHJAX || null,
  351. },
  352. };
  353. return local_config;
  354. }
  355. /*
  356. configSchema.statics.isInstalled = function(config)
  357. {
  358. if (!config.crowi) {
  359. return false;
  360. }
  361. if (config.crowi['app:installed']
  362. && config.crowi['app:installed'] !== '0.0.0') {
  363. return true;
  364. }
  365. return false;
  366. }
  367. */
  368. Config = mongoose.model('Config', configSchema);
  369. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  370. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  371. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  372. return Config;
  373. };