config.js 6.5 KB

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