permissions store

This commit is contained in:
Peter Clement 2025-01-08 10:55:11 +00:00
parent aec4cc5bfb
commit 5f353a85f7
2 changed files with 47 additions and 27 deletions

View File

@ -1,27 +0,0 @@
import { writable } from "svelte/store"
import { API } from "@/api"
export function createPermissionStore() {
const { subscribe } = writable([])
return {
subscribe,
save: async ({ level, role, resource }) => {
return await API.updatePermissionForResource(resource, role, level)
},
remove: async ({ level, role, resource }) => {
return await API.removePermissionFromResource(resource, role, level)
},
forResource: async resourceId => {
return (await API.getPermissionForResource(resourceId)).permissions
},
forResourceDetailed: async resourceId => {
return await API.getPermissionForResource(resourceId)
},
getDependantsInfo: async resourceId => {
return await API.getDependants(resourceId)
},
}
}
export const permissions = createPermissionStore()

View File

@ -0,0 +1,47 @@
import { BudiStore } from "../BudiStore"
import { API } from "@/api"
import {
PermissionLevel,
GetResourcePermsResponse,
GetDependantResourcesResponse,
} from "@budibase/types"
interface Permission {
level: PermissionLevel
role: string
resource: string
}
export class PermissionStore extends BudiStore<Permission[]> {
constructor() {
super([])
}
save = async (permission: Permission) => {
const { level, role, resource } = permission
return await API.updatePermissionForResource(resource, role, level)
}
remove = async (permission: Permission) => {
const { level, role, resource } = permission
return await API.removePermissionFromResource(resource, role, level)
}
forResource = async (resourceId: string): Promise<any> => {
return (await API.getPermissionForResource(resourceId)).permissions
}
forResourceDetailed = async (
resourceId: string
): Promise<GetResourcePermsResponse> => {
return await API.getPermissionForResource(resourceId)
}
getDependantsInfo = async (
resourceId: string
): Promise<GetDependantResourcesResponse> => {
return await API.getDependants(resourceId)
}
}
export const permissions = new PermissionStore()