This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathdashboard_permissions.go
113 lines (99 loc) · 3.69 KB
/
dashboard_permissions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gapi
import (
"encoding/json"
"fmt"
)
// DashboardPermission has information such as a dashboard, user, team, role and permission.
type DashboardPermission struct {
DashboardID int64 `json:"dashboardId"`
DashboardUID string `json:"uid"`
UserID int64 `json:"userId"`
TeamID int64 `json:"teamId"`
Role string `json:"role"`
IsFolder bool `json:"isFolder"`
Inherited bool `json:"inherited"`
// Permission levels are
// 1 = View
// 2 = Edit
// 4 = Admin
Permission int64 `json:"permission"`
PermissionName string `json:"permissionName"`
}
// DashboardPermissions fetches and returns the permissions for the dashboard whose ID it's passed.
func (c *Client) DashboardPermissions(id int64) ([]*DashboardPermission, error) {
permissions := make([]*DashboardPermission, 0)
err := c.request("GET", fmt.Sprintf("/api/dashboards/id/%d/permissions", id), nil, nil, &permissions)
return permissions, err
}
// UpdateDashboardPermissions remove existing permissions if items are not included in the request.
func (c *Client) UpdateDashboardPermissions(id int64, items *PermissionItems) error {
path := fmt.Sprintf("/api/dashboards/id/%d/permissions", id)
data, err := json.Marshal(items)
if err != nil {
return err
}
return c.request("POST", path, nil, data, nil)
}
// DashboardPermissionsByUID fetches and returns the permissions for the dashboard whose UID it's passed.
func (c *Client) DashboardPermissionsByUID(uid string) ([]*DashboardPermission, error) {
permissions := make([]*DashboardPermission, 0)
err := c.request("GET", fmt.Sprintf("/api/dashboards/uid/%s/permissions", uid), nil, nil, &permissions)
return permissions, err
}
// UpdateDashboardPermissionsByUID remove existing permissions if items are not included in the request.
func (c *Client) UpdateDashboardPermissionsByUID(uid string, items *PermissionItems) error {
path := fmt.Sprintf("/api/dashboards/uid/%s/permissions", uid)
data, err := json.Marshal(items)
if err != nil {
return err
}
return c.request("POST", path, nil, data, nil)
}
func (c *Client) ListDashboardResourcePermissions(uid string) ([]*ResourcePermission, error) {
return c.listResourcePermissions(DashboardsResource, ResourceUID(uid))
}
func (c *Client) SetDashboardResourcePermissions(uid string, body SetResourcePermissionsBody) (*SetResourcePermissionsResponse, error) {
return c.setResourcePermissions(DashboardsResource, ResourceUID(uid), body)
}
func (c *Client) SetUserDashboardResourcePermissions(dashboardUID string, userID int64, permission string) (*SetResourcePermissionsResponse, error) {
return c.setResourcePermissionByAssignment(
DashboardsResource,
ResourceUID(dashboardUID),
UsersResource,
ResourceID(userID),
SetResourcePermissionBody{
Permission: SetResourcePermissionItem{
UserID: userID,
Permission: permission,
},
},
)
}
func (c *Client) SetTeamDashboardResourcePermissions(dashboardUID string, teamID int64, permission string) (*SetResourcePermissionsResponse, error) {
return c.setResourcePermissionByAssignment(
DashboardsResource,
ResourceUID(dashboardUID),
TeamsResource,
ResourceID(teamID),
SetResourcePermissionBody{
Permission: SetResourcePermissionItem{
TeamID: teamID,
Permission: permission,
},
},
)
}
func (c *Client) SetBuiltInRoleDashboardResourcePermissions(dashboardUID string, builtInRole string, permission string) (*SetResourcePermissionsResponse, error) {
return c.setResourcePermissionByAssignment(
DashboardsResource,
ResourceUID(dashboardUID),
BuiltInRolesResource,
ResourceUID(builtInRole),
SetResourcePermissionBody{
Permission: SetResourcePermissionItem{
BuiltinRole: builtInRole,
Permission: permission,
},
},
)
}