refactor: remove config namespaces

This commit is contained in:
NGPixel
2018-05-28 14:46:55 -04:00
parent b1499d1d64
commit 416755f17a
27 changed files with 556 additions and 283 deletions

View File

@@ -0,0 +1,64 @@
const Model = require('objection').Model
const autoload = require('auto-load')
const path = require('path')
const _ = require('lodash')
/* global WIKI */
/**
* Authentication model
*/
module.exports = class Authentication extends Model {
static get tableName() { return 'authentication' }
static get jsonSchema () {
return {
type: 'object',
required: ['key', 'title', 'isEnabled', 'useForm'],
properties: {
id: {type: 'integer'},
key: {type: 'string'},
title: {type: 'string'},
isEnabled: {type: 'boolean'},
useForm: {type: 'boolean'},
config: {type: 'object'}
}
}
}
static async getEnabledStrategies() {
return WIKI.db.authentication.query().where({ isEnabled: true })
}
static async refreshStrategiesFromDisk() {
try {
const dbStrategies = await WIKI.db.authentication.query()
const diskStrategies = autoload(path.join(WIKI.SERVERPATH, 'modules/authentication'))
let newStrategies = []
_.forOwn(diskStrategies, (strategy, strategyKey) => {
if (!_.some(dbStrategies, ['key', strategy.key])) {
newStrategies.push({
key: strategy.key,
title: strategy.title,
isEnabled: false,
useForm: strategy.useForm,
config: _.reduce(strategy.props, (result, value, key) => {
_.set(result, value, '')
return result
}, {})
})
}
})
if (newStrategies.length > 0) {
await WIKI.db.authentication.query().insert(newStrategies)
WIKI.logger.info(`Loaded ${newStrategies.length} new authentication strategies: [ OK ]`)
} else {
WIKI.logger.info(`No new authentication strategies found: [ SKIPPED ]`)
}
} catch (err) {
WIKI.logger.error(`Failed to scan or load new authentication providers: [ FAILED ]`)
WIKI.logger.error(err)
}
}
}

View File

@@ -0,0 +1,62 @@
const Model = require('objection').Model
const autoload = require('auto-load')
const path = require('path')
const _ = require('lodash')
/* global WIKI */
/**
* Editor model
*/
module.exports = class Editor extends Model {
static get tableName() { return 'editors' }
static get jsonSchema () {
return {
type: 'object',
required: ['key', 'title', 'isEnabled'],
properties: {
id: {type: 'integer'},
key: {type: 'string'},
title: {type: 'string'},
isEnabled: {type: 'boolean'},
config: {type: 'object'}
}
}
}
static async getEnabledEditors() {
return WIKI.db.editors.query().where({ isEnabled: true })
}
static async refreshEditorsFromDisk() {
try {
const dbEditors = await WIKI.db.editors.query()
const diskEditors = autoload(path.join(WIKI.SERVERPATH, 'modules/editor'))
let newEditors = []
_.forOwn(diskEditors, (strategy, strategyKey) => {
if (!_.some(dbEditors, ['key', strategy.key])) {
newEditors.push({
key: strategy.key,
title: strategy.title,
isEnabled: false,
config: _.reduce(strategy.props, (result, value, key) => {
_.set(result, value, '')
return result
}, {})
})
}
})
if (newEditors.length > 0) {
await WIKI.db.editors.query().insert(newEditors)
WIKI.logger.info(`Loaded ${newEditors.length} new editors: [ OK ]`)
} else {
WIKI.logger.info(`No new editors found: [ SKIPPED ]`)
}
} catch (err) {
WIKI.logger.error(`Failed to scan or load new editors: [ FAILED ]`)
WIKI.logger.error(err)
}
}
}

View File

@@ -1,9 +1,12 @@
const Model = require('objection').Model
const _ = require('lodash')
/* global WIKI */
/**
* Settings model
*/
module.exports = class User extends Model {
module.exports = class Setting extends Model {
static get tableName() { return 'settings' }
static get jsonSchema () {
@@ -25,7 +28,18 @@ module.exports = class User extends Model {
this.updatedAt = new Date().toISOString()
}
$beforeInsert() {
this.createdAt = new Date().toISOString()
this.updatedAt = new Date().toISOString()
}
static async getConfig() {
const settings = await WIKI.db.settings.query()
if (settings.length > 0) {
return _.reduce(settings, (res, val, key) => {
_.set(res, val.key, (val.value.v) ? val.value.v : val.value)
return res
}, {})
} else {
return false
}
}
}

View File

@@ -30,6 +30,9 @@ module.exports = class User extends Model {
tfaIsActive: {type: 'boolean', default: false},
tfaSecret: {type: 'string'},
locale: {type: 'string'},
jobTitle: {type: 'string'},
location: {type: 'string'},
pictureUrl: {type: 'string'},
createdAt: {type: 'string'},
updatedAt: {type: 'string'}
}