forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
280 lines (227 loc) · 6.29 KB
/
user.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gapi
import (
"fmt"
"net/url"
)
// User represents a Grafana user
type User struct {
ID int64 `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Login string `json:"login"`
OrgID int64 `json:"orgId"`
IsAdmin bool `json:"isGrafanaAdmin"` // TODO: handle isAdmin returned from /api/users
Password string `json:"password,omitempty"`
}
// Users is a collection of user models
type Users []*User
// FindByEmail returns the user with the given email from a
// collection of users, and a false if it was not found
func (users Users) FindByEmail(email string) (*User, bool) {
for _, u := range users {
if u.Email == email {
return u, true
}
}
return &User{}, false
}
// FindIndexByEmail is like FindByEmail but it returns the index
func (users Users) FindIndexByEmail(email string) (int, bool) {
for i, u := range users {
if u.Email == email {
return i, true
}
}
return 0, false
}
// FindByLogin returns the user with the given email from a
// collection of users, and a false if it was not found
func (users Users) FindByLogin(login string) (*User, bool) {
for _, u := range users {
if u.Login == login {
return u, true
}
}
return &User{}, false
}
// FindIndexByLogin is like FindByEmail but it returns the index
func (users Users) FindIndexByLogin(login string) (int, bool) {
for i, u := range users {
if u.Login == login {
return i, true
}
}
return 0, false
}
// SwitchOrg will change the current org context for the user
func (u User) SwitchOrg(c *Client, orgID int64) error {
return c.SwitchUserOrg(u.ID, orgID)
}
// MakeGlobalAdmin assigns the user to all orgs with an Admin role
func (u User) MakeGlobalAdmin(c *Client) error {
return u.AddToAllOrgs(c, OrgUserRoleAdmin)
}
// MakeGlobalEditor assigns the user to all orgs with a Editor role
func (u User) MakeGlobalEditor(c *Client) error {
return u.AddToAllOrgs(c, OrgUserRoleEditor)
}
// MakeGlobalViewer assigns the user to all orgs with a Viewer role
func (u User) MakeGlobalViewer(c *Client) error {
return u.AddToAllOrgs(c, OrgUserRoleViewer)
}
// RemoveFromAllOrgs will remove the user from all the orgs that they
// have a current role in
func (u User) RemoveFromAllOrgs(c *Client) error {
orgs, err := c.Orgs()
if err != nil {
return err
}
for _, org := range orgs {
ousers, err := org.Users(c)
if err != nil {
return err
}
u, ok := OrgUsers(ousers).FindByLogin(u.Login)
if !ok {
continue
}
if err := org.RemoveUser(c, u.ID); err != nil {
return err
}
}
return nil
}
// AddToAllOrgs will add the user to all orgs with the given role
func (u User) AddToAllOrgs(c *Client, role string) error {
orgs, err := c.Orgs()
if err != nil {
return err
}
for _, org := range orgs {
err := org.AddUser(c, u.Login, role)
if err != nil && err != ErrConflict {
return err
}
if err != nil && err == ErrConflict {
ousers, err := org.Users(c)
if err != nil {
return err
}
ouser, ok := OrgUsers(ousers).FindByLogin(u.Login)
if !ok {
return fmt.Errorf("Conflict occured while assigning %s to %s, but user is not found in that org", u.Login, org.Name)
}
if err := ouser.UpdateRole(c, role); err != nil {
return fmt.Errorf("unable to update role for %s on %s: %s", u.Login, org.Name, err)
}
}
}
return nil
}
// Users returns all the users from Grafana
func (c *Client) Users() ([]*User, error) {
users := make([]*User, 0)
res, err := c.doRequest("GET", "/api/users", nil)
if err != nil {
return users, err
}
if !res.OK() {
return users, res.Error()
}
err = res.BindJSON(&users)
return users, err
}
// User returns the user with the given id
func (c *Client) User(id int64) (*User, error) {
user := &User{}
res, err := c.doRequest("GET", fmt.Sprintf("/api/users/%d", id), nil)
if err != nil {
return user, err
}
if !res.OK() {
return user, res.Error()
}
err = res.BindJSON(&user)
user.ID = id
return user, err
}
// NewUser is DEPRECATED
func (c *Client) NewUser(u User) error {
return c.CreateUser(u)
}
// CreateUser creates a new user by wrapping the CreateUserForm method to
// avoiding requiring a dependency on Grafana code in your code
func (c *Client) CreateUser(u User) error {
form := AdminCreateUserForm{}
form.Password = u.Password
form.Email = u.Email
form.Name = u.Name
form.Login = u.Login
return c.CreateUserForm(form)
}
// SaveUser will save the given user to the API
func (c *Client) SaveUser(u *User) error {
res, err := c.doRequest("PUT", fmt.Sprintf("/api/users/%d", u.ID), nil)
if err != nil {
return err
}
return res.Error()
}
// SwitchUserOrg will switch the current organisation of the given user ID (via basic auth) to
// the given organisation ID
func (c *Client) SwitchUserOrg(userID, orgID int64) error {
res, err := c.doRequest("POST", fmt.Sprintf("/api/users/%d/using/%d", userID, orgID), nil)
if err != nil {
return err
}
return res.Error()
}
// ActualUser will return the actual user that is logged into the API
func (c *Client) ActualUser() (*User, error) {
user := &User{}
res, err := c.doRequest("GET", "/api/user", nil)
if err != nil {
return user, err
}
if !res.OK() {
return user, res.Error()
}
err = res.BindJSON(&user)
return user, err
}
// SwitchCurrentUserOrg will switch the current organisation of the signed in user
func (c *Client) SwitchCurrentUserOrg(orgID int64) error {
res, err := c.doRequest("POST", fmt.Sprintf("/api/user/using/%d", orgID), nil)
if err != nil {
return err
}
return res.Error()
}
// SetUserAdmin will set the given user ID as an admin
func (c *Client) SetUserAdmin(id int64, admin bool) error {
body := map[string]bool{"isGrafanaAdmin": admin}
res, err := c.doJSONRequest("PUT", fmt.Sprintf("/api/admin/users/%d/permissions", id), body)
if err != nil {
return err
}
return res.Error()
}
// UserByEmail will find a user by their email address
func (c *Client) UserByEmail(email string) (*User, error) {
user := &User{}
values := url.Values{}
values.Set("loginOrEmail", email)
res, err := c.doRequest("GET", "/api/users/lookup?"+values.Encode(), nil)
if err != nil {
return user, err
}
if !res.OK() {
return user, res.Error()
}
err = res.BindJSON(&user)
return user, err
}
// UserByLogin will find a user by their login
func (c *Client) UserByLogin(login string) (*User, error) {
return c.UserByEmail(login)
}