A lot of general fixes around getting logged in, setting up users etc.

This commit is contained in:
mike12345567 2021-07-19 19:00:53 +01:00
parent 99fe16d169
commit 07dd7d68d4
14 changed files with 82 additions and 26 deletions

View File

@ -4,7 +4,9 @@
import { onMount } from "svelte"
let loaded = false
$: multiTenancyEnabled = $admin.multiTenancy
$: hasAdminUser = !!$admin?.checklist?.adminUser
$: tenantSet = $auth.tenantSet
onMount(async () => {
await admin.init()
@ -14,7 +16,9 @@
// Force creation of an admin user if one doesn't exist
$: {
if (loaded && !hasAdminUser) {
if (loaded && multiTenancyEnabled && !tenantSet) {
$redirect("./auth/org")
} else if (loaded && !hasAdminUser) {
$redirect("./admin")
}
}

View File

@ -9,15 +9,18 @@
} from "@budibase/bbui"
import { goto } from "@roxi/routify"
import api from "builderStore/api"
import { admin } from "stores/portal"
import { admin, auth } from "stores/portal"
import PasswordRepeatInput from "components/common/users/PasswordRepeatInput.svelte"
import Logo from "assets/bb-emblem.svg"
let adminUser = {}
let error
$: tenantId = $auth.tenantId
async function save() {
try {
adminUser.tenantId = tenantId
// Save the admin user
const response = await api.post(`/api/admin/users/init`, adminUser)
const json = await response.json()
@ -44,7 +47,6 @@
</Body>
</Layout>
<Layout gap="XS" noPadding>
<Input label="Organisation" bind:value={adminUser.tenantId} />
<Input label="Email" bind:value={adminUser.email} />
<PasswordRepeatInput bind:password={adminUser.password} bind:error />
</Layout>

View File

@ -1,5 +1,24 @@
<script>
import { redirect } from "@roxi/routify"
// TODO: need to check if the tenant is already set
$redirect("./org")
import { auth, admin } from "stores/portal"
import { onMount } from "svelte"
$: tenantSet = $auth.tenantSet
$: multiTenancyEnabled = $admin.multiTenancy
let loaded = false
$: {
console.log(loaded)
if (loaded && multiTenancyEnabled && !tenantSet) {
$redirect("./org")
} else if (loaded) {
$redirect("./login")
}
}
onMount(async () => {
await admin.init()
loaded = true
})
</script>

View File

@ -1,14 +1,16 @@
<script>
import { Body, Button, Divider, Heading, Input, Layout } from "@budibase/bbui"
import { goto } from "@roxi/routify"
import { auth } from "stores/portal"
import { auth, admin } from "stores/portal"
import Logo from "assets/bb-emblem.svg"
let tenantId = ""
async function setOrg() {
auth.setOrg(tenantId)
$goto("./login")
// re-init now org selected
await admin.init()
$goto("../")
}
function handleKeydown(evt) {

View File

@ -4,7 +4,7 @@
$: {
if (!$auth.user) {
$redirect("./auth/login")
$redirect("./auth")
} else if ($auth.user.builder?.global) {
$redirect("./portal")
} else {

View File

@ -90,10 +90,13 @@
}
loading = false
requireAuth = smtpConfig.config.auth != null
console.log(requireAuth)
// always attach the auth for the forms purpose -
// this will be removed later if required
smtpConfig.config.auth = {
type: "login",
if (!smtpDoc.config.auth) {
smtpConfig.config.auth = {
type: "login",
}
}
}

View File

@ -3,7 +3,7 @@ import api from "builderStore/api"
import { auth } from "stores/portal"
export function createAdminStore() {
const { subscribe, set } = writable({})
const admin = writable({})
async function init() {
try {
@ -20,19 +20,38 @@ export function createAdminStore() {
0
)
set({
checklist: json,
onboardingProgress: (stepsComplete / onboardingSteps.length) * 100,
admin.update(store => {
store.checklist = json
store.onboardingProgress = (stepsComplete / onboardingSteps.length) * 100
return store
})
await multiTenancyEnabled()
} catch (err) {
set({
checklist: null,
admin.update( store => {
store.checklist = null
return store
})
}
}
async function multiTenancyEnabled() {
let enabled = false
try {
const response = await api.get(`/api/admin/tenants/enabled`)
const json = await response.json()
enabled = json.enabled
} catch (err) {
// just let it stay disabled
}
admin.update(store => {
store.multiTenancy = enabled
return store
})
return enabled
}
return {
subscribe,
subscribe: admin.subscribe,
init,
}
}

View File

@ -5,6 +5,7 @@ export function createAuthStore() {
const auth = writable({
user: null,
tenantId: "default",
tenantSet: false,
})
const store = derived(auth, $store => {
let initials = null
@ -28,6 +29,7 @@ export function createAuthStore() {
return {
user: $store.user,
tenantId: $store.tenantId,
tenantSet: $store.tenantSet,
initials,
isAdmin,
isBuilder,
@ -46,6 +48,7 @@ export function createAuthStore() {
setOrg: tenantId => {
auth.update(store => {
store.tenantId = tenantId
store.tenantSet = true
return store
})
},

View File

@ -12,12 +12,11 @@ async function init() {
INTERNAL_API_KEY: "budibase",
MINIO_ACCESS_KEY: "budibase",
MINIO_SECRET_KEY: "budibase",
COUCH_DB_USER: "budibase",
COUCH_DB_PASSWORD: "budibase",
REDIS_URL: "localhost:6379",
REDIS_PASSWORD: "budibase",
MINIO_URL: "http://localhost:10000/",
COUCH_DB_URL: "http://budibase:budibase@localhost:10000/db/",
MULTI_TENANCY: false,
}
let envFile = ""
Object.keys(envFileJson).forEach(key => {

View File

@ -1,5 +1,9 @@
exports.multiTenancyEnabled = async ctx => {
const env = require("../../../environment")
exports.multiTenancyEnabled = async ctx => {
ctx.body = {
enabled: !!env.MULTI_TENANCY,
}
}
exports.exists = async ctx => {

View File

@ -41,6 +41,10 @@ const PUBLIC_ENDPOINTS = [
route: "/api/admin/configs/public",
method: "GET",
},
{
route: "api/admin/tenants/enabled",
method: "GET",
}
]
const router = new Router()

View File

@ -1,8 +1,5 @@
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()

View File

@ -2,6 +2,7 @@ const userRoutes = require("./admin/users")
const configRoutes = require("./admin/configs")
const workspaceRoutes = require("./admin/workspaces")
const templateRoutes = require("./admin/templates")
const tenantsRoutes = require("./admin/tenants")
const emailRoutes = require("./admin/email")
const authRoutes = require("./admin/auth")
const roleRoutes = require("./admin/roles")
@ -15,6 +16,7 @@ exports.routes = [
authRoutes,
appRoutes,
templateRoutes,
tenantsRoutes,
emailRoutes,
sessionRoutes,
roleRoutes,

View File

@ -30,9 +30,7 @@ module.exports = {
REDIS_URL: process.env.REDIS_URL,
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
INTERNAL_API_KEY: process.env.INTERNAL_API_KEY,
/* TODO: to remove - once deployment removed */
COUCH_DB_USERNAME: process.env.COUCH_DB_USERNAME,
COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD,
MULTI_TENANCY: process.env.MULTI_TENANCY,
_set(key, value) {
process.env[key] = value
module.exports[key] = value