config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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' : false,
  37. };
  38. }
  39. configSchema.statics.getRegistrationModeLabels = function()
  40. {
  41. var labels = {};
  42. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  43. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  44. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  45. return labels;
  46. };
  47. configSchema.statics.updateConfigCache = function(ns, config)
  48. {
  49. var originalConfig = crowi.getConfig();
  50. var newNSConfig = originalConfig[ns] || {};
  51. Object.keys(config).forEach(function (key) {
  52. if (config[key] || config[key] === '' || config[key] === false) {
  53. newNSConfig[key] = config[key];
  54. }
  55. });
  56. originalConfig[ns] = newNSConfig;
  57. crowi.setConfig(originalConfig);
  58. };
  59. // Execute only once for installing application
  60. configSchema.statics.applicationInstall = function(callback)
  61. {
  62. var Config = this;
  63. Config.count({ ns: 'crowi' }, function (err, count) {
  64. if (count > 0) {
  65. return callback(new Error('Application already installed'), null);
  66. }
  67. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  68. Config.updateConfigCache('crowi', configs);
  69. return callback(err, configs);
  70. });
  71. });
  72. };
  73. configSchema.statics.setupCofigFormData = function(ns, config)
  74. {
  75. var defaultConfig = {};
  76. if (ns === 'crowi') {
  77. defaultConfig = getArrayForInstalling();
  78. }
  79. if (!defaultConfig[ns]) {
  80. defaultConfig[ns] = {};
  81. }
  82. Object.keys(config[ns] || {}).forEach(function (key) {
  83. if (config[ns][key]) {
  84. defaultConfig[key] = config[ns][key];
  85. }
  86. });
  87. return defaultConfig;
  88. };
  89. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  90. {
  91. var Config = this;
  92. if (configs.length < 0) {
  93. return callback(new Error('Argument #1 is not array.'), null);
  94. }
  95. Object.keys(configs).forEach(function (key) {
  96. var value = configs[key];
  97. Config.findOneAndUpdate(
  98. { ns: ns, key: key },
  99. { ns: ns, key: key, value: JSON.stringify(value) },
  100. { upsert: true, },
  101. function (err, config) {
  102. debug('Config.findAndUpdate', err, config);
  103. });
  104. });
  105. return callback(null, configs);
  106. };
  107. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  108. {
  109. var Config = this;
  110. Config.findOneAndUpdate(
  111. { ns: ns, key: key },
  112. { ns: ns, key: key, value: JSON.stringify(value) },
  113. { upsert: true, },
  114. function (err, config) {
  115. debug('Config.findAndUpdate', err, config);
  116. callback(err, config);
  117. });
  118. };
  119. configSchema.statics.getConfig = function(callback)
  120. {
  121. };
  122. configSchema.statics.loadAllConfig = function(callback)
  123. {
  124. var Config = this
  125. , config = {};
  126. config.crowi = {}; // crowi namespace
  127. Config.find()
  128. .sort({ns: 1, key: 1})
  129. .exec(function(err, doc) {
  130. doc.forEach(function(el) {
  131. if (!config[el.ns]) {
  132. config[el.ns] = {};
  133. }
  134. config[el.ns][el.key] = JSON.parse(el.value);
  135. });
  136. debug('Config loaded', config);
  137. return callback(null, config);
  138. });
  139. };
  140. configSchema.statics.isUploadable = function(config)
  141. {
  142. var method = crowi.env.FILE_UPLOAD || 'aws';
  143. if (method == 'aws' && (
  144. !config.crowi['aws:accessKeyId'] ||
  145. !config.crowi['aws:secretAccessKey'] ||
  146. !config.crowi['aws:region'] ||
  147. !config.crowi['aws:bucket'])) {
  148. return false;
  149. }
  150. return method != 'none';
  151. };
  152. configSchema.statics.hasSlackConfig = function(config)
  153. {
  154. if (!config.notification) {
  155. return false;
  156. }
  157. if (!config.notification['slack:clientId'] ||
  158. !config.notification['slack:clientSecret']) {
  159. return false;
  160. }
  161. return true;
  162. };
  163. configSchema.statics.hasSlackToken = function(config)
  164. {
  165. if (!this.hasSlackConfig(config)) {
  166. return false;
  167. }
  168. if (!config.notification['slack:token']) {
  169. return false;
  170. }
  171. return true;
  172. };
  173. /*
  174. configSchema.statics.isInstalled = function(config)
  175. {
  176. if (!config.crowi) {
  177. return false;
  178. }
  179. if (config.crowi['app:installed']
  180. && config.crowi['app:installed'] !== '0.0.0') {
  181. return true;
  182. }
  183. return false;
  184. }
  185. */
  186. Config = mongoose.model('Config', configSchema);
  187. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  188. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  189. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  190. return Config;
  191. };