-
Notifications
You must be signed in to change notification settings - Fork 5
/
users.go
447 lines (354 loc) · 10.6 KB
/
users.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package redmine
import (
"net/http"
"net/url"
"strconv"
"strings"
)
// UserStatus defines user status type
type UserStatus int64
// UserNotification defines user notification type
type UserNotification string
type UserInclude string
// UserStatus const
const (
UserStatusAnonymous UserStatus = 0
UserStatusActive UserStatus = 1
UserStatusRegistered UserStatus = 2
UserStatusLocked UserStatus = 3
)
// UserNotification const
const (
UserNotificationAll UserNotification = "all"
UserNotificationSelected UserNotification = "selected"
UserNotificationOnlyMyEvents UserNotification = "only_my_events"
UserNotificationOnlyAssigned UserNotification = "only_assigned"
UserNotificationOnlyOwner UserNotification = "only_owner"
UserNotificationOnlyNone UserNotification = "none"
)
const (
UserIncludeGroups UserInclude = "groups"
UserIncludeMemberships UserInclude = "memberships"
)
/* Get */
// UserObject struct used for users get operations
type UserObject struct {
ID int64 `json:"id"`
Login string `json:"login"`
Admin bool `json:"admin"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Mail string `json:"mail"`
CreatedOn string `json:"created_on"`
LastLoginOn string `json:"last_login_on"`
PasswdChangedOn string `json:"passwd_changed_on"`
TwofaScheme *string `json:"twofa_scheme"` // has nil value if 2FA not enabled and "totp" string value otherwise
APIKey *string `json:"api_key"` // used only: get single user
Status *UserStatus `json:"status"` // used only: get single user
CustomFields []CustomFieldGetObject `json:"custom_fields"`
Groups *[]IDName `json:"groups"` // used only: get single user and include specified
Memberships *[]UserMembershipObject `json:"memberships"` // used only: get single user and include specified
}
// UserMembershipObject struct used for users get operations
type UserMembershipObject struct {
ID int64 `json:"id"`
Project IDName `json:"project"`
Roles []IDName `json:"roles"`
}
/* Create */
// UserCreate struct used for users create operations
type UserCreate struct {
User UserCreateObject `json:"user"`
SendInformation *bool `json:"send_information,omitempty"`
}
type UserCreateObject struct {
Login string `json:"login"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Mail string `json:"mail"`
Password *string `json:"password,omitempty"`
AuthSourceID *int64 `json:"auth_source_id,omitempty"`
MailNotification *string `json:"mail_notification,omitempty"`
MustChangePasswd *bool `json:"must_change_passwd,omitempty"`
GeneratePassword *bool `json:"generate_password,omitempty"`
CustomFields *[]CustomFieldUpdateObject `json:"custom_fields,omitempty"`
}
/* Update */
// UserUpdate struct used for users update operations
type UserUpdate struct {
User UserUpdateObject `json:"user"`
SendInformation *bool `json:"send_information,omitempty"`
}
type UserUpdateObject struct {
Login *string `json:"login,omitempty"`
FirstName *string `json:"firstname,omitempty"`
LastName *string `json:"lastname,omitempty"`
Mail *string `json:"mail,omitempty"`
Password *string `json:"password,omitempty"`
AuthSourceID *int64 `json:"auth_source_id,omitempty"`
MailNotification *string `json:"mail_notification,omitempty"`
MustChangePasswd *bool `json:"must_change_passwd,omitempty"`
GeneratePassword *bool `json:"generate_password,omitempty"`
CustomFields *[]CustomFieldUpdateObject `json:"custom_fields,omitempty"`
}
/* Requests */
// UserAllGetRequest contains data for making request to get all users satisfying specified filters
type UserAllGetRequest struct {
Filters *UserGetRequestFilters
}
// UserMultiGetRequest contains data for making request to get limited users count satisfying specified filters
type UserMultiGetRequest struct {
Filters *UserGetRequestFilters
Offset int64
Limit int64
}
// UserSingleGetRequest contains data for making request to get specified user
type UserSingleGetRequest struct {
Includes []UserInclude
}
// UserCurrentGetRequest contains data for making request to get current user
type UserCurrentGetRequest struct {
Includes []UserInclude
}
// UserGetRequestFilters contains data for making users get request
type UserGetRequestFilters struct {
status *UserStatus
name *string
groupID *int64
}
/* Results */
// UserResult stores users requests processing result
type UserResult struct {
Users []UserObject `json:"users"`
TotalCount int64 `json:"total_count"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
/* Internal types */
type userSingleResult struct {
User UserObject `json:"user"`
}
func (u UserStatus) String() string {
status := map[UserStatus]string{
UserStatusAnonymous: "anonymous",
UserStatusActive: "active",
UserStatusRegistered: "registered",
UserStatusLocked: "locked",
}
s, b := status[u]
if b == false {
return "unknown"
}
return s
}
func (u UserNotification) String() string {
return string(u)
}
func (ui UserInclude) String() string {
return string(ui)
}
// UserAllGet gets info for all users satisfying specified filters
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#GET
func (r *Context) UserAllGet(request UserAllGetRequest) (UserResult, StatusCode, error) {
var (
users UserResult
offset int64
status StatusCode
)
up := request.url()
up.Set("limit", strconv.FormatInt(limitDefault, 10))
for {
var u UserResult
// m.Offset = offset
up.Set("offset", strconv.FormatInt(offset, 10))
s, err := r.Get(
&u,
url.URL{
Path: "/users.json",
RawQuery: up.Encode(),
},
http.StatusOK,
)
if err != nil {
return users, s, err
}
status = s
users.Users = append(users.Users, u.Users...)
if offset+u.Limit >= u.TotalCount {
users.TotalCount = u.TotalCount
users.Limit = u.TotalCount
break
}
offset += u.Limit
}
return users, status, nil
}
// UserMultiGet gets info for multiple users satisfying specified filters
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#GET
func (r *Context) UserMultiGet(request UserMultiGetRequest) (UserResult, StatusCode, error) {
var u UserResult
s, err := r.Get(
&u,
url.URL{
Path: "/users.json",
RawQuery: request.url().Encode(),
},
http.StatusOK,
)
return u, s, err
}
// UserSingleGet gets single user info by specific ID
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#GET-2
func (r *Context) UserSingleGet(id int64, request UserSingleGetRequest) (UserObject, StatusCode, error) {
var u userSingleResult
status, err := r.Get(
&u,
url.URL{
Path: "/users/" + strconv.FormatInt(id, 10) + ".json",
RawQuery: request.url().Encode(),
},
http.StatusOK,
)
return u.User, status, err
}
// UserCurrentGet gets current user info
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#GET-2
func (r *Context) UserCurrentGet(request UserCurrentGetRequest) (UserObject, StatusCode, error) {
var u userSingleResult
status, err := r.Get(
&u,
url.URL{
Path: "/users/current.json",
RawQuery: request.url().Encode(),
},
http.StatusOK,
)
return u.User, status, err
}
// UserCreate creates new user
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#POST
func (r *Context) UserCreate(user UserCreate) (UserObject, StatusCode, error) {
var u userSingleResult
status, err := r.Post(
user,
&u,
url.URL{
Path: "/users.json",
},
http.StatusCreated,
)
return u.User, status, err
}
// UserUpdate updates user with specified ID
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#PUT
func (r *Context) UserUpdate(id int64, user UserUpdate) (StatusCode, error) {
status, err := r.Put(
user,
nil,
url.URL{
Path: "/users/" + strconv.FormatInt(id, 10) + ".json",
},
http.StatusNoContent,
)
return status, err
}
// UserDelete deletes user with specified ID
//
// see: https://www.redmine.org/projects/redmine/wiki/Rest_Users#DELETE
func (r *Context) UserDelete(id int64) (StatusCode, error) {
status, err := r.Del(
nil,
nil,
url.URL{
Path: "/users/" + strconv.FormatInt(id, 10) + ".json",
},
http.StatusNoContent,
)
return status, err
}
func (ur UserAllGetRequest) url() url.Values {
v := url.Values{}
if ur.Filters != nil {
ur.Filters.url(&v)
}
return v
}
func (ur UserMultiGetRequest) url() url.Values {
v := url.Values{}
if ur.Filters != nil {
ur.Filters.url(&v)
}
v.Set("offset", strconv.FormatInt(ur.Offset, 10))
v.Set("limit", strconv.FormatInt(ur.Limit, 10))
return v
}
func (ur UserSingleGetRequest) url() url.Values {
v := url.Values{}
if len(ur.Includes) > 0 {
v.Set(
"include",
strings.Join(
func() []string {
var is []string
for _, i := range ur.Includes {
is = append(is, i.String())
}
return is
}(),
",",
),
)
}
return v
}
func (ur UserCurrentGetRequest) url() url.Values {
v := url.Values{}
if len(ur.Includes) > 0 {
v.Set(
"include",
strings.Join(
func() []string {
var is []string
for _, i := range ur.Includes {
is = append(is, i.String())
}
return is
}(),
",",
),
)
}
return v
}
func UserGetRequestFiltersInit() *UserGetRequestFilters {
return &UserGetRequestFilters{}
}
func (f *UserGetRequestFilters) StatusSet(s UserStatus) *UserGetRequestFilters {
f.status = &s
return f
}
func (f *UserGetRequestFilters) NameSet(n string) *UserGetRequestFilters {
f.name = &n
return f
}
func (f *UserGetRequestFilters) GroupIDSet(g int64) *UserGetRequestFilters {
f.groupID = &g
return f
}
func (f *UserGetRequestFilters) url(v *url.Values) {
if f.status != nil {
v.Set("status", strconv.FormatInt(int64(*f.status), 10))
}
if f.name != nil {
v.Set("name", *f.name)
}
if f.groupID != nil {
v.Set("group_id", strconv.FormatInt(*f.groupID, 10))
}
}