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. '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. if (!defaultConfig[ns]) {
  81. defaultConfig[ns] = {};
  82. }
  83. Object.keys(config[ns] || {}).forEach(function (key) {
  84. if (config[ns][key]) {
  85. defaultConfig[key] = config[ns][key];
  86. }
  87. });
  88. return defaultConfig;
  89. };
  90. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  91. {
  92. var Config = this;
  93. if (configs.length < 0) {
  94. return callback(new Error('Argument #1 is not array.'), null);
  95. }
  96. Object.keys(configs).forEach(function (key) {
  97. var value = configs[key];
  98. Config.findOneAndUpdate(
  99. { ns: ns, key: key },
  100. { ns: ns, key: key, value: JSON.stringify(value) },
  101. { upsert: true, },
  102. function (err, config) {
  103. debug('Config.findAndUpdate', err, config);
  104. });
  105. });
  106. return callback(null, configs);
  107. };
  108. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  109. {
  110. var Config = this;
  111. Config.findOneAndUpdate(
  112. { ns: ns, key: key },
  113. { ns: ns, key: key, value: JSON.stringify(value) },
  114. { upsert: true, },
  115. function (err, config) {
  116. debug('Config.findAndUpdate', err, config);
  117. callback(err, config);
  118. });
  119. };
  120. configSchema.statics.getConfig = function(callback)
  121. {
  122. };
  123. configSchema.statics.loadAllConfig = function(callback)
  124. {
  125. var Config = this
  126. , config = {};
  127. config.crowi = {}; // crowi namespace
  128. Config.find()
  129. .sort({ns: 1, key: 1})
  130. .exec(function(err, doc) {
  131. doc.forEach(function(el) {
  132. if (!config[el.ns]) {
  133. config[el.ns] = {};
  134. }
  135. config[el.ns][el.key] = JSON.parse(el.value);
  136. });
  137. debug('Config loaded', config);
  138. return callback(null, config);
  139. });
  140. };
  141. configSchema.statics.isUploadable = function(config)
  142. {
  143. var method = crowi.env.FILE_UPLOAD || 'aws';
  144. if (method == 'aws' && (
  145. !config.crowi['aws:accessKeyId'] ||
  146. !config.crowi['aws:secretAccessKey'] ||
  147. !config.crowi['aws:region'] ||
  148. !config.crowi['aws:bucket'])) {
  149. return false;
  150. }
  151. return method != 'none';
  152. };
  153. configSchema.statics.hasSlackConfig = function(config)
  154. {
  155. if (!config.notification) {
  156. return false;
  157. }
  158. if (!config.notification['slack:clientId'] ||
  159. !config.notification['slack:clientSecret']) {
  160. return false;
  161. }
  162. return true;
  163. };
  164. configSchema.statics.hasSlackToken = function(config)
  165. {
  166. if (!this.hasSlackConfig(config)) {
  167. return false;
  168. }
  169. if (!config.notification['slack:token']) {
  170. return false;
  171. }
  172. return true;
  173. };
  174. /*
  175. configSchema.statics.isInstalled = function(config)
  176. {
  177. if (!config.crowi) {
  178. return false;
  179. }
  180. if (config.crowi['app:installed']
  181. && config.crowi['app:installed'] !== '0.0.0') {
  182. return true;
  183. }
  184. return false;
  185. }
  186. */
  187. Config = mongoose.model('Config', configSchema);
  188. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  189. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  190. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  191. return Config;
  192. };