config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. module.exports = function(app) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('crowi:models:config')
  4. , ObjectId = mongoose.Schema.Types.ObjectId
  5. , configSchema
  6. , Config
  7. ;
  8. configSchema = new mongoose.Schema({
  9. ns: { type: String, required: true, index: true },
  10. key: { type: String, required: true, index: true },
  11. value: { type: String, required: true }
  12. });
  13. function getArrayForInstalling()
  14. {
  15. return {
  16. 'app:title' : 'Crowi',
  17. 'app:confidential' : '',
  18. 'security:registrationMode' : 'Open',
  19. 'security:registrationWhiteList' : [],
  20. 'aws:bucket' : 'crowi',
  21. 'aws:region' : 'ap-northeast-1',
  22. 'aws:accessKeyId' : '',
  23. 'aws:secretAccessKey' : '',
  24. 'searcher:url': '',
  25. 'google:clientId' : '',
  26. 'google:clientSecret' : '',
  27. 'facebook:appId' : '',
  28. 'facebook:secret' : '',
  29. };
  30. }
  31. configSchema.statics.updateConfigCache = function(ns, config)
  32. {
  33. var originalConfig = app.set('config');
  34. var newNSConfig = originalConfig[ns] || {};
  35. Object.keys(config).forEach(function (key) {
  36. if (config[key] || config[key] === '') {
  37. newNSConfig[key] = config[key];
  38. }
  39. });
  40. originalConfig[ns] = newNSConfig;
  41. app.set('config', originalConfig);
  42. };
  43. // Execute only once for installing application
  44. configSchema.statics.applicationInstall = function(callback)
  45. {
  46. var Config = this;
  47. Config.count({ ns: 'crowi' }, function (err, count) {
  48. if (count > 0) {
  49. return callback(new Error('Application already installed'), null);
  50. }
  51. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  52. Config.updateConfigCache('crowi', configs);
  53. return callback(err, configs);
  54. });
  55. });
  56. };
  57. configSchema.statics.setupCofigFormData = function(ns, config)
  58. {
  59. var defaultConfig;
  60. if (ns === 'crowi') {
  61. defaultConfig = getArrayForInstalling();
  62. }
  63. Object.keys(config[ns]).forEach(function (key) {
  64. if (config[ns][key]) {
  65. defaultConfig[key] = config[ns][key];
  66. }
  67. });
  68. return defaultConfig;
  69. };
  70. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  71. {
  72. var Config = this;
  73. if (configs.length < 0) {
  74. return callback(new Error('Argument #1 is not array.'), null);
  75. }
  76. Object.keys(configs).forEach(function (key) {
  77. var value = configs[key];
  78. Config.findOneAndUpdate(
  79. { ns: ns, key: key },
  80. { ns: ns, key: key, value: JSON.stringify(value) },
  81. { upsert: true, },
  82. function (err, config) {
  83. debug('Config.findAndUpdate', err, config);
  84. });
  85. });
  86. return callback(null, configs);
  87. };
  88. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  89. {
  90. var Config = this;
  91. Config.findOneAndUpdate(
  92. { ns: ns, key: key },
  93. { ns: ns, key: key, value: JSON.stringify(value) },
  94. { upsert: true, },
  95. function (err, config) {
  96. debug('Config.findAndUpdate', err, config);
  97. callback(err, config);
  98. });
  99. };
  100. configSchema.statics.getConfig = function(callback)
  101. {
  102. };
  103. configSchema.statics.getConfigArray = function(callback)
  104. {
  105. var Config = this
  106. , config = {};
  107. config.crowi = {}; // crowi namespace
  108. Config.find()
  109. .sort({ns: 1, key: 1})
  110. .exec(function(err, doc) {
  111. doc.forEach(function(el) {
  112. if (!config[el.ns]) {
  113. config[el.ns] = {};
  114. }
  115. config[el.ns][el.key] = JSON.parse(el.value);
  116. });
  117. return callback(null, config);
  118. });
  119. };
  120. Config = mongoose.model('Config', configSchema);
  121. return Config;
  122. };