0

Add User or Group to Folder

by
Published today

Grant a user (`u/<username>`) or group (`g/<group>`) access to a folder as viewer, writer or admin. Requires a folder admin or a workspace admin.

Script windmill Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
import * as wmill from "windmill-client"
4

5
// windmill-client errors only carry the status text: throw the response body, which says why
6
wmill.OpenAPI.interceptors.response.use(async (response) => {
7
  if (!response.ok) {
8
    throw new Error(`${response.status} ${await response.text()}`)
9
  }
10
  return response
11
})
12

13
export type DynSelect_name = string
14

15
export async function name() {
16
  const names = await wmill.FolderService.listFolderNames({
17
    workspace: wmill.getWorkspace(),
18
  })
19
  return names.map((n) => ({ value: n, label: n }))
20
}
21

22
/**
23
 * Add User or Group to Folder
24
 * Grant a user (`u/<username>`) or group (`g/<group>`) access to a folder as viewer, writer or admin. Requires a folder admin or a workspace admin.
25
 */
26
export async function main(
27
  name: DynSelect_name,
28
  member: string,
29
  role: "viewer" | "writer" | "admin" = "viewer"
30
) {
31
  const ws = wmill.getWorkspace()
32
  if (role === "admin") {
33
    return await wmill.FolderService.addOwnerToFolder({
34
      workspace: ws,
35
      name,
36
      requestBody: { owner: member },
37
    })
38
  }
39
  return await wmill.GranularAclService.addGranularAcls({
40
    workspace: ws,
41
    kind: "folder",
42
    path: name,
43
    requestBody: { owner: member, write: role === "writer" },
44
  })
45
}
46