config.js 9.7 KB

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