All files / models config.js

54.84% Statements 51/93
34.15% Branches 14/41
45.45% Functions 10/22
56.04% Lines 51/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 23415x 15x 15x 15x         15x 15x 15x                                                                         15x 1x               15x                 15x                       15x                       15x           15x                       15x                       15x 16x   16x   16x       16x 4x 1x     4x     16x     15x 1x     15x 1x     15x 2x   2x       2x           15x 1x   1x 1x           15x       15x       15x 15x 15x                 15x                       15x 1x 1x   1x                                     1x     15x 15x 15x   15x    
module.exports = function(crowi) {
  const mongoose = require('mongoose')
  const debug = require('debug')('crowi:models:config')
  const configSchema = new mongoose.Schema({
    ns: { type: String, required: true, index: true },
    key: { type: String, required: true, index: true },
    value: { type: String, required: true },
  })
  const SECURITY_REGISTRATION_MODE_OPEN = 'Open'
  const SECURITY_REGISTRATION_MODE_RESTRICTED = 'Resricted'
  const SECURITY_REGISTRATION_MODE_CLOSED = 'Closed'
 
  function getArrayForInstalling() {
    return {
      // 'app:installed'     : "0.0.0",
      'app:title': 'Crowi',
      'app:confidential': '',
 
      'app:fileUpload': false,
      'app:externalShare': false,
 
      'security:registrationMode': 'Open',
      'security:registrationWhiteList': [],
 
      'auth:requireThirdPartyAuth': false,
      'auth:disablePasswordAuth': false,
 
      'aws:bucket': 'crowi',
      'aws:region': 'ap-northeast-1',
      'aws:accessKeyId': '',
      'aws:secretAccessKey': '',
 
      'mail:from': '',
      'mail:smtpHost': '',
      'mail:smtpPort': '',
      'mail:smtpUser': '',
      'mail:smtpPassword': '',
 
      'google:clientId': '',
      'google:clientSecret': '',
 
      'github:clientId': '',
      'github:clientSecret': '',
      'github:organization': '',
    }
  }
 
  configSchema.statics.getRegistrationModeLabels = function() {
    return {
      [SECURITY_REGISTRATION_MODE_OPEN]: '公開 (だれでも登録可能)',
      [SECURITY_REGISTRATION_MODE_RESTRICTED]: '制限 (登録完了には管理者の承認が必要)',
      [SECURITY_REGISTRATION_MODE_CLOSED]: '非公開 (登録には管理者による招待が必要)',
    }
  }
 
  // Execute only once for installing application
  configSchema.statics.applicationInstall = async function() {
    const Config = this
    const count = await Config.count({ ns: 'crowi' }).exec()
    if (count > 0) {
      throw new Error('Application already installed')
    }
    await Config.updateConfigByNamespace('crowi', getArrayForInstalling())
  }
 
  configSchema.statics.updateCache = function(ns, key, value) {
    const config = crowi.getConfig()
 
    if (!config[ns]) {
      config[ns] = {}
    }
 
    config[ns][key] = value
 
    crowi.setConfig(config)
  }
 
  configSchema.statics.updateCacheByNamespace = function(ns, nsConfig) {
    const config = crowi.getConfig()
 
    if (!config[ns]) {
      config[ns] = {}
    }
 
    Object.entries(nsConfig).forEach(([key, value]) => (config[ns][key] = value))
 
    crowi.setConfig(config)
  }
 
  configSchema.statics.update = async function(ns, key, value) {
    const Config = this
 
    await Config.findOneAndUpdate({ ns, key }, { ns, key, value: JSON.stringify(value) }, { upsert: true }).exec()
  }
 
  configSchema.statics.updateConfig = async function(ns, key, value) {
    const Config = this
 
    try {
      await Config.update(ns, key, value)
    } catch (err) {
      debug('updateConfig', err)
    }
 
    Config.updateCache(ns, key, value)
  }
 
  configSchema.statics.updateConfigByNamespace = async function(ns, nsConfig) {
    const Config = this
 
    try {
      await Promise.all(Object.entries(nsConfig).map(([key, value]) => Config.update(ns, key, value)))
    } catch (err) {
      debug('updateConfigByNamespace', err)
    }
 
    Config.updateCacheByNamespace(ns, nsConfig)
  }
 
  configSchema.statics.loadAllConfig = async function() {
    const Config = this
 
    const config = { crowi: {} }
 
    const doc = await Config.find()
      .sort({ ns: 1, key: 1 })
      .exec()
 
    doc.forEach(({ ns, key, value }) => {
      if (!config[ns]) {
        config[ns] = {}
      }
 
      config[ns][key] = JSON.parse(value)
    })
 
    return config
  }
 
  configSchema.statics.isRequiredThirdPartyAuth = function(config) {
    return !!config.crowi['auth:requireThirdPartyAuth']
  }
 
  configSchema.statics.isDisabledPasswordAuth = function(config) {
    return !!config.crowi['auth:disablePasswordAuth']
  }
 
  configSchema.statics.isUploadable = function(config) {
    var method = crowi.env.FILE_UPLOAD || 'aws'
 
    Eif (
      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.fileUploadEnabled = function(config) {
    const Config = this
 
    Eif (!Config.isUploadable(config)) {
      return false
    }
 
    return config.crowi['app:fileUpload'] || false
  }
 
  configSchema.statics.googleLoginEnabled = function(config) {
    return config.crowi['google:clientId'] && config.crowi['google:clientSecret']
  }
 
  configSchema.statics.githubLoginEnabled = function(config) {
    return config.crowi['github:clientId'] && config.crowi['github:clientSecret']
  }
 
  configSchema.statics.hasSlackConfig = function(config) {
    Eif (!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.getLocalconfig = function(config) {
    const Config = this
    const env = crowi.getEnv()
 
    const localConfig = {
      crowi: {
        title: config.crowi['app:title'],
        url: config.crowi['app:url'] || '',
        auth: {
          requireThirdPartyAuth: Config.isRequiredThirdPartyAuth(config),
          disablePasswordAuth: Config.isDisabledPasswordAuth(config),
        },
      },
      upload: {
        image: Config.isUploadable(config),
        file: Config.fileUploadEnabled(config),
      },
      env: {
        PLANTUML_URI: env.PLANTUML_URI || null,
        MATHJAX: env.MATHJAX || null,
      },
    }
 
    return localConfig
  }
 
  configSchema.statics.SECURITY_REGISTRATION_MODE_OPEN = SECURITY_REGISTRATION_MODE_OPEN
  configSchema.statics.SECURITY_REGISTRATION_MODE_RESTRICTED = SECURITY_REGISTRATION_MODE_RESTRICTED
  configSchema.statics.SECURITY_REGISTRATION_MODE_CLOSED = SECURITY_REGISTRATION_MODE_CLOSED
 
  return mongoose.model('Config', configSchema)
}