Fixing some tenancy issues.
This commit is contained in:
parent
1252fbdf38
commit
8241df2581
|
@ -9,6 +9,7 @@ const { options } = require("./middleware/passport/jwt")
|
||||||
const { createUserEmailView } = require("./db/views")
|
const { createUserEmailView } = require("./db/views")
|
||||||
const { getDB } = require("./db")
|
const { getDB } = require("./db")
|
||||||
const { getGlobalDB } = require("./db/utils")
|
const { getGlobalDB } = require("./db/utils")
|
||||||
|
const { DEFAULT_TENANT_ID } = require("./constants")
|
||||||
|
|
||||||
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
||||||
|
|
||||||
|
@ -103,11 +104,16 @@ exports.isClient = ctx => {
|
||||||
|
|
||||||
exports.lookupTenantId = async userId => {
|
exports.lookupTenantId = async userId => {
|
||||||
const db = getDB(StaticDatabases.PLATFORM_INFO.name)
|
const db = getDB(StaticDatabases.PLATFORM_INFO.name)
|
||||||
|
let tenantId = DEFAULT_TENANT_ID
|
||||||
|
try {
|
||||||
const doc = await db.get(userId)
|
const doc = await db.get(userId)
|
||||||
if (!doc || !doc.tenantId) {
|
if (doc && doc.tenantId) {
|
||||||
throw "Unable to find tenant"
|
tenantId = doc.tenantId
|
||||||
}
|
}
|
||||||
return doc.tenantId
|
} catch (err) {
|
||||||
|
// just return the default
|
||||||
|
}
|
||||||
|
return tenantId
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -161,7 +161,7 @@ exports.fetchView = async ctx => {
|
||||||
schema: {},
|
schema: {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rows = await outputProcessing(appId, table, response.rows)
|
rows = await outputProcessing(ctx, table, response.rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (calculation === CALCULATION_TYPES.STATS) {
|
if (calculation === CALCULATION_TYPES.STATS) {
|
||||||
|
@ -204,7 +204,7 @@ exports.fetch = async ctx => {
|
||||||
)
|
)
|
||||||
rows = response.rows.map(row => row.doc)
|
rows = response.rows.map(row => row.doc)
|
||||||
}
|
}
|
||||||
return outputProcessing(appId, table, rows)
|
return outputProcessing(ctx, table, rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.find = async ctx => {
|
exports.find = async ctx => {
|
||||||
|
@ -212,7 +212,7 @@ exports.find = async ctx => {
|
||||||
const db = new CouchDB(appId)
|
const db = new CouchDB(appId)
|
||||||
const table = await db.get(ctx.params.tableId)
|
const table = await db.get(ctx.params.tableId)
|
||||||
let row = await findRow(ctx, db, ctx.params.tableId, ctx.params.rowId)
|
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
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ exports.search = async ctx => {
|
||||||
// Enrich search results with relationships
|
// Enrich search results with relationships
|
||||||
if (response.rows && response.rows.length) {
|
if (response.rows && response.rows.length) {
|
||||||
const table = await db.get(tableId)
|
const table = await db.get(tableId)
|
||||||
response.rows = await outputProcessing(appId, table, response.rows)
|
response.rows = await outputProcessing(ctx, table, response.rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
@ -327,7 +327,7 @@ exports.fetchEnrichedRow = async ctx => {
|
||||||
})
|
})
|
||||||
// need to include the IDs in these rows for any links they may have
|
// need to include the IDs in these rows for any links they may have
|
||||||
let linkedRows = await outputProcessing(
|
let linkedRows = await outputProcessing(
|
||||||
appId,
|
ctx,
|
||||||
table,
|
table,
|
||||||
response.rows.map(row => row.doc)
|
response.rows.map(row => row.doc)
|
||||||
)
|
)
|
||||||
|
|
|
@ -387,7 +387,7 @@ describe("/rows", () => {
|
||||||
})
|
})
|
||||||
// the environment needs configured for this
|
// the environment needs configured for this
|
||||||
await setup.switchToSelfHosted(async () => {
|
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(
|
expect(enriched[0].attachment[0].url).toBe(
|
||||||
`/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`
|
`/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`
|
||||||
)
|
)
|
||||||
|
|
|
@ -207,7 +207,7 @@ exports.outputProcessing = async (ctx, table, rows) => {
|
||||||
wasArray = false
|
wasArray = false
|
||||||
}
|
}
|
||||||
// attach any linked row information
|
// attach any linked row information
|
||||||
let enriched = await linkRows.attachFullLinkedDocs(appId, table, rows)
|
let enriched = await linkRows.attachFullLinkedDocs(ctx, table, rows)
|
||||||
|
|
||||||
// process formulas
|
// process formulas
|
||||||
enriched = processFormulas(table, enriched)
|
enriched = processFormulas(table, enriched)
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
exports.multiTenancyEnabled = async ctx => {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.exists = async ctx => {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.fetch = async ctx => {
|
||||||
|
|
||||||
|
}
|
|
@ -14,19 +14,19 @@ const { invalidateSessions } = require("@budibase/auth/sessions")
|
||||||
const CouchDB = require("../../../db")
|
const CouchDB = require("../../../db")
|
||||||
|
|
||||||
const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name
|
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) {
|
async function tryAddTenant(tenantId) {
|
||||||
const db = new CouchDB(PLATFORM_INFO_DB)
|
const db = new CouchDB(PLATFORM_INFO_DB)
|
||||||
let tenants
|
let tenants
|
||||||
try {
|
try {
|
||||||
tenants = await db.get(tenantDocId)
|
tenants = await db.get(TENANT_DOC)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// if theres an error don't worry, we'll just write it in
|
// if theres an error don't worry, we'll just write it in
|
||||||
}
|
}
|
||||||
if (!tenants || !Array.isArray(tenants.tenantIds)) {
|
if (!tenants || !Array.isArray(tenants.tenantIds)) {
|
||||||
tenants = {
|
tenants = {
|
||||||
_id: tenantDocId,
|
_id: TENANT_DOC,
|
||||||
tenantIds: [],
|
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) {
|
async function allUsers(ctx) {
|
||||||
const db = getGlobalDBFromCtx(ctx)
|
const db = getGlobalDBFromCtx(ctx)
|
||||||
const response = await db.allDocs(
|
const response = await db.allDocs(
|
||||||
|
@ -121,6 +133,10 @@ exports.save = async ctx => {
|
||||||
|
|
||||||
exports.adminUser = async ctx => {
|
exports.adminUser = async ctx => {
|
||||||
const { email, password, tenantId } = ctx.request.body
|
const { email, password, tenantId } = ctx.request.body
|
||||||
|
if (await doesTenantExist(tenantId)) {
|
||||||
|
ctx.throw(403, "Organisation already exists.")
|
||||||
|
}
|
||||||
|
|
||||||
const db = getGlobalDB(tenantId)
|
const db = getGlobalDB(tenantId)
|
||||||
const response = await db.allDocs(
|
const response = await db.allDocs(
|
||||||
getGlobalUserParams(null, {
|
getGlobalUserParams(null, {
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue