Skip to content

Commit 8a2ef07

Browse files
UpdateUserPermissions function
1 parent 96f0664 commit 8a2ef07

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

admin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ func (c *Client) UpdateUserPassword(id int64, password string) error {
4848
return c.request("PUT", fmt.Sprintf("/api/admin/users/%d/password", id), nil, bytes.NewBuffer(data), nil)
4949
}
5050

51+
// UpdateUserPermissions sets a user's admin status.
52+
func (c *Client) UpdateUserPermissions(id int64, isAdmin bool) error {
53+
body := map[string]bool{"isGrafanaAdmin": isAdmin}
54+
data, err := json.Marshal(body)
55+
if err != nil {
56+
return err
57+
}
58+
return c.request("PUT", fmt.Sprintf("/api/admin/users/%d/permissions", id), nil, bytes.NewBuffer(data), nil)
59+
}
60+
5161
// PauseAllAlerts pauses all Grafana alerts.
5262
func (c *Client) PauseAllAlerts() (PauseAllAlertsResponse, error) {
5363
result := PauseAllAlertsResponse{}

admin_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
)
99

1010
const (
11-
createUserJSON = `{"id":1,"message":"User created"}`
12-
deleteUserJSON = `{"message":"User deleted"}`
13-
updateUserPasswordJSON = `{"message":"User password updated"}`
11+
createUserJSON = `{"id":1,"message":"User created"}`
12+
deleteUserJSON = `{"message":"User deleted"}`
13+
updateUserPasswordJSON = `{"message":"User password updated"}`
14+
updateUserPermissionsJSON = `{"message":"User permissions updated"}`
1415

1516
pauseAllAlertsJSON = `{
1617
"alertsAffected": 1,
@@ -58,6 +59,16 @@ func TestUpdateUserPassword(t *testing.T) {
5859
}
5960
}
6061

62+
func TestUpdateUserPermissions(t *testing.T) {
63+
server, client := gapiTestTools(200, updateUserPermissionsJSON)
64+
defer server.Close()
65+
66+
err := client.UpdateUserPermissions(int64(1), false)
67+
if err != nil {
68+
t.Error(err)
69+
}
70+
}
71+
6172
func TestPauseAllAlerts(t *testing.T) {
6273
server, client := gapiTestTools(200, pauseAllAlertsJSON)
6374
defer server.Close()

0 commit comments

Comments
 (0)