Fixing dev-tools handling of custom roles and build issue - PR comments.

This commit is contained in:
mike12345567 2023-09-27 16:52:20 +01:00
parent 395969e0f0
commit e3469ed38d
5 changed files with 39 additions and 30 deletions

View File

@ -85,7 +85,6 @@
onMount(async () => { onMount(async () => {
await initialise() await initialise()
await authStore.actions.fetchUser() await authStore.actions.fetchUser()
await roleStore.actions.fetchAccessibleRoles()
dataLoaded = true dataLoaded = true
if (get(builderStore).inBuilder) { if (get(builderStore).inBuilder) {
builderStore.actions.notifyLoaded() builderStore.actions.notifyLoaded()

View File

@ -60,8 +60,7 @@
}) })
setContext("layout", store) setContext("layout", store)
$: accessibleRoles = $roleStore $: validLinks = getValidLinks(links, $roleStore)
$: validLinks = getValidLinks(links)
$: typeClass = NavigationClasses[navigation] || NavigationClasses.None $: typeClass = NavigationClasses[navigation] || NavigationClasses.None
$: navWidthClass = WidthClasses[navWidth || width] || WidthClasses.Large $: navWidthClass = WidthClasses[navWidth || width] || WidthClasses.Large
$: pageWidthClass = WidthClasses[pageWidth || width] || WidthClasses.Large $: pageWidthClass = WidthClasses[pageWidth || width] || WidthClasses.Large
@ -99,12 +98,12 @@
} }
} }
const getValidLinks = allLinks => { const getValidLinks = (allLinks, userRoleHierarchy) => {
// Strip links missing required info // Strip links missing required info
let validLinks = (allLinks || []).filter(link => link.text && link.url) let validLinks = (allLinks || []).filter(link => link.text && link.url)
// Filter to only links allowed by the current role // Filter to only links allowed by the current role
return validLinks.filter(link => { return validLinks.filter(link => {
return accessibleRoles?.find(roleId => roleId === link.roleId) return userRoleHierarchy?.find(roleId => roleId === link.roleId)
}) })
} }

View File

@ -1,32 +1,39 @@
<script> <script>
import { Heading, Select, ActionButton } from "@budibase/bbui" import { Heading, Select, ActionButton } from "@budibase/bbui"
import { devToolsStore, appStore } from "../../stores" import { devToolsStore, appStore, roleStore } from "../../stores"
import { getContext } from "svelte" import { getContext, onMount } from "svelte"
const context = getContext("context") const context = getContext("context")
const SELF_ROLE = "self"
$: previewOptions = [ let staticRoleList
{
$: previewOptions = buildRoleList(staticRoleList)
function buildRoleList(roleIds) {
const list = []
list.push({
label: "View as yourself", label: "View as yourself",
value: "self", value: SELF_ROLE,
}, })
{ if (!roleIds) {
label: "View as public user", return list
value: "PUBLIC", }
}, for (let roleId of roleIds) {
{ list.push({
label: "View as basic user", label: `View as ${roleId.toLowerCase()} user`,
value: "BASIC", value: roleId,
}, })
{ }
label: "View as power user", devToolsStore.actions.changeRole(SELF_ROLE)
value: "POWER", return list
}, }
{
label: "View as admin user", onMount(async () => {
value: "ADMIN", // make sure correct before starting
}, await devToolsStore.actions.changeRole(SELF_ROLE)
] staticRoleList = await roleStore.actions.fetchAccessibleRoles()
})
</script> </script>
<div class="dev-preview-header" class:mobile={$context.device.mobile}> <div class="dev-preview-header" class:mobile={$context.device.mobile}>
@ -34,7 +41,7 @@
<Select <Select
quiet quiet
options={previewOptions} options={previewOptions}
value={$devToolsStore.role || "self"} value={$devToolsStore.role || SELF_ROLE}
placeholder={null} placeholder={null}
autoWidth autoWidth
on:change={e => devToolsStore.actions.changeRole(e.detail)} on:change={e => devToolsStore.actions.changeRole(e.detail)}

View File

@ -1,5 +1,6 @@
import { API } from "api" import { API } from "api"
import { writable } from "svelte/store" import { writable } from "svelte/store"
import { currentRole } from "./derived"
const createRoleStore = () => { const createRoleStore = () => {
const store = writable([]) const store = writable([])
@ -9,6 +10,7 @@ const createRoleStore = () => {
const accessible = await API.getAccessibleRoles() const accessible = await API.getAccessibleRoles()
// Use the app self if present, otherwise fallback to the global self // Use the app self if present, otherwise fallback to the global self
store.set(accessible || []) store.set(accessible || [])
return accessible
} }
return { return {
@ -18,3 +20,5 @@ const createRoleStore = () => {
} }
export const roleStore = createRoleStore() export const roleStore = createRoleStore()
currentRole.subscribe(roleStore.actions.fetchAccessibleRoles)

View File

@ -55,7 +55,7 @@ const checkAuthorizedResource = async (
) => { ) => {
// get the user's roles // get the user's roles
const roleId = ctx.roleId || roles.BUILTIN_ROLE_IDS.PUBLIC const roleId = ctx.roleId || roles.BUILTIN_ROLE_IDS.PUBLIC
const userRoles = await roles.getUserRoleIdHierarchy(roleId) const userRoles = await roles.getUserRoleHierarchy(roleId)
const permError = "User does not have permission" const permError = "User does not have permission"
// check if the user has the required role // check if the user has the required role
if (resourceRoles.length > 0) { if (resourceRoles.length > 0) {