1
0
Fork 0
mirror of synced 2024-07-02 21:10:43 +12:00

Removing tenancy validity check, instead depending on the PLATFORM_URL environment variable for determining tenant ID.

This commit is contained in:
mike12345567 2023-03-29 16:19:35 +01:00
parent 0257617ba1
commit 26aeac357d
10 changed files with 16 additions and 72 deletions

View file

@ -1,6 +1,6 @@
<script>
import { isActive, redirect, params } from "@roxi/routify"
import { admin, auth, licensing, tenants } from "stores/portal"
import { admin, auth, licensing } from "stores/portal"
import { onMount } from "svelte"
import { CookieUtils, Constants } from "@budibase/frontend-core"
import { API } from "api"
@ -10,24 +10,27 @@
$: multiTenancyEnabled = $admin.multiTenancy
$: hasAdminUser = $admin?.checklist?.adminUser?.checked
$: baseUrl = $admin?.baseUrl
$: tenantSet = $auth.tenantSet
$: cloud = $admin.cloud
$: cloud = $admin?.cloud
$: user = $auth.user
$: useAccountPortal = cloud && !$admin.disableAccountPortal
const validateTenantId = async () => {
const host = window.location.host
if (host.includes("localhost:")) {
if (host.includes("localhost:") || !baseUrl) {
// ignore local dev
return
}
// e.g. ['tenant', 'budibase', 'app'] vs ['budibase', 'app']
const mainHost = new URL(baseUrl).host
let urlTenantId
const hostParts = host.split(".")
if (hostParts.length > 2) {
urlTenantId = hostParts[0]
// remove the main host part
const hostParts = host.split(mainHost).filter(part => part !== "")
// if there is a part left, it has to be the tenant ID subdomain
if (hostParts.length === 1) {
urlTenantId = hostParts[0].replace(/\./g, "")
}
if (user && user.tenantId) {
@ -41,16 +44,15 @@
return
}
// check if real tenant
const { exists: tenantExists } = await tenants.info(urlTenantId)
if (tenantExists && user.tenantId !== urlTenantId) {
if (urlTenantId && user.tenantId !== urlTenantId) {
// user should not be here - play it safe and log them out
try {
await auth.logout()
await auth.setOrganisation(null)
} catch (error) {
console.error("Tenant mis-match, logout.")
console.error(
`Tenant mis-match - "${urlTenantId}" and "${user.tenantId}" - logout`
)
}
}
} else {

View file

@ -53,6 +53,7 @@ export function createAdminStore() {
store.disableAccountPortal = environment.disableAccountPortal
store.accountPortalUrl = environment.accountPortalUrl
store.isDev = environment.isDev
store.baseUrl = environment.baseUrl
return store
})
}

View file

@ -14,4 +14,3 @@ export { overview } from "./overview"
export { environment } from "./environment"
export { menu } from "./menu"
export { auditLogs } from "./auditLogs"
export { tenants } from "./tenants"

View file

@ -1,27 +0,0 @@
import { writable, get } from "svelte/store"
import { API } from "api"
export function tenantsStore() {
const store = writable({ tenantInfo: {} })
return {
info: async tenantId => {
if (!tenantId) {
return { exists: false }
}
const contents = get(store)
const found = contents.tenantInfo[tenantId]
if (found) {
return found
}
const tenantInfo = await API.getTenantInfo(tenantId)
store.update(state => {
state.tenantInfo[tenantId] = tenantInfo
return state
})
return tenantInfo
},
}
}
export const tenants = tenantsStore()

View file

@ -29,7 +29,6 @@ import { buildBackupsEndpoints } from "./backups"
import { buildEnvironmentVariableEndpoints } from "./environmentVariables"
import { buildEventEndpoints } from "./events"
import { buildAuditLogsEndpoints } from "./auditLogs"
import { buildTenantEndpoints } from "./tenants"
const defaultAPIClientConfig = {
/**
@ -254,6 +253,5 @@ export const createAPIClient = config => {
...buildEnvironmentVariableEndpoints(API),
...buildEventEndpoints(API),
...buildAuditLogsEndpoints(API),
...buildTenantEndpoints(API),
}
}

View file

@ -1,8 +0,0 @@
export const buildTenantEndpoints = API => ({
/**
* Get information about a tenant
*/
getTenantInfo: async tenantId => {
return await API.get({ url: `/api/system/tenants/${tenantId}/info` })
},
})

View file

@ -7,6 +7,7 @@ export const fetch = async (ctx: BBContext) => {
cloud: !env.SELF_HOSTED,
accountPortalUrl: env.ACCOUNT_PORTAL_URL,
disableAccountPortal: env.DISABLE_ACCOUNT_PORTAL,
baseUrl: env.PLATFORM_URL,
// in test need to pretend its in production for the UI (Cypress)
isDev: env.isDev() && !env.isTest(),
}

View file

@ -10,6 +10,4 @@ router.delete(
controller.destroy
)
router.get("/api/system/tenants/:tenantId/info", controller.info)
export default router

View file

@ -58,17 +58,4 @@ describe("/api/global/tenants", () => {
expect(res.body).toEqual(config.adminOnlyResponse())
})
})
describe("GET /api/system/tenants/:tenantId/info", () => {
it("allows retrieving information about the tenant", async () => {
const user1 = await config.createTenant()
const res = await config.api.tenants.info(user1.tenantId)
expect(res.body.exists).toEqual(true)
})
it("check a tenant that doesn't exist", async () => {
const res = await config.api.tenants.info("cannot-exist-tenantid")
expect(res.body.exists).toEqual(false)
})
})
})

View file

@ -14,11 +14,4 @@ export class TenantAPI extends TestAPI {
.set(opts?.headers)
.expect(opts?.status ? opts.status : 204)
}
info = (tenantId: string) => {
return this.request
.get(`/api/system/tenants/${tenantId}/info`)
.set(this.config.defaultHeaders())
.expect(200)
}
}