config.js 12 KB

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