2
0

config.js 5.9 KB

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