2
0

config.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. /**
  20. * default values when crowi-plus is cleanly installed
  21. */
  22. function getArrayForInstalling() {
  23. let config = getDefaultCrowiConfigs();
  24. // overwrite
  25. config['app:title'] = 'crowi-plus';
  26. config['security:isEnabledPassport'] = true;
  27. config['customize:behavior'] = 'crowi-plus';
  28. config['customize:layout'] = 'crowi-plus';
  29. config['customize:isSavedStatesOfTabChanges'] = false;
  30. return config;
  31. }
  32. /**
  33. * default values when migrated from Official Crowi
  34. */
  35. function getDefaultCrowiConfigs()
  36. {
  37. return {
  38. //'app:installed' : "0.0.0",
  39. 'app:title' : 'Crowi',
  40. 'app:confidential' : '',
  41. 'app:fileUpload' : false,
  42. 'security:restrictGuestMode' : 'Deny',
  43. 'security:registrationMode' : 'Open',
  44. 'security:registrationWhiteList' : [],
  45. 'security:isEnabledPassport' : false,
  46. 'security:passport-ldap:isEnabled' : false,
  47. 'aws:bucket' : 'crowi',
  48. 'aws:region' : 'ap-northeast-1',
  49. 'aws:accessKeyId' : '',
  50. 'aws:secretAccessKey' : '',
  51. 'mail:from' : '',
  52. 'mail:smtpHost' : '',
  53. 'mail:smtpPort' : '',
  54. 'mail:smtpUser' : '',
  55. 'mail:smtpPassword' : '',
  56. 'google:clientId' : '',
  57. 'google:clientSecret' : '',
  58. 'plugin:isEnabledPlugins' : true,
  59. 'customize:css' : '',
  60. 'customize:script' : '',
  61. 'customize:behavior' : 'crowi',
  62. 'customize:layout' : 'crowi',
  63. 'customize:isEnabledTimeline' : true,
  64. 'customize:isSavedStatesOfTabChanges' : true,
  65. };
  66. }
  67. function getDefaultMarkdownConfigs() {
  68. return {
  69. 'markdown:isEnabledLinebreaks': true,
  70. 'markdown:isEnabledLinebreaksInComments': true,
  71. }
  72. }
  73. function getValueForCrowiNS(config, key) {
  74. // return the default value if undefined
  75. if (undefined === config.crowi || undefined === config.crowi[key]) {
  76. return getDefaultCrowiConfigs()[key];
  77. }
  78. return config.crowi[key];
  79. }
  80. configSchema.statics.getRestrictGuestModeLabels = function()
  81. {
  82. var labels = {};
  83. labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'アカウントを持たないユーザーはアクセス不可';
  84. labels[SECURITY_RESTRICT_GUEST_MODE_READONLY] = '閲覧のみ許可';
  85. return labels;
  86. };
  87. configSchema.statics.getRegistrationModeLabels = function()
  88. {
  89. var labels = {};
  90. labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
  91. labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
  92. labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
  93. return labels;
  94. };
  95. configSchema.statics.updateConfigCache = function(ns, config)
  96. {
  97. var originalConfig = crowi.getConfig();
  98. var newNSConfig = originalConfig[ns] || {};
  99. Object.keys(config).forEach(function (key) {
  100. if (config[key] || config[key] === '' || config[key] === false) {
  101. newNSConfig[key] = config[key];
  102. }
  103. });
  104. originalConfig[ns] = newNSConfig;
  105. crowi.setConfig(originalConfig);
  106. // initialize custom css/script
  107. Config.initCustomCss(originalConfig);
  108. Config.initCustomScript(originalConfig);
  109. };
  110. // Execute only once for installing application
  111. configSchema.statics.applicationInstall = function(callback)
  112. {
  113. var Config = this;
  114. Config.count({ ns: 'crowi' }, function (err, count) {
  115. if (count > 0) {
  116. return callback(new Error('Application already installed'), null);
  117. }
  118. Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
  119. Config.updateConfigCache('crowi', configs);
  120. return callback(err, configs);
  121. });
  122. });
  123. };
  124. configSchema.statics.setupCofigFormData = function(ns, config)
  125. {
  126. var defaultConfig = {};
  127. // set Default Settings
  128. if (ns === 'crowi') {
  129. defaultConfig = getDefaultCrowiConfigs();
  130. }
  131. else if (ns === 'markdown') {
  132. defaultConfig = getDefaultMarkdownConfigs();
  133. }
  134. if (!defaultConfig[ns]) {
  135. defaultConfig[ns] = {};
  136. }
  137. Object.keys(config[ns] || {}).forEach(function (key) {
  138. if (config[ns][key] !== undefined) {
  139. defaultConfig[key] = config[ns][key];
  140. }
  141. });
  142. return defaultConfig;
  143. };
  144. configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
  145. {
  146. var Config = this;
  147. if (configs.length < 0) {
  148. return callback(new Error('Argument #1 is not array.'), null);
  149. }
  150. Object.keys(configs).forEach(function (key) {
  151. var value = configs[key];
  152. Config.findOneAndUpdate(
  153. { ns: ns, key: key },
  154. { ns: ns, key: key, value: JSON.stringify(value) },
  155. { upsert: true, },
  156. function (err, config) {
  157. debug('Config.findAndUpdate', err, config);
  158. });
  159. });
  160. return callback(null, configs);
  161. };
  162. configSchema.statics.findAndUpdate = function(ns, key, value, callback)
  163. {
  164. var Config = this;
  165. Config.findOneAndUpdate(
  166. { ns: ns, key: key },
  167. { ns: ns, key: key, value: JSON.stringify(value) },
  168. { upsert: true, },
  169. function (err, config) {
  170. debug('Config.findAndUpdate', err, config);
  171. callback(err, config);
  172. });
  173. };
  174. configSchema.statics.getConfig = function(callback)
  175. {
  176. };
  177. configSchema.statics.loadAllConfig = function(callback)
  178. {
  179. var Config = this
  180. , config = {};
  181. config.crowi = {}; // crowi namespace
  182. Config.find()
  183. .sort({ns: 1, key: 1})
  184. .exec(function(err, doc) {
  185. doc.forEach(function(el) {
  186. if (!config[el.ns]) {
  187. config[el.ns] = {};
  188. }
  189. config[el.ns][el.key] = JSON.parse(el.value);
  190. });
  191. debug('Config loaded', config);
  192. // initialize custom css/script
  193. Config.initCustomCss(config);
  194. Config.initCustomScript(config);
  195. return callback(null, config);
  196. });
  197. };
  198. configSchema.statics.isEnabledPassport = function(config)
  199. {
  200. const key = 'security:isEnabledPassport';
  201. return getValueForCrowiNS(config, key);
  202. };
  203. configSchema.statics.isEnabledLdap = function(config)
  204. {
  205. // TODO impl
  206. return false;
  207. };
  208. configSchema.statics.isUploadable = function(config)
  209. {
  210. var method = crowi.env.FILE_UPLOAD || 'aws';
  211. if (method == 'aws' && (
  212. !config.crowi['aws:accessKeyId'] ||
  213. !config.crowi['aws:secretAccessKey'] ||
  214. !config.crowi['aws:region'] ||
  215. !config.crowi['aws:bucket'])) {
  216. return false;
  217. }
  218. return method != 'none';
  219. };
  220. configSchema.statics.isGuesstAllowedToRead = function(config)
  221. {
  222. // return false if undefined
  223. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  224. return false;
  225. }
  226. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  227. };
  228. configSchema.statics.isEnabledPlugins = function(config)
  229. {
  230. const key = 'plugin:isEnabledPlugins';
  231. return getValueForCrowiNS(config, key);
  232. };
  233. configSchema.statics.isEnabledLinebreaks = function(config)
  234. {
  235. const key = 'markdown:isEnabledLinebreaks';
  236. // return default value if undefined
  237. if (undefined === config.markdown || undefined === config.markdown[key]) {
  238. return getDefaultMarkdownConfigs[key];
  239. }
  240. return config.markdown[key];
  241. };
  242. configSchema.statics.isEnabledLinebreaksInComments = function(config)
  243. {
  244. const key = 'markdown:isEnabledLinebreaksInComments';
  245. // return default value if undefined
  246. if (undefined === config.markdown || undefined === config.markdown[key]) {
  247. return getDefaultMarkdownConfigs[key];
  248. }
  249. return config.markdown[key];
  250. };
  251. /**
  252. * initialize custom css strings
  253. */
  254. configSchema.statics.initCustomCss = function(config)
  255. {
  256. const key = 'customize:css';
  257. const rawCss = getValueForCrowiNS(config, key);
  258. // uglify and store
  259. this._customCss = uglifycss.processString(rawCss);
  260. }
  261. configSchema.statics.customCss = function(config)
  262. {
  263. return this._customCss;
  264. }
  265. configSchema.statics.initCustomScript = function(config)
  266. {
  267. const key = 'customize:script';
  268. const rawScript = getValueForCrowiNS(config, key);
  269. // store as is
  270. this._customScript = rawScript;
  271. }
  272. configSchema.statics.customScript = function(config)
  273. {
  274. return this._customScript;
  275. }
  276. configSchema.statics.behaviorType = function(config)
  277. {
  278. const key = 'customize:behavior';
  279. return getValueForCrowiNS(config, key);
  280. }
  281. configSchema.statics.layoutType = function(config)
  282. {
  283. const key = 'customize:layout';
  284. return getValueForCrowiNS(config, key);
  285. }
  286. configSchema.statics.isEnabledTimeline = function(config)
  287. {
  288. const key = 'customize:isEnabledTimeline';
  289. return getValueForCrowiNS(config, key);
  290. };
  291. configSchema.statics.isSavedStatesOfTabChanges = function(config)
  292. {
  293. const key = 'customize:isSavedStatesOfTabChanges';
  294. return getValueForCrowiNS(config, key);
  295. };
  296. configSchema.statics.fileUploadEnabled = function(config)
  297. {
  298. const Config = this;
  299. if (!Config.isUploadable(config)) {
  300. return false;
  301. }
  302. return config.crowi['app:fileUpload'] || false;
  303. };
  304. configSchema.statics.hasSlackConfig = function(config)
  305. {
  306. return Config.hasSlackAppConfig(config) || Config.hasSlackIwhUrl(config);
  307. };
  308. configSchema.statics.hasSlackAppConfig = function(config)
  309. {
  310. if (!config.notification) {
  311. return false;
  312. }
  313. if (!config.notification['slack:clientId'] ||
  314. !config.notification['slack:clientSecret']) {
  315. return false;
  316. }
  317. return true;
  318. };
  319. /**
  320. * for Slack Incoming Webhooks
  321. */
  322. configSchema.statics.hasSlackIwhUrl = function(config)
  323. {
  324. if (!config.notification) {
  325. return false;
  326. }
  327. return config.notification['slack:incomingWebhookUrl'];
  328. };
  329. configSchema.statics.isIncomingWebhookPrioritized = function(config)
  330. {
  331. if (!config.notification) {
  332. return false;
  333. }
  334. return config.notification['slack:isIncomingWebhookPrioritized'];
  335. };
  336. configSchema.statics.hasSlackToken = function(config)
  337. {
  338. if (!this.hasSlackAppConfig(config)) {
  339. return false;
  340. }
  341. if (!config.notification['slack:token']) {
  342. return false;
  343. }
  344. return true;
  345. };
  346. configSchema.statics.getLocalconfig = function(config)
  347. {
  348. const Config = this;
  349. const env = crowi.getEnv();
  350. const local_config = {
  351. crowi: {
  352. title: config.crowi['app:title'],
  353. url: config.crowi['app:url'] || '',
  354. },
  355. upload: {
  356. image: Config.isUploadable(config),
  357. file: Config.fileUploadEnabled(config),
  358. },
  359. behaviorType: Config.behaviorType(config),
  360. layoutType: Config.layoutType(config),
  361. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  362. env: {
  363. PLANTUML_URI: env.PLANTUML_URI || null,
  364. MATHJAX: env.MATHJAX || null,
  365. },
  366. };
  367. return local_config;
  368. }
  369. /*
  370. configSchema.statics.isInstalled = function(config)
  371. {
  372. if (!config.crowi) {
  373. return false;
  374. }
  375. if (config.crowi['app:installed']
  376. && config.crowi['app:installed'] !== '0.0.0') {
  377. return true;
  378. }
  379. return false;
  380. }
  381. */
  382. Config = mongoose.model('Config', configSchema);
  383. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  384. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  385. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  386. return Config;
  387. };