config.js 5.3 KB

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