config.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. // always true if crowi-plus installed cleanly
  207. if (Object.keys(config.crowi).length == 0) {
  208. return true;
  209. }
  210. const key = 'security:isEnabledPassport';
  211. return getValueForCrowiNS(config, key);
  212. };
  213. configSchema.statics.isEnabledPassportLdap = function(config)
  214. {
  215. const key = 'security:passport-ldap:isEnabled';
  216. return getValueForCrowiNS(config, key);
  217. };
  218. configSchema.statics.isUploadable = function(config)
  219. {
  220. var method = crowi.env.FILE_UPLOAD || 'aws';
  221. if (method == 'aws' && (
  222. !config.crowi['aws:accessKeyId'] ||
  223. !config.crowi['aws:secretAccessKey'] ||
  224. !config.crowi['aws:region'] ||
  225. !config.crowi['aws:bucket'])) {
  226. return false;
  227. }
  228. return method != 'none';
  229. };
  230. configSchema.statics.isGuesstAllowedToRead = function(config)
  231. {
  232. // return false if undefined
  233. if (undefined === config.crowi || undefined === config.crowi['security:restrictGuestMode']) {
  234. return false;
  235. }
  236. return SECURITY_RESTRICT_GUEST_MODE_READONLY === config.crowi['security:restrictGuestMode'];
  237. };
  238. configSchema.statics.isEnabledPlugins = function(config)
  239. {
  240. const key = 'plugin:isEnabledPlugins';
  241. return getValueForCrowiNS(config, key);
  242. };
  243. configSchema.statics.isEnabledLinebreaks = function(config)
  244. {
  245. const key = 'markdown:isEnabledLinebreaks';
  246. // return default value if undefined
  247. if (undefined === config.markdown || undefined === config.markdown[key]) {
  248. return getDefaultMarkdownConfigs[key];
  249. }
  250. return config.markdown[key];
  251. };
  252. configSchema.statics.isEnabledLinebreaksInComments = function(config)
  253. {
  254. const key = 'markdown:isEnabledLinebreaksInComments';
  255. // return default value if undefined
  256. if (undefined === config.markdown || undefined === config.markdown[key]) {
  257. return getDefaultMarkdownConfigs[key];
  258. }
  259. return config.markdown[key];
  260. };
  261. /**
  262. * initialize custom css strings
  263. */
  264. configSchema.statics.initCustomCss = function(config)
  265. {
  266. const key = 'customize:css';
  267. const rawCss = getValueForCrowiNS(config, key);
  268. // uglify and store
  269. this._customCss = uglifycss.processString(rawCss);
  270. }
  271. configSchema.statics.customCss = function(config)
  272. {
  273. return this._customCss;
  274. }
  275. configSchema.statics.initCustomScript = function(config)
  276. {
  277. const key = 'customize:script';
  278. const rawScript = getValueForCrowiNS(config, key);
  279. // store as is
  280. this._customScript = rawScript;
  281. }
  282. configSchema.statics.customScript = function(config)
  283. {
  284. return this._customScript;
  285. }
  286. configSchema.statics.behaviorType = function(config)
  287. {
  288. const key = 'customize:behavior';
  289. return getValueForCrowiNS(config, key);
  290. }
  291. configSchema.statics.layoutType = function(config)
  292. {
  293. const key = 'customize:layout';
  294. return getValueForCrowiNS(config, key);
  295. }
  296. configSchema.statics.isEnabledTimeline = function(config)
  297. {
  298. const key = 'customize:isEnabledTimeline';
  299. return getValueForCrowiNS(config, key);
  300. };
  301. configSchema.statics.isSavedStatesOfTabChanges = function(config)
  302. {
  303. const key = 'customize:isSavedStatesOfTabChanges';
  304. return getValueForCrowiNS(config, key);
  305. };
  306. configSchema.statics.fileUploadEnabled = function(config)
  307. {
  308. const Config = this;
  309. if (!Config.isUploadable(config)) {
  310. return false;
  311. }
  312. return config.crowi['app:fileUpload'] || false;
  313. };
  314. configSchema.statics.hasSlackConfig = function(config)
  315. {
  316. return Config.hasSlackWebClientConfig(config) || Config.hasSlackIwhUrl(config);
  317. };
  318. configSchema.statics.hasSlackWebClientConfig = function(config)
  319. {
  320. if (!config.notification) {
  321. return false;
  322. }
  323. if (!config.notification['slack:clientId'] ||
  324. !config.notification['slack:clientSecret']) {
  325. return false;
  326. }
  327. return true;
  328. };
  329. /**
  330. * for Slack Incoming Webhooks
  331. */
  332. configSchema.statics.hasSlackIwhUrl = function(config)
  333. {
  334. if (!config.notification) {
  335. return false;
  336. }
  337. return config.notification['slack:incomingWebhookUrl'];
  338. };
  339. configSchema.statics.isIncomingWebhookPrioritized = function(config)
  340. {
  341. if (!config.notification) {
  342. return false;
  343. }
  344. return config.notification['slack:isIncomingWebhookPrioritized'];
  345. };
  346. configSchema.statics.hasSlackToken = function(config)
  347. {
  348. if (!this.hasSlackWebClientConfig(config)) {
  349. return false;
  350. }
  351. if (!config.notification['slack:token']) {
  352. return false;
  353. }
  354. return true;
  355. };
  356. configSchema.statics.getLocalconfig = function(config)
  357. {
  358. const Config = this;
  359. const env = crowi.getEnv();
  360. const local_config = {
  361. crowi: {
  362. title: config.crowi['app:title'],
  363. url: config.crowi['app:url'] || '',
  364. },
  365. upload: {
  366. image: Config.isUploadable(config),
  367. file: Config.fileUploadEnabled(config),
  368. },
  369. behaviorType: Config.behaviorType(config),
  370. layoutType: Config.layoutType(config),
  371. isSavedStatesOfTabChanges: Config.isSavedStatesOfTabChanges(config),
  372. env: {
  373. PLANTUML_URI: env.PLANTUML_URI || null,
  374. MATHJAX: env.MATHJAX || null,
  375. },
  376. };
  377. return local_config;
  378. }
  379. /*
  380. configSchema.statics.isInstalled = function(config)
  381. {
  382. if (!config.crowi) {
  383. return false;
  384. }
  385. if (config.crowi['app:installed']
  386. && config.crowi['app:installed'] !== '0.0.0') {
  387. return true;
  388. }
  389. return false;
  390. }
  391. */
  392. Config = mongoose.model('Config', configSchema);
  393. Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
  394. Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
  395. Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
  396. return Config;
  397. };