Fixing an issue where you could accidentally end up on org/admin page due to the reactivity statements firing all the time.

This commit is contained in:
mike12345567 2021-07-27 17:56:39 +01:00
parent 5324f506b6
commit 35f1b50511
7 changed files with 29 additions and 6 deletions

View File

@ -2,6 +2,7 @@ const { newid } = require("../hashing")
const Replication = require("./Replication") const Replication = require("./Replication")
const { getDB } = require("./index") const { getDB } = require("./index")
const { DEFAULT_TENANT_ID } = require("../constants") const { DEFAULT_TENANT_ID } = require("../constants")
const env = require("../environment")
const UNICODE_MAX = "\ufff0" const UNICODE_MAX = "\ufff0"
const SEPARATOR = "_" const SEPARATOR = "_"
@ -82,6 +83,9 @@ exports.getGlobalDB = tenantId => {
if (tenantId && tenantId !== DEFAULT_TENANT_ID) { if (tenantId && tenantId !== DEFAULT_TENANT_ID) {
dbName = `${tenantId}${SEPARATOR}${dbName}` dbName = `${tenantId}${SEPARATOR}${dbName}`
} }
if (env.MULTI_TENANCY && tenantId == null) {
throw "Cannot create global DB without tenantId"
}
return getDB(dbName) return getDB(dbName)
} }
@ -210,7 +214,7 @@ exports.getDeployedAppID = appId => {
* @return {Promise<object[]>} returns the app information document stored in each app database. * @return {Promise<object[]>} returns the app information document stored in each app database.
*/ */
exports.getAllApps = async (CouchDB, { tenantId, dev, all } = {}) => { exports.getAllApps = async (CouchDB, { tenantId, dev, all } = {}) => {
if (!tenantId) { if (!env.MULTI_TENANCY && !tenantId) {
tenantId = DEFAULT_TENANT_ID tenantId = DEFAULT_TENANT_ID
} }
let allDbs = await CouchDB.allDbs() let allDbs = await CouchDB.allDbs()

View File

@ -16,6 +16,7 @@ module.exports = {
MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY, MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
MINIO_URL: process.env.MINIO_URL, MINIO_URL: process.env.MINIO_URL,
INTERNAL_API_KEY: process.env.INTERNAL_API_KEY, INTERNAL_API_KEY: process.env.INTERNAL_API_KEY,
MULTI_TENANCY: process.env.MULTI_TENANCY,
isTest, isTest,
_set(key, value) { _set(key, value) {
process.env[key] = value process.env[key] = value

View File

@ -10,6 +10,7 @@ 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, Headers } = require("./constants") const { DEFAULT_TENANT_ID, Headers } = require("./constants")
const env = require("./environment")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -104,7 +105,7 @@ 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 let tenantId = env.MULTI_TENANCY ? DEFAULT_TENANT_ID : null
try { try {
const doc = await db.get(userId) const doc = await db.get(userId)
if (doc && doc.tenantId) { if (doc && doc.tenantId) {

View File

@ -16,9 +16,10 @@
// Force creation of an admin user if one doesn't exist // Force creation of an admin user if one doesn't exist
$: { $: {
if (loaded && multiTenancyEnabled && !tenantSet) { const apiReady = $admin.loaded && $auth.loaded
if (loaded && apiReady && multiTenancyEnabled && !tenantSet) {
$redirect("./auth/org") $redirect("./auth/org")
} else if (loaded && !hasAdminUser) { } else if (loaded && apiReady && !hasAdminUser) {
$redirect("./admin") $redirect("./admin")
} }
} }

View File

@ -27,6 +27,8 @@
auth.checkQueryString() auth.checkQueryString()
if (!multiTenancyEnabled) { if (!multiTenancyEnabled) {
$goto("../") $goto("../")
} else {
admin.unload()
} }
}) })
</script> </script>

View File

@ -3,7 +3,9 @@ import api from "builderStore/api"
import { auth } from "stores/portal" import { auth } from "stores/portal"
export function createAdminStore() { export function createAdminStore() {
const admin = writable({}) const admin = writable({
loaded: false,
})
async function init() { async function init() {
try { try {
@ -20,13 +22,14 @@ export function createAdminStore() {
0 0
) )
await multiTenancyEnabled()
admin.update(store => { admin.update(store => {
store.loaded = true
store.checklist = json store.checklist = json
store.onboardingProgress = store.onboardingProgress =
(stepsComplete / onboardingSteps.length) * 100 (stepsComplete / onboardingSteps.length) * 100
return store return store
}) })
await multiTenancyEnabled()
} catch (err) { } catch (err) {
admin.update(store => { admin.update(store => {
store.checklist = null store.checklist = null
@ -51,9 +54,17 @@ export function createAdminStore() {
return enabled return enabled
} }
function unload() {
admin.update(store => {
store.loaded = false
return store
})
}
return { return {
subscribe: admin.subscribe, subscribe: admin.subscribe,
init, init,
unload,
} }
} }

View File

@ -6,6 +6,7 @@ export function createAuthStore() {
user: null, user: null,
tenantId: "default", tenantId: "default",
tenantSet: false, tenantSet: false,
loaded: false,
}) })
const store = derived(auth, $store => { const store = derived(auth, $store => {
let initials = null let initials = null
@ -30,6 +31,7 @@ export function createAuthStore() {
user: $store.user, user: $store.user,
tenantId: $store.tenantId, tenantId: $store.tenantId,
tenantSet: $store.tenantSet, tenantSet: $store.tenantSet,
loaded: $store.loaded,
initials, initials,
isAdmin, isAdmin,
isBuilder, isBuilder,
@ -38,6 +40,7 @@ export function createAuthStore() {
function setUser(user) { function setUser(user) {
auth.update(store => { auth.update(store => {
store.loaded = true
store.user = user store.user = user
if (user) { if (user) {
store.tenantId = user.tenantId || "default" store.tenantId = user.tenantId || "default"