Skip to content

Commit 6562121

Browse files
authored
user password (#219)
* user password * add password integration test * password not return bug
1 parent 53350f3 commit 6562121

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

rest-admin.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *Client) DeleteUser(ctx context.Context, uid uint) (StatusMessage, error
4747

4848
// UpdateUserPermissions updates the permissions of a global user.
4949
// Requires basic authentication and that the authenticated user is a Grafana Admin.
50-
// Reflects PUT /api/admin/users/:userId/password API call.
50+
// Reflects PUT /api/admin/users/:userId/permissions API call.
5151
func (r *Client) UpdateUserPermissions(ctx context.Context, permissions UserPermissions, uid uint) (StatusMessage, error) {
5252
var (
5353
raw []byte
@@ -82,3 +82,22 @@ func (r *Client) SwitchUserContext(ctx context.Context, uid uint, oid uint) (Sta
8282
}
8383
return resp, nil
8484
}
85+
86+
// UpdateUserPassword updates the password of a global user.
87+
// Requires basic authentication and that the authenticated user is a Grafana Admin.
88+
// Reflects PUT /api/admin/users/:userId/password API call.
89+
func (r *Client) UpdateUserPassword(ctx context.Context, password UserPassword, uid uint) (StatusMessage, error) {
90+
var (
91+
raw []byte
92+
reply StatusMessage
93+
err error
94+
)
95+
if raw, err = json.Marshal(password); err != nil {
96+
return StatusMessage{}, err
97+
}
98+
if raw, _, err = r.put(ctx, fmt.Sprintf("api/admin/users/%d/password", uid), nil, raw); err != nil {
99+
return StatusMessage{}, err
100+
}
101+
err = json.Unmarshal(raw, &reply)
102+
return reply, err
103+
}

rest-admin_integration_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package sdk_test
22

33
import (
44
"context"
5-
"github.com/stretchr/testify/assert"
65
"testing"
76

7+
"github.com/stretchr/testify/assert"
8+
89
"github.com/grafana-tools/sdk"
910
)
1011

@@ -53,6 +54,10 @@ func TestAdminOperations(t *testing.T) {
5354
if retrievedUser.IsGrafanaAdmin != true {
5455
t.Fatal("user should be an admin but is not")
5556
}
57+
statusMessage, err := client.UpdateUserPassword(ctx, sdk.UserPassword{Password: "123456"}, uid)
58+
if err != nil || *statusMessage.Message != "User password updated" {
59+
t.Fatalf("failed to change the user's password")
60+
}
5661
//Delete user
5762
msg, err := client.DeleteUser(ctx, retrievedUser.ID)
5863
assert.Nil(t, err)

user.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ type PageUsers struct {
4949
Page int `json:"page"`
5050
PerPage int `json:"perPage"`
5151
}
52+
53+
type UserPassword struct {
54+
Password string `json:"password"`
55+
}

0 commit comments

Comments
 (0)