2
0

config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. return callback(err, configs);
  53. });
  54. });
  55. };
  56. configSchema.statics.setupCofigFormData = function(ns, config)
  57. {
  58. var defaultConfig;
  59. if (ns === 'crowi') {
  60. defaultConfig = getArrayForInstalling();
  61. }
  62. Object.keys(config[ns]).forEach(function (key) {
  63. if (config[ns][key]) {
  64. defaultConfig[key] = config[ns][key];
  65. }
  66. });
  67. return defaultConfig;
  68. };
  69. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  70. {
  71. var Config = this;
  72. if (configs.length < 0) {
  73. return callback(new Error('Argument #1 is not array.'), null);
  74. }
  75. Object.keys(configs).forEach(function (key) {
  76. var value = configs[key];
  77. Config.findOneAndUpdate(
  78. { ns: ns, key: key },
  79. { ns: ns, key: key, value: JSON.stringify(value) },
  80. { upsert: true, },
  81. function (err, config) {
  82. debug('Config.findAndUpdate', err, config);
  83. });
  84. });
  85. return callback(null, configs);
  86. };
  87. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  88. {
  89. var Config = this;
  90. Config.findOneAndUpdate(
  91. { ns: ns, key: key },
  92. { ns: ns, key: key, value: JSON.stringify(value) },
  93. { upsert: true, },
  94. function (err, config) {
  95. debug('Config.findAndUpdate', err, config);
  96. callback(err, config);
  97. });
  98. };
  99. configSchema.statics.getConfig = function(callback)
  100. {
  101. };
  102. configSchema.statics.getConfigArray = function(callback)
  103. {
  104. var Config = this
  105. , config = {};
  106. config.crowi = {}; // crowi namespace
  107. Config.find()
  108. .sort({ns: 1, key: 1})
  109. .exec(function(err, doc) {
  110. doc.forEach(function(el) {
  111. if (!config[el.ns]) {
  112. config[el.ns] = {};
  113. }
  114. config[el.ns][el.key] = JSON.parse(el.value);
  115. });
  116. return callback(null, config);
  117. });
  118. };
  119. Config = mongoose.model('Config', configSchema);
  120. return Config;
  121. };