config.js 12 KB

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