Fixing some tenancy issues.

This commit is contained in:
mike12345567 2021-07-19 12:57:52 +01:00
parent d6c47c5027
commit e56a0f5aa3
7 changed files with 62 additions and 14 deletions

View File

@ -9,6 +9,7 @@ const { options } = require("./middleware/passport/jwt")
const { createUserEmailView } = require("./db/views")
const { getDB } = require("./db")
const { getGlobalDB } = require("./db/utils")
const { DEFAULT_TENANT_ID } = require("./constants")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -103,11 +104,16 @@ exports.isClient = ctx => {
exports.lookupTenantId = async userId => {
const db = getDB(StaticDatabases.PLATFORM_INFO.name)
const doc = await db.get(userId)
if (!doc || !doc.tenantId) {
throw "Unable to find tenant"
let tenantId = DEFAULT_TENANT_ID
try {
const doc = await db.get(userId)
if (doc && doc.tenantId) {
tenantId = doc.tenantId
}
} catch (err) {
// just return the default
}
return doc.tenantId
return tenantId
}
/**

View File

@ -161,7 +161,7 @@ exports.fetchView = async ctx => {
schema: {},
}
}
rows = await outputProcessing(appId, table, response.rows)
rows = await outputProcessing(ctx, table, response.rows)
}
if (calculation === CALCULATION_TYPES.STATS) {
@ -204,7 +204,7 @@ exports.fetch = async ctx => {
)
rows = response.rows.map(row => row.doc)
}
return outputProcessing(appId, table, rows)
return outputProcessing(ctx, table, rows)
}
exports.find = async ctx => {
@ -212,7 +212,7 @@ exports.find = async ctx => {
const db = new CouchDB(appId)
const table = await db.get(ctx.params.tableId)
let row = await findRow(ctx, db, ctx.params.tableId, ctx.params.rowId)
row = await outputProcessing(appId, table, row)
row = await outputProcessing(ctx, table, row)
return row
}
@ -290,7 +290,7 @@ exports.search = async ctx => {
// Enrich search results with relationships
if (response.rows && response.rows.length) {
const table = await db.get(tableId)
response.rows = await outputProcessing(appId, table, response.rows)
response.rows = await outputProcessing(ctx, table, response.rows)
}
return response
@ -327,7 +327,7 @@ exports.fetchEnrichedRow = async ctx => {
})
// need to include the IDs in these rows for any links they may have
let linkedRows = await outputProcessing(
appId,
ctx,
table,
response.rows.map(row => row.doc)
)

View File

@ -387,7 +387,7 @@ describe("/rows", () => {
})
// the environment needs configured for this
await setup.switchToSelfHosted(async () => {
const enriched = await outputProcessing(config.getAppId(), table, [row])
const enriched = await outputProcessing({ appId: config.getAppId() }, table, [row])
expect(enriched[0].attachment[0].url).toBe(
`/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`
)

View File

@ -207,7 +207,7 @@ exports.outputProcessing = async (ctx, table, rows) => {
wasArray = false
}
// attach any linked row information
let enriched = await linkRows.attachFullLinkedDocs(appId, table, rows)
let enriched = await linkRows.attachFullLinkedDocs(ctx, table, rows)
// process formulas
enriched = processFormulas(table, enriched)

View File

@ -0,0 +1,11 @@
exports.multiTenancyEnabled = async ctx => {
}
exports.exists = async ctx => {
}
exports.fetch = async ctx => {
}

View File

@ -14,19 +14,19 @@ const { invalidateSessions } = require("@budibase/auth/sessions")
const CouchDB = require("../../../db")
const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name
const tenantDocId = StaticDatabases.PLATFORM_INFO.docs.tenants
const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants
async function tryAddTenant(tenantId) {
const db = new CouchDB(PLATFORM_INFO_DB)
let tenants
try {
tenants = await db.get(tenantDocId)
tenants = await db.get(TENANT_DOC)
} catch (err) {
// if theres an error don't worry, we'll just write it in
}
if (!tenants || !Array.isArray(tenants.tenantIds)) {
tenants = {
_id: tenantDocId,
_id: TENANT_DOC,
tenantIds: [],
}
}
@ -36,6 +36,18 @@ async function tryAddTenant(tenantId) {
}
}
async function doesTenantExist(tenantId) {
const db = new CouchDB(PLATFORM_INFO_DB)
let tenants
try {
tenants = await db.get(TENANT_DOC)
} catch (err) {
// if theres an error the doc doesn't exist, no tenants exist
return false
}
return tenants && Array.isArray(tenants.tenantIds) && tenants.tenantIds.indexOf(tenantId) !== -1
}
async function allUsers(ctx) {
const db = getGlobalDBFromCtx(ctx)
const response = await db.allDocs(
@ -121,6 +133,10 @@ exports.save = async ctx => {
exports.adminUser = async ctx => {
const { email, password, tenantId } = ctx.request.body
if (await doesTenantExist(tenantId)) {
ctx.throw(403, "Organisation already exists.")
}
const db = getGlobalDB(tenantId)
const response = await db.allDocs(
getGlobalUserParams(null, {

View File

@ -0,0 +1,15 @@
const Router = require("@koa/router")
const controller = require("../../controllers/admin/tenants")
const joiValidator = require("../../../middleware/joi-validator")
const Joi = require("joi")
const { TemplatePurpose, TemplateTypes } = require("../../../constants")
const adminOnly = require("../../../middleware/adminOnly")
const router = Router()
router
.get("/api/admin/tenants/enabled", controller.multiTenancyEnabled)
.get("/api/admin/tenants/:tenantId/exists", controller.exists)
.get("/api/admin/tenants", adminOnly, controller.fetch)
module.exports = router