config.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. module.exports = function(crowi) {
  2. var mongoose = require('mongoose')
  3. , debug = require('debug')('crowi:models:config')
  4. , uglifycss = require('uglifycss')
  5. , ObjectId = mongoose.Schema.Types.ObjectId
  6. , configSchema
  7. , Config
  8. , SECURITY_RESTRICT_GUEST_MODE_DENY = 'Deny'
  9. , SECURITY_RESTRICT_GUEST_MODE_READONLY = 'Readonly'
  10. , SECURITY_REGISTRATION_MODE_OPEN = 'Open'
  11. , SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted'
  12. , SECURITY_REGISTRATION_MODE_CLOSED = 'Closed'
  13. ;
  14. configSchema = new mongoose.Schema({
  15. ns: { type: String, required: true, index: true },
  16. key: { type: String, required: true, index: true },
  17. value: { type: String, required: true }
  18. });
  19. function getArrayForInstalling()
  20. {
  21. return {
  22. //'app:installed' : "0.0.0",
  23. 'app:title' : 'Crowi',
  24. 'app:confidential' : '',
  25. 'app:fileUpload' : false,
  26. 'security:restrictGuestMode' : 'Deny',
  27. 'security:registrationMode' : 'Open',
  28. 'security:registrationWhiteList' : [],
  29. 'aws:bucket' : 'crowi',
  30. 'aws:region' : 'ap-northeast-1',
  31. 'aws:accessKeyId' : '',
  32. 'aws:secretAccessKey' : '',
  33. 'mail:from' : '',
  34. 'mail:smtpHost' : '',
  35. 'mail:smtpPort' : '',
  36. 'mail:smtpUser' : '',
  37. 'mail:smtpPassword' : '',
  38. 'google:clientId' : '',
  39. 'google:clientSecret' : '',
  40. 'plugin:isEnabledPlugins' : true,
  41. 'customize:css' : '',
  42. 'customize:behavior' : 'crowi',
  43. 'customize:layout' : 'crowi',
  44. };
  45. }
  46. function getDefaultMarkdownConfigs() {
  47. return {
  48. 'markdown:isEnabledLinebreaks': true,
  49. 'markdown:isEnabledLinebreaksInComments': true,
  50. }
  51. }
  52. configSchema.statics.getRestrictGuestModeLabels = function()
  53. {
  54. var labels = {};
  55. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'アカウントを持たないユーザーはアクセス不可';
  56. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = '閲覧のみ許可';
  57. return labels;
  58. };
  59. configSchema.statics.getRegistrationModeLabels = function()
  60. {
  61. var labels = {};
  62. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  63. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  64. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  65. return labels;
  66. };
  67. configSchema.statics.updateConfigCache = function(ns, config)
  68. {
  69. var originalConfig = crowi.getConfig();
  70. var newNSConfig = originalConfig[ns] || {};
  71. Object.keys(config).forEach(function (key) {
  72. if (config[key] || config[key] === '' || config[key] === false) {
  73. newNSConfig[key] = config[key];
  74. }
  75. });
  76. originalConfig[ns] = newNSConfig;
  77. crowi.setConfig(originalConfig);
  78. // uglify and store
  79. Config.generateUglifiedCustomCss(originalConfig);
  80. };
  81. // Execute only once for installing application
  82. configSchema.statics.applicationInstall = function(callback)
  83. {
  84. var Config = this;
  85. Config.count({ ns: 'crowi' }, function (err, count) {
  86. if (count > 0) {
  87. return callback(new Error('Application already installed'), null);
  88. }
  89. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  90. Config.updateConfigCache('crowi', configs);
  91. return callback(err, configs);
  92. });
  93. });
  94. };
  95. configSchema.statics.setupCofigFormData = function(ns, config)
  96. {
  97. var defaultConfig = {};
  98. // set Default Settings
  99. if (ns === 'crowi') {
  100. defaultConfig = getArrayForInstalling();
  101. }
  102. else if (ns === 'markdown') {
  103. defaultConfig = getDefaultMarkdownConfigs();
  104. }
  105. if (!defaultConfig[ns]) {
  106. defaultConfig[ns] = {};
  107. }
  108. Object.keys(config[ns] || {}).forEach(function (key) {
  109. if (config[ns][key] !== undefined) {
  110. defaultConfig[key] = config[ns][key];
  111. }
  112. });
  113. return defaultConfig;
  114. };
  115. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  116. {
  117. var Config = this;
  118. if (configs.length < 0) {
  119. return callback(new Error('Argument #1 is not array.'), null);
  120. }
  121. Object.keys(configs).forEach(function (key) {
  122. var value = configs[key];
  123. Config.findOneAndUpdate(
  124. { ns: ns, key: key },
  125. { ns: ns, key: key, value: JSON.stringify(value) },
  126. { upsert: true, },
  127. function (err, config) {
  128. debug('Config.findAndUpdate', err, config);
  129. });
  130. });
  131. return callback(null, configs);
  132. };
  133. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  134. {
  135. var Config = this;
  136. Config.findOneAndUpdate(
  137. { ns: ns, key: key },
  138. { ns: ns, key: key, value: JSON.stringify(value) },
  139. { upsert: true, },
  140. function (err, config) {
  141. debug('Config.findAndUpdate', err, config);
  142. callback(err, config);
  143. });
  144. };
  145. configSchema.statics.getConfig = function(callback)
  146. {
  147. };
  148. configSchema.statics.loadAllConfig = function(callback)
  149. {
  150. var Config = this
  151. , config = {};
  152. config.crowi = {}; // crowi namespace
  153. Config.find()
  154. .sort({ns: 1, key: 1})
  155. .exec(function(err, doc) {
  156. doc.forEach(function(el) {
  157. if (!config[el.ns]) {
  158. config[el.ns] = {};
  159. }
  160. config[el.ns][el.key] = JSON.parse(el.value);
  161. });
  162. debug('Config loaded', config);
  163. // uglify and store
  164. Config.generateUglifiedCustomCss(config);
  165. return callback(null, config);
  166. });
  167. };
  168. configSchema.statics.isUploadable = function(config)
  169. {
  170. var method = crowi.env.FILE_UPLOAD || 'aws';
  171. if (method == 'aws' && (
  172. !config.crowi['aws:accessKeyId'] ||
  173. !config.crowi['aws:secretAccessKey'] ||
  174. !config.crowi['aws:region'] ||
  175. !config.crowi['aws:bucket'])) {
  176. return false;
  177. }
  178. return method != 'none';
  179. };
  180. configSchema.statics.isGuesstAllowedToRead = function(config)
  181. {
  182. // return false if undefined
  183. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  184. return false;
  185. }
  186. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  187. };
  188. configSchema.statics.isEnabledPlugins = function(config)
  189. {
  190. var defaultValue = getArrayForInstalling()['plugin:isEnabledPlugins'];
  191. // return defaultValue if undefined
  192. if (undefined === config.crowi || undefined === config.crowi['plugin:isEnabledPlugins']) {
  193. return defaultValue;
  194. }
  195. return config.crowi['plugin:isEnabledPlugins'];
  196. };
  197. configSchema.statics.isEnabledLinebreaks = function(config)
  198. {
  199. var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaks'];
  200. // return defaultValue if undefined
  201. if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaks']) {
  202. return defaultValue;
  203. }
  204. return config.markdown['markdown:isEnabledLinebreaks'];
  205. };
  206. configSchema.statics.isEnabledLinebreaksInComments = function(config)
  207. {
  208. var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaksInComments'];
  209. // return defaultValue if undefined
  210. if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaksInComments']) {
  211. return defaultValue;
  212. }
  213. return config.markdown['markdown:isEnabledLinebreaksInComments'];
  214. };
  215. /**
  216. * uglify store custom css strings
  217. */
  218. configSchema.statics.generateUglifiedCustomCss = function(config)
  219. {
  220. var rawCss = config.crowi['customize:css'] || getArrayForInstalling()['customize:css'];
  221. this.uglifiedCustomCss = uglifycss.processString(rawCss);
  222. }
  223. configSchema.statics.customCss = function(config)
  224. {
  225. return this.uglifiedCustomCss;
  226. }
  227. configSchema.statics.behaviorType = function(config)
  228. {
  229. return config.crowi['customize:behavior'] || 'crowi';
  230. }
  231. configSchema.statics.layoutType = function(config)
  232. {
  233. return config.crowi['customize:layout'] || 'crowi';
  234. }
  235. configSchema.statics.fileUploadEnabled = function(config)
  236. {
  237. const Config = this;
  238. if (!Config.isUploadable(config)) {
  239. return false;
  240. }
  241. return config.crowi['app:fileUpload'] || false;
  242. };
  243. configSchema.statics.hasSlackConfig = function(config)
  244. {
  245. if (!config.notification) {
  246. return false;
  247. }
  248. if (!config.notification['slack:clientId'] ||
  249. !config.notification['slack:clientSecret']) {
  250. return false;
  251. }
  252. return true;
  253. };
  254. configSchema.statics.hasSlackToken = function(config)
  255. {
  256. if (!this.hasSlackConfig(config)) {
  257. return false;
  258. }
  259. if (!config.notification['slack:token']) {
  260. return false;
  261. }
  262. return true;
  263. };
  264. configSchema.statics.getLocalconfig = function(config)
  265. {
  266. const Config = this;
  267. const env = crowi.getEnv();
  268. const local_config = {
  269. crowi: {
  270. title: config.crowi['app:title'],
  271. url: config.crowi['app:url'] || '',
  272. },
  273. upload: {
  274. image: Config.isUploadable(config),
  275. file: Config.fileUploadEnabled(config),
  276. },
  277. layoutType: Config.layoutType(config),
  278. env: {
  279. PLANTUML_URI: env.PLANTUML_URI || null,
  280. MATHJAX: env.MATHJAX || null,
  281. },
  282. };
  283. return local_config;
  284. }
  285. /*
  286. configSchema.statics.isInstalled = function(config)
  287. {
  288. if (!config.crowi) {
  289. return false;
  290. }
  291. if (config.crowi['app:installed']
  292. && config.crowi['app:installed'] !== '0.0.0') {
  293. return true;
  294. }
  295. return false;
  296. }
  297. */
  298. Config = mongoose.model('Config', configSchema);
  299. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  300. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  301. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  302. return Config;
  303. };