Set default permissions
This commit is contained in:
parent
b3efea95bf
commit
d01462221f
|
@ -94,18 +94,17 @@ export async function getDependantResources(
|
||||||
|
|
||||||
export async function addPermission(ctx: UserCtx<void, AddPermissionResponse>) {
|
export async function addPermission(ctx: UserCtx<void, AddPermissionResponse>) {
|
||||||
const params: AddPermissionRequest = ctx.params
|
const params: AddPermissionRequest = ctx.params
|
||||||
ctx.body = await sdk.permissions.updatePermissionOnRole(
|
await sdk.permissions.updatePermissionOnRole(params, PermissionUpdateType.ADD)
|
||||||
params,
|
ctx.status = 200
|
||||||
PermissionUpdateType.ADD
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removePermission(
|
export async function removePermission(
|
||||||
ctx: UserCtx<void, RemovePermissionResponse>
|
ctx: UserCtx<void, RemovePermissionResponse>
|
||||||
) {
|
) {
|
||||||
const params: RemovePermissionRequest = ctx.params
|
const params: RemovePermissionRequest = ctx.params
|
||||||
ctx.body = await sdk.permissions.updatePermissionOnRole(
|
await sdk.permissions.updatePermissionOnRole(
|
||||||
params,
|
params,
|
||||||
PermissionUpdateType.REMOVE
|
PermissionUpdateType.REMOVE
|
||||||
)
|
)
|
||||||
|
ctx.status = 200
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,12 +42,11 @@ describe("/permission", () => {
|
||||||
|
|
||||||
describe("table permissions", () => {
|
describe("table permissions", () => {
|
||||||
let tableId: string
|
let tableId: string
|
||||||
let perms: Document[]
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const table = await config.createTable()
|
const table = await config.createTable()
|
||||||
tableId = table._id!
|
tableId = table._id!
|
||||||
perms = await config.api.permission.add({
|
await config.api.permission.add({
|
||||||
roleId: STD_ROLE_ID,
|
roleId: STD_ROLE_ID,
|
||||||
resourceId: tableId,
|
resourceId: tableId,
|
||||||
level: PermissionLevel.READ,
|
level: PermissionLevel.READ,
|
||||||
|
@ -59,11 +58,11 @@ describe("/permission", () => {
|
||||||
const { permissions } = await config.api.permission.get(table._id!)
|
const { permissions } = await config.api.permission.get(table._id!)
|
||||||
expect(permissions).toEqual({
|
expect(permissions).toEqual({
|
||||||
read: {
|
read: {
|
||||||
permissionType: "BASE",
|
permissionType: "EXPLICIT",
|
||||||
role: DEFAULT_TABLE_ROLE_ID,
|
role: DEFAULT_TABLE_ROLE_ID,
|
||||||
},
|
},
|
||||||
write: {
|
write: {
|
||||||
permissionType: "BASE",
|
permissionType: "EXPLICIT",
|
||||||
role: DEFAULT_TABLE_ROLE_ID,
|
role: DEFAULT_TABLE_ROLE_ID,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -71,11 +70,6 @@ describe("/permission", () => {
|
||||||
|
|
||||||
describe("add", () => {
|
describe("add", () => {
|
||||||
it("should be able to add permission to a role for the table", async () => {
|
it("should be able to add permission to a role for the table", async () => {
|
||||||
expect(perms.length).toEqual(1)
|
|
||||||
expect(perms[0]._id).toEqual(`${STD_ROLE_ID}`)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should get the resource permissions", async () => {
|
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/permission/${tableId}`)
|
.get(`/api/permission/${tableId}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
|
@ -84,13 +78,13 @@ describe("/permission", () => {
|
||||||
expect(res.body).toEqual({
|
expect(res.body).toEqual({
|
||||||
permissions: {
|
permissions: {
|
||||||
read: { permissionType: "EXPLICIT", role: STD_ROLE_ID },
|
read: { permissionType: "EXPLICIT", role: STD_ROLE_ID },
|
||||||
write: { permissionType: "BASE", role: DEFAULT_TABLE_ROLE_ID },
|
write: { permissionType: "EXPLICIT", role: DEFAULT_TABLE_ROLE_ID },
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should get resource permissions with multiple roles", async () => {
|
it("should get resource permissions with multiple roles", async () => {
|
||||||
perms = await config.api.permission.add({
|
await config.api.permission.add({
|
||||||
roleId: HIGHER_ROLE_ID,
|
roleId: HIGHER_ROLE_ID,
|
||||||
resourceId: tableId,
|
resourceId: tableId,
|
||||||
level: PermissionLevel.WRITE,
|
level: PermissionLevel.WRITE,
|
||||||
|
@ -115,12 +109,12 @@ describe("/permission", () => {
|
||||||
|
|
||||||
describe("remove", () => {
|
describe("remove", () => {
|
||||||
it("should be able to remove the permission", async () => {
|
it("should be able to remove the permission", async () => {
|
||||||
const res = await config.api.permission.revoke({
|
await config.api.permission.revoke({
|
||||||
roleId: STD_ROLE_ID,
|
roleId: STD_ROLE_ID,
|
||||||
resourceId: tableId,
|
resourceId: tableId,
|
||||||
level: PermissionLevel.READ,
|
level: PermissionLevel.READ,
|
||||||
})
|
})
|
||||||
expect(res[0]._id).toEqual(STD_ROLE_ID)
|
|
||||||
const permsRes = await config.api.permission.get(tableId)
|
const permsRes = await config.api.permission.get(tableId)
|
||||||
expect(permsRes.permissions[STD_ROLE_ID]).toBeUndefined()
|
expect(permsRes.permissions[STD_ROLE_ID]).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
|
@ -185,6 +185,26 @@ export async function updatePermissionOnRole(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function setPermissions(
|
||||||
|
resourceId: string,
|
||||||
|
{
|
||||||
|
writeRole,
|
||||||
|
readRole,
|
||||||
|
}: {
|
||||||
|
writeRole: string
|
||||||
|
readRole: string
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
await updatePermissionOnRole(
|
||||||
|
{ roleId: writeRole, resourceId, level: PermissionLevel.WRITE },
|
||||||
|
PermissionUpdateType.ADD
|
||||||
|
)
|
||||||
|
await updatePermissionOnRole(
|
||||||
|
{ roleId: readRole, resourceId, level: PermissionLevel.READ },
|
||||||
|
PermissionUpdateType.ADD
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// utility function to stop this repetition - permissions always stored under roles
|
// utility function to stop this repetition - permissions always stored under roles
|
||||||
export async function getAllDBRoles(db: Database) {
|
export async function getAllDBRoles(db: Database) {
|
||||||
const body = await db.allDocs<Role>(
|
const body = await db.allDocs<Role>(
|
||||||
|
|
|
@ -3,6 +3,8 @@ import { Row, Table } from "@budibase/types"
|
||||||
import * as external from "./external"
|
import * as external from "./external"
|
||||||
import * as internal from "./internal"
|
import * as internal from "./internal"
|
||||||
import { isExternal } from "./utils"
|
import { isExternal } from "./utils"
|
||||||
|
import { setPermissions } from "../permissions"
|
||||||
|
import { roles } from "@budibase/backend-core"
|
||||||
|
|
||||||
export async function create(
|
export async function create(
|
||||||
table: Omit<Table, "_id" | "_rev">,
|
table: Omit<Table, "_id" | "_rev">,
|
||||||
|
@ -15,5 +17,11 @@ export async function create(
|
||||||
} else {
|
} else {
|
||||||
createdTable = await internal.create(table, rows, userId)
|
createdTable = await internal.create(table, rows, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await setPermissions(createdTable._id!, {
|
||||||
|
writeRole: roles.BUILTIN_ROLE_IDS.ADMIN,
|
||||||
|
readRole: roles.BUILTIN_ROLE_IDS.ADMIN,
|
||||||
|
})
|
||||||
|
|
||||||
return createdTable
|
return createdTable
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ export interface AddedPermission {
|
||||||
reason?: string
|
reason?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AddPermissionResponse = AddedPermission[]
|
export interface AddPermissionResponse {}
|
||||||
|
|
||||||
export interface AddPermissionRequest {
|
export interface AddPermissionRequest {
|
||||||
roleId: string
|
roleId: string
|
||||||
|
@ -34,4 +34,4 @@ export interface AddPermissionRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RemovePermissionRequest extends AddPermissionRequest {}
|
export interface RemovePermissionRequest extends AddPermissionRequest {}
|
||||||
export interface RemovePermissionResponse extends AddPermissionResponse {}
|
export interface RemovePermissionResponse {}
|
||||||
|
|
Loading…
Reference in New Issue