config.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. module.exports = function(crowi) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('crowi:models:config')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , configSchema
  6. , Config
  7. , SECURITY_REGISTRATION_MODE_OPEN = 'Open'
  8. , SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted'
  9. , SECURITY_REGISTRATION_MODE_CLOSED = 'Closed'
  10. ;
  11. configSchema = new mongoose.Schema({
  12. ns: { type: String, required: true, index: true },
  13. key: { type: String, required: true, index: true },
  14. value: { type: String, required: true }
  15. });
  16. function getArrayForInstalling()
  17. {
  18. return {
  19. //'app:installed' : "0.0.0",
  20. 'app:title' : 'Crowi',
  21. 'app:confidential' : '',
  22. 'security:registrationMode' : 'Open',
  23. 'security:registrationWhiteList' : [],
  24. 'aws:bucket' : 'crowi',
  25. 'aws:region' : 'ap-northeast-1',
  26. 'aws:accessKeyId' : '',
  27. 'aws:secretAccessKey' : '',
  28. 'mail:from' : '',
  29. 'mail:smtpHost' : '',
  30. 'mail:smtpPort' : '',
  31. 'mail:smtpUser' : '',
  32. 'mail:smtpPassword' : '',
  33. 'searcher:url': '',
  34. 'google:clientId' : '',
  35. 'google:clientSecret' : '',
  36. 'plugin:isEnabledPlugins' : true,
  37. };
  38. }
  39. function getDefaultMarkdownConfigs() {
  40. return {
  41. 'markdown:isEnabledLinebreaks': true,
  42. 'markdown:isEnabledLinebreaksInComments': true,
  43. }
  44. }
  45. configSchema.statics.getRegistrationModeLabels = function()
  46. {
  47. var labels = {};
  48. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  49. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  50. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  51. return labels;
  52. };
  53. configSchema.statics.updateConfigCache = function(ns, config)
  54. {
  55. var originalConfig = crowi.getConfig();
  56. var newNSConfig = originalConfig[ns] || {};
  57. Object.keys(config).forEach(function (key) {
  58. if (config[key] || config[key] === '' || config[key] === false) {
  59. newNSConfig[key] = config[key];
  60. }
  61. });
  62. originalConfig[ns] = newNSConfig;
  63. crowi.setConfig(originalConfig);
  64. };
  65. // Execute only once for installing application
  66. configSchema.statics.applicationInstall = function(callback)
  67. {
  68. var Config = this;
  69. Config.count({ ns: 'crowi' }, function (err, count) {
  70. if (count > 0) {
  71. return callback(new Error('Application already installed'), null);
  72. }
  73. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  74. Config.updateConfigCache('crowi', configs);
  75. return callback(err, configs);
  76. });
  77. });
  78. };
  79. configSchema.statics.setupCofigFormData = function(ns, config)
  80. {
  81. var defaultConfig = {};
  82. // set Default Settings
  83. if (ns === 'crowi') {
  84. defaultConfig = getArrayForInstalling();
  85. }
  86. else if (ns === 'markdown') {
  87. defaultConfig = getDefaultMarkdownConfigs();
  88. }
  89. if (!defaultConfig[ns]) {
  90. defaultConfig[ns] = {};
  91. }
  92. Object.keys(config[ns] || {}).forEach(function (key) {
  93. if (config[ns][key] !== undefined) {
  94. defaultConfig[key] = config[ns][key];
  95. }
  96. });
  97. return defaultConfig;
  98. };
  99. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  100. {
  101. var Config = this;
  102. if (configs.length < 0) {
  103. return callback(new Error('Argument #1 is not array.'), null);
  104. }
  105. Object.keys(configs).forEach(function (key) {
  106. var value = configs[key];
  107. Config.findOneAndUpdate(
  108. { ns: ns, key: key },
  109. { ns: ns, key: key, value: JSON.stringify(value) },
  110. { upsert: true, },
  111. function (err, config) {
  112. debug('Config.findAndUpdate', err, config);
  113. });
  114. });
  115. return callback(null, configs);
  116. };
  117. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  118. {
  119. var Config = this;
  120. Config.findOneAndUpdate(
  121. { ns: ns, key: key },
  122. { ns: ns, key: key, value: JSON.stringify(value) },
  123. { upsert: true, },
  124. function (err, config) {
  125. debug('Config.findAndUpdate', err, config);
  126. callback(err, config);
  127. });
  128. };
  129. configSchema.statics.getConfig = function(callback)
  130. {
  131. };
  132. configSchema.statics.loadAllConfig = function(callback)
  133. {
  134. var Config = this
  135. , config = {};
  136. config.crowi = {}; // crowi namespace
  137. Config.find()
  138. .sort({ns: 1, key: 1})
  139. .exec(function(err, doc) {
  140. doc.forEach(function(el) {
  141. if (!config[el.ns]) {
  142. config[el.ns] = {};
  143. }
  144. config[el.ns][el.key] = JSON.parse(el.value);
  145. });
  146. debug('Config loaded', config);
  147. return callback(null, config);
  148. });
  149. };
  150. configSchema.statics.isUploadable = function(config)
  151. {
  152. var method = crowi.env.FILE_UPLOAD || 'aws';
  153. if (method == 'aws' && (
  154. !config.crowi['aws:accessKeyId'] ||
  155. !config.crowi['aws:secretAccessKey'] ||
  156. !config.crowi['aws:region'] ||
  157. !config.crowi['aws:bucket'])) {
  158. return false;
  159. }
  160. return method != 'none';
  161. };
  162. configSchema.statics.isEnabledLinebreaks = function(config)
  163. {
  164. var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaks'];
  165. if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaks']) {
  166. return defaultValue;
  167. }
  168. return config.markdown['markdown:isEnabledLinebreaks'];
  169. };
  170. configSchema.statics.isEnabledLinebreaksInComments = function(config)
  171. {
  172. var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaksInComments'];
  173. if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaksInComments']) {
  174. return defaultValue;
  175. }
  176. return config.markdown['markdown:isEnabledLinebreaksInComments'];
  177. };
  178. configSchema.statics.hasSlackConfig = function(config)
  179. {
  180. if (!config.notification) {
  181. return false;
  182. }
  183. if (!config.notification['slack:clientId'] ||
  184. !config.notification['slack:clientSecret']) {
  185. return false;
  186. }
  187. return true;
  188. };
  189. configSchema.statics.hasSlackToken = function(config)
  190. {
  191. if (!this.hasSlackConfig(config)) {
  192. return false;
  193. }
  194. if (!config.notification['slack:token']) {
  195. return false;
  196. }
  197. return true;
  198. };
  199. /*
  200. configSchema.statics.isInstalled = function(config)
  201. {
  202. if (!config.crowi) {
  203. return false;
  204. }
  205. if (config.crowi['app:installed']
  206. && config.crowi['app:installed'] !== '0.0.0') {
  207. return true;
  208. }
  209. return false;
  210. }
  211. */
  212. Config = mongoose.model('Config', configSchema);
  213. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  214. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  215. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  216. return Config;
  217. };