2021-12-07 19:24:10 +01:00
|
|
|
<script>
|
2022-01-24 13:37:22 +01:00
|
|
|
import { Label, notifications, Select } from "@budibase/bbui"
|
2021-12-07 19:24:10 +01:00
|
|
|
import { permissions, roles } from "stores/backend"
|
2022-04-28 16:13:33 +02:00
|
|
|
import { Constants } from "@budibase/frontend-core"
|
2021-12-07 19:24:10 +01:00
|
|
|
|
|
|
|
export let query
|
|
|
|
export let label
|
|
|
|
|
2022-01-26 19:59:41 +01:00
|
|
|
$: getPermissions(query)
|
2021-12-07 19:24:10 +01:00
|
|
|
|
2022-01-26 19:59:41 +01:00
|
|
|
let roleId, loaded, fetched
|
2021-12-07 19:24:10 +01:00
|
|
|
|
|
|
|
async function updateRole(role, id) {
|
2022-01-24 13:37:22 +01:00
|
|
|
try {
|
|
|
|
roleId = role
|
|
|
|
const queryId = query?._id || id
|
|
|
|
if (roleId && queryId) {
|
|
|
|
for (let level of ["read", "write"]) {
|
|
|
|
await permissions.save({
|
|
|
|
level,
|
|
|
|
role,
|
|
|
|
resource: queryId,
|
|
|
|
})
|
|
|
|
}
|
2021-12-07 19:24:10 +01:00
|
|
|
}
|
2022-01-24 13:37:22 +01:00
|
|
|
} catch (error) {
|
|
|
|
notifications.error("Error updating role")
|
2021-12-07 19:24:10 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 19:50:29 +01:00
|
|
|
|
2022-01-26 19:59:41 +01:00
|
|
|
async function getPermissions(queryToFetch) {
|
|
|
|
if (fetched?._id === queryToFetch?._id) {
|
2022-02-03 19:26:26 +01:00
|
|
|
loaded = true
|
2022-01-26 19:59:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fetched = queryToFetch
|
|
|
|
if (!queryToFetch || !queryToFetch._id) {
|
2022-04-28 16:13:33 +02:00
|
|
|
roleId = Constants.Roles.BASIC
|
2021-12-07 19:50:29 +01:00
|
|
|
loaded = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try {
|
2022-01-26 19:59:41 +01:00
|
|
|
roleId = (await permissions.forResource(queryToFetch._id))["read"]
|
2021-12-07 19:50:29 +01:00
|
|
|
} catch (err) {
|
2022-04-28 16:13:33 +02:00
|
|
|
roleId = Constants.Roles.BASIC
|
2021-12-07 19:50:29 +01:00
|
|
|
}
|
|
|
|
loaded = true
|
2022-01-26 19:59:41 +01:00
|
|
|
}
|
2021-12-07 19:24:10 +01:00
|
|
|
</script>
|
|
|
|
|
2021-12-07 19:50:29 +01:00
|
|
|
{#if loaded}
|
|
|
|
{#if label}
|
|
|
|
<Label>{label}</Label>
|
|
|
|
{/if}
|
|
|
|
<Select
|
|
|
|
value={roleId}
|
|
|
|
on:change={e => updateRole(e.detail)}
|
|
|
|
options={$roles}
|
|
|
|
getOptionLabel={x => x.name}
|
|
|
|
getOptionValue={x => x._id}
|
|
|
|
autoWidth
|
|
|
|
/>
|
2021-12-07 19:24:10 +01:00
|
|
|
{/if}
|