Merge pull request #10149 from Budibase/fix/budicloud-log-out

Budicloud logout on googlesheet auth
This commit is contained in:
Michael Drury 2023-03-29 14:47:15 +01:00 committed by GitHub
commit d255bfad0b
11 changed files with 79 additions and 3 deletions

View File

@ -162,7 +162,7 @@ export async function getGoogleConfig(): Promise<
export async function getGoogleDatasourceConfig(): Promise< export async function getGoogleDatasourceConfig(): Promise<
GoogleInnerConfig | undefined GoogleInnerConfig | undefined
> { > {
if (!env.SELF_HOSTED) { if (!env.isDev() && !env.SELF_HOSTED) {
// always use the env vars in cloud // always use the env vars in cloud
return getDefaultGoogleConfig() return getDefaultGoogleConfig()
} }

View File

@ -1,6 +1,6 @@
<script> <script>
import { isActive, redirect, params } from "@roxi/routify" import { isActive, redirect, params } from "@roxi/routify"
import { admin, auth, licensing } from "stores/portal" import { admin, auth, licensing, tenants } from "stores/portal"
import { onMount } from "svelte" import { onMount } from "svelte"
import { CookieUtils, Constants } from "@budibase/frontend-core" import { CookieUtils, Constants } from "@budibase/frontend-core"
import { API } from "api" import { API } from "api"
@ -41,7 +41,10 @@
return return
} }
if (urlTenantId && user.tenantId !== urlTenantId) { // check if real tenant
const { exists: tenantExists } = await tenants.info(urlTenantId)
if (tenantExists && user.tenantId !== urlTenantId) {
// user should not be here - play it safe and log them out // user should not be here - play it safe and log them out
try { try {
await auth.logout() await auth.logout()

View File

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

View File

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

View File

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

View File

@ -17,3 +17,7 @@ export async function destroy(ctx: UserCtx) {
throw err throw err
} }
} }
export async function info(ctx: UserCtx) {
ctx.body = await tenantSdk.tenantInfo(ctx.params.tenantId)
}

View File

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

View File

@ -58,4 +58,17 @@ describe("/api/global/tenants", () => {
expect(res.body).toEqual(config.adminOnlyResponse()) 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

@ -74,3 +74,10 @@ async function removeTenantUsers(tenantId: string) {
throw err throw err
} }
} }
export async function tenantInfo(tenantId: string) {
const globalDbName = tenancy.getGlobalDBName(tenantId)
return {
exists: await dbCore.dbExists(globalDbName),
}
}

View File

@ -2,8 +2,10 @@ import TestConfiguration from "../TestConfiguration"
import { TestAPI, TestAPIOpts } from "./base" import { TestAPI, TestAPIOpts } from "./base"
export class TenantAPI extends TestAPI { export class TenantAPI extends TestAPI {
config: TestConfiguration
constructor(config: TestConfiguration) { constructor(config: TestConfiguration) {
super(config) super(config)
this.config = config
} }
delete = (tenantId: string, opts?: TestAPIOpts) => { delete = (tenantId: string, opts?: TestAPIOpts) => {
@ -12,4 +14,11 @@ export class TenantAPI extends TestAPI {
.set(opts?.headers) .set(opts?.headers)
.expect(opts?.status ? opts.status : 204) .expect(opts?.status ? opts.status : 204)
} }
info = (tenantId: string) => {
return this.request
.get(`/api/system/tenants/${tenantId}/info`)
.set(this.config.defaultHeaders())
.expect(200)
}
} }