config.js 13 KB

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