| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- module.exports = function(crowi) {
- var mongoose = require('mongoose')
- , debug = require('debug')('crowi:models:config')
- , ObjectId = mongoose.Schema.Types.ObjectId
- , configSchema
- , Config
- , SECURITY_REGISTRATION_MODE_OPEN = 'Open'
- , SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted'
- , SECURITY_REGISTRATION_MODE_CLOSED = 'Closed'
- ;
- configSchema = new mongoose.Schema({
- ns: { type: String, required: true, index: true },
- key: { type: String, required: true, index: true },
- value: { type: String, required: true }
- });
- function getArrayForInstalling()
- {
- return {
- //'app:installed' : "0.0.0",
- 'app:title' : 'Crowi',
- 'app:confidential' : '',
- 'security:registrationMode' : 'Open',
- 'security:registrationWhiteList' : [],
- 'aws:bucket' : 'crowi',
- 'aws:region' : 'ap-northeast-1',
- 'aws:accessKeyId' : '',
- 'aws:secretAccessKey' : '',
- 'mail:from' : '',
- 'mail:smtpHost' : '',
- 'mail:smtpPort' : '',
- 'mail:smtpUser' : '',
- 'mail:smtpPassword' : '',
- 'searcher:url': '',
- 'google:clientId' : '',
- 'google:clientSecret' : '',
- 'plugin:isEnabledPlugins' : true,
- };
- }
- function getDefaultMarkdownConfigs() {
- return {
- 'markdown:isEnabledLinebreaks': true,
- 'markdown:isEnabledLinebreaksInComments': true,
- }
- }
- configSchema.statics.getRegistrationModeLabels = function()
- {
- var labels = {};
- labels[SECURITY_REGISTRATION_MODE_OPEN] = '公開 (だれでも登録可能)';
- labels[SECURITY_REGISTRATION_MODE_RESTRICTED] = '制限 (登録完了には管理者の承認が必要)';
- labels[SECURITY_REGISTRATION_MODE_CLOSED] = '非公開 (登録には管理者による招待が必要)';
- return labels;
- };
- configSchema.statics.updateConfigCache = function(ns, config)
- {
- var originalConfig = crowi.getConfig();
- var newNSConfig = originalConfig[ns] || {};
- Object.keys(config).forEach(function (key) {
- if (config[key] || config[key] === '' || config[key] === false) {
- newNSConfig[key] = config[key];
- }
- });
- originalConfig[ns] = newNSConfig;
- crowi.setConfig(originalConfig);
- };
- // Execute only once for installing application
- configSchema.statics.applicationInstall = function(callback)
- {
- var Config = this;
- Config.count({ ns: 'crowi' }, function (err, count) {
- if (count > 0) {
- return callback(new Error('Application already installed'), null);
- }
- Config.updateNamespaceByArray('crowi', getArrayForInstalling(), function(err, configs) {
- Config.updateConfigCache('crowi', configs);
- return callback(err, configs);
- });
- });
- };
- configSchema.statics.setupCofigFormData = function(ns, config)
- {
- var defaultConfig = {};
- // set Default Settings
- if (ns === 'crowi') {
- defaultConfig = getArrayForInstalling();
- }
- else if (ns === 'markdown') {
- defaultConfig = getDefaultMarkdownConfigs();
- }
- if (!defaultConfig[ns]) {
- defaultConfig[ns] = {};
- }
- Object.keys(config[ns] || {}).forEach(function (key) {
- if (config[ns][key] !== undefined) {
- defaultConfig[key] = config[ns][key];
- }
- });
- return defaultConfig;
- };
- configSchema.statics.updateNamespaceByArray = function(ns, configs, callback)
- {
- var Config = this;
- if (configs.length < 0) {
- return callback(new Error('Argument #1 is not array.'), null);
- }
- Object.keys(configs).forEach(function (key) {
- var value = configs[key];
- Config.findOneAndUpdate(
- { ns: ns, key: key },
- { ns: ns, key: key, value: JSON.stringify(value) },
- { upsert: true, },
- function (err, config) {
- debug('Config.findAndUpdate', err, config);
- });
- });
- return callback(null, configs);
- };
- configSchema.statics.findAndUpdate = function(ns, key, value, callback)
- {
- var Config = this;
- Config.findOneAndUpdate(
- { ns: ns, key: key },
- { ns: ns, key: key, value: JSON.stringify(value) },
- { upsert: true, },
- function (err, config) {
- debug('Config.findAndUpdate', err, config);
- callback(err, config);
- });
- };
- configSchema.statics.getConfig = function(callback)
- {
- };
- configSchema.statics.loadAllConfig = function(callback)
- {
- var Config = this
- , config = {};
- config.crowi = {}; // crowi namespace
- Config.find()
- .sort({ns: 1, key: 1})
- .exec(function(err, doc) {
- doc.forEach(function(el) {
- if (!config[el.ns]) {
- config[el.ns] = {};
- }
- config[el.ns][el.key] = JSON.parse(el.value);
- });
- debug('Config loaded', config);
- return callback(null, config);
- });
- };
- configSchema.statics.isUploadable = function(config)
- {
- var method = crowi.env.FILE_UPLOAD || 'aws';
- if (method == 'aws' && (
- !config.crowi['aws:accessKeyId'] ||
- !config.crowi['aws:secretAccessKey'] ||
- !config.crowi['aws:region'] ||
- !config.crowi['aws:bucket'])) {
- return false;
- }
- return method != 'none';
- };
- configSchema.statics.isEnabledLinebreaks = function(config)
- {
- var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaks'];
- if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaks']) {
- return defaultValue;
- }
- return config.markdown['markdown:isEnabledLinebreaks'];
- };
- configSchema.statics.isEnabledLinebreaksInComments = function(config)
- {
- var defaultValue = getDefaultMarkdownConfigs()['markdown:isEnabledLinebreaksInComments'];
- if (undefined === config.markdown || undefined === config.markdown['markdown:isEnabledLinebreaksInComments']) {
- return defaultValue;
- }
- return config.markdown['markdown:isEnabledLinebreaksInComments'];
- };
- configSchema.statics.hasSlackConfig = function(config)
- {
- if (!config.notification) {
- return false;
- }
- if (!config.notification['slack:clientId'] ||
- !config.notification['slack:clientSecret']) {
- return false;
- }
- return true;
- };
- configSchema.statics.hasSlackToken = function(config)
- {
- if (!this.hasSlackConfig(config)) {
- return false;
- }
- if (!config.notification['slack:token']) {
- return false;
- }
- return true;
- };
- /*
- configSchema.statics.isInstalled = function(config)
- {
- if (!config.crowi) {
- return false;
- }
- if (config.crowi['app:installed']
- && config.crowi['app:installed'] !== '0.0.0') {
- return true;
- }
- return false;
- }
- */
- Config = mongoose.model('Config', configSchema);
- Config.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN;
- Config.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED;
- Config.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED;
- return Config;
- };
|