Support for tenancy subdomain locally + improvements

This commit is contained in:
Rory Powell 2021-10-01 15:55:57 +01:00
parent 6a6dac9d3a
commit 4ce0147217
4 changed files with 50 additions and 10 deletions

View File

@ -50,6 +50,8 @@
"multi:disable": "lerna run multi:disable", "multi:disable": "lerna run multi:disable",
"selfhost:enable": "lerna run selfhost:enable", "selfhost:enable": "lerna run selfhost:enable",
"selfhost:disable": "lerna run selfhost:disable", "selfhost:disable": "lerna run selfhost:disable",
"localdomain:enable": "lerna run localdomain:enable",
"localdomain:disable": "lerna run localdomain:disable",
"postinstall": "husky install" "postinstall": "husky install"
} }
} }

View File

@ -14,16 +14,30 @@
$: useAccountPortal = cloud && !$admin.disableAccountPortal $: useAccountPortal = cloud && !$admin.disableAccountPortal
const validateTenantId = async () => { const validateTenantId = async () => {
// set the tenant from the url in the cloud const host = window.location.host
const tenantId = window.location.host.split(".")[0] if (host.includes("localhost:")) {
// ignore local dev
return
}
if (!tenantId.includes("localhost:")) { if (user && user.tenantId) {
// user doesn't have permission to access this tenant - kick them out let urlTenantId
if (user && user.tenantId !== tenantId) { const hostParts = host.split(".")
await auth.logout()
await auth.setOrganisation(null) // only run validation when we know we are in a tenant url
// not when we visit the root budibase.app domain
// e.g. ['tenant', 'budibase', 'app'] vs ['budibase', 'app']
if (hostParts.length > 2) {
urlTenantId = hostParts[0]
} else { } else {
await auth.setOrganisation(tenantId) // no tenant in the url - send to account portal to fix this
window.location.href = $admin.accountPortalUrl
return
}
if (user.tenantId !== urlTenantId) {
// user should not be here - play it safe and log them out
await auth.logout()
} }
} }
} }
@ -32,7 +46,7 @@
await auth.checkAuth() await auth.checkAuth()
await admin.init() await admin.init()
if (cloud && multiTenancyEnabled) { if (useAccountPortal && multiTenancyEnabled) {
await validateTenantId() await validateTenantId()
} }

View File

@ -20,7 +20,9 @@
"multi:enable": "node scripts/multiTenancy.js enable", "multi:enable": "node scripts/multiTenancy.js enable",
"multi:disable": "node scripts/multiTenancy.js disable", "multi:disable": "node scripts/multiTenancy.js disable",
"selfhost:enable": "node scripts/selfhost.js enable", "selfhost:enable": "node scripts/selfhost.js enable",
"selfhost:disable": "node scripts/selfhost.js disable" "selfhost:disable": "node scripts/selfhost.js disable",
"localdomain:enable": "node scripts/localdomain.js enable",
"localdomain:disable": "node scripts/localdomain.js disable"
}, },
"author": "Budibase", "author": "Budibase",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",

View File

@ -0,0 +1,22 @@
#!/usr/bin/env node
const updateDotEnv = require("update-dotenv")
const arg = process.argv.slice(2)[0]
/**
* For testing multi tenancy sub domains locally.
*
* Relies on an entry in /etc/hosts e.g:
*
* 127.0.0.1 local.com
*
* and an entry for each tenant you wish to test locally e.g:
*
* 127.0.0.1 t1.local.com
* 127.0.0.1 t2.local.com
*/
updateDotEnv({
ACCOUNT_PORTAL_URL:
arg === "enable" ? "http://local.com:10001" : "http://localhost:10001",
COOKIE_DOMAIN: arg === "enable" ? ".local.com" : "",
}).then(() => console.log("Updated worker!"))