Making some changes to how configs are scoped.

This commit is contained in:
mike12345567 2021-05-06 10:51:21 +01:00
parent b3292e991a
commit 8c1f274fbb
5 changed files with 18 additions and 12 deletions

View File

@ -123,7 +123,7 @@ const getConfigParams = ({ type, group, user }, otherProps = {}) => {
* @param {Object} scopes - the type, group and userID scopes of the configuration.
* @returns The most granular configuration document based on the scope.
*/
const determineScopedConfig = async function (db, { type, user, group }) {
const getScopedFullConfig = async function (db, { type, user, group }) {
const response = await db.allDocs(
getConfigParams(
{ type, user, group },
@ -160,6 +160,12 @@ const determineScopedConfig = async function (db, { type, user, group }) {
return scopedConfig.doc
}
async function getScopedConfig(db, params) {
const configDoc = await getScopedFullConfig(db, params)
return configDoc && configDoc.config ? configDoc.config : configDoc
}
exports.getScopedConfig = getScopedConfig
exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams
exports.determineScopedConfig = determineScopedConfig
exports.getScopedFullConfig = getScopedFullConfig

View File

@ -93,7 +93,7 @@ exports.logout = async ctx => {
*/
exports.googlePreAuth = async (ctx, next) => {
const db = new CouchDB(GLOBAL_DB)
const config = await authPkg.db.determineScopedConfig(db, {
const config = await authPkg.db.getScopedConfig(db, {
type: Configs.GOOGLE,
group: ctx.query.group,
})
@ -107,7 +107,7 @@ exports.googlePreAuth = async (ctx, next) => {
exports.googleAuth = async (ctx, next) => {
const db = new CouchDB(GLOBAL_DB)
const config = await authPkg.db.determineScopedConfig(db, {
const config = await authPkg.db.getScopedConfig(db, {
type: Configs.GOOGLE,
group: ctx.query.group,
})

View File

@ -3,7 +3,7 @@ const {
generateConfigID,
StaticDatabases,
getConfigParams,
determineScopedConfig,
getScopedFullConfig,
} = require("@budibase/auth").db
const { Configs } = require("../../../constants")
const email = require("../../../utilities/email")
@ -16,7 +16,7 @@ exports.save = async function (ctx) {
// Config does not exist yet
if (!ctx.request.body._id) {
config._id = generateConfigID({
ctx.request.body._id = generateConfigID({
type,
group,
user,
@ -31,7 +31,7 @@ exports.save = async function (ctx) {
}
try {
const response = await db.put(config)
const response = await db.put(ctx.request.body)
ctx.body = {
type,
_id: response.id,
@ -73,7 +73,7 @@ exports.find = async function (ctx) {
try {
// Find the config with the most granular scope based on context
const scopedConfig = await determineScopedConfig(db, {
const scopedConfig = await getScopedFullConfig(db, {
type: ctx.params.type,
user: userId,
group: groupId,

View File

@ -1,6 +1,6 @@
const nodemailer = require("nodemailer")
const CouchDB = require("../db")
const { StaticDatabases, determineScopedConfig } = require("@budibase/auth").db
const { StaticDatabases, getScopedConfig } = require("@budibase/auth").db
const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../constants")
const { getTemplateByPurpose } = require("../constants/templates")
const { getSettingsTemplateContext } = require("./templates")
@ -97,7 +97,7 @@ async function getSmtpConfiguration(db, groupId = null) {
if (groupId) {
params.group = groupId
}
return determineScopedConfig(db, params)
return getScopedConfig(db, params)
}
/**

View File

@ -1,5 +1,5 @@
const CouchDB = require("../db")
const { determineScopedConfig, StaticDatabases } = require("@budibase/auth").db
const { getScopedConfig, StaticDatabases } = require("@budibase/auth").db
const {
Configs,
TemplateBindings,
@ -15,7 +15,7 @@ const BASE_COMPANY = "Budibase"
exports.getSettingsTemplateContext = async (purpose, code = null) => {
const db = new CouchDB(StaticDatabases.GLOBAL.name)
// TODO: use more granular settings in the future if required
const settings = await determineScopedConfig(db, { type: Configs.SETTINGS })
const settings = await getScopedConfig(db, { type: Configs.SETTINGS })
if (!settings.platformUrl) {
settings.platformUrl = LOCAL_URL
}