config.js 11 KB

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