-
Notifications
You must be signed in to change notification settings - Fork 15
/
user.go
180 lines (144 loc) · 5.02 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
package zabbix
// For `UserObject` field: `AutoLogin`
const (
UserAutoLoginDisabled = 0
UserAutoLoginEnabled = 1
)
// For `UserObject` field: `Theme`
const (
UserThemeDefault = "default"
UserThemeBlue = "blue-theme"
UserThemeDark = "dark-theme"
)
// For `UserObject` field: `Type`
const (
UserTypeUser = 1
UserTypeAdmin = 2
UserTypeSuperAdmin = 3
)
// For `MediaObject` field: `Active`
const (
MediaActiveEnabled = 0
MediaActiveDisabled = 1
)
// UserObject struct is used to store user operations results
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/object
type UserObject struct {
UserID int `json:"userid,omitempty"`
Alias string `json:"alias,omitempty"`
AttemptClock int `json:"attempt_clock,omitempty"`
AttemptFailed int `json:"attempt_failed,omitempty"`
AttemptIP string `json:"attempt_ip,omitempty"`
AutoLogin int `json:"autologin,omitempty"` // has defined consts, see above
AutoLogout string `json:"autologout"`
Lang string `json:"lang,omitempty"`
Name string `json:"name,omitempty"`
Refresh string `json:"refresh,omitempty"`
RowsPerPage int `json:"rows_per_page,omitempty"`
Surname string `json:"surname,omitempty"`
Theme string `json:"theme,omitempty"` // has defined consts, see above
Type int `json:"type,omitempty"` // has defined consts, see above
URL string `json:"url,omitempty"`
// used for user.login
UserDataObject
Medias []MediaObject `json:"medias,omitempty"`
Mediatypes []MediatypeObject `json:"mediatypes,omitempty"`
Usrgrps []UsergroupObject `json:"usrgrps,omitempty"`
// used when new user created
UserMedias []MediaObject `json:"user_medias,omitempty"`
Passwd string `json:"passwd,omitempty"`
}
// MediaObject struct is used to store media operations results
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/object#media
type MediaObject struct {
MediaID int `json:"mediaid,omitempty"`
MediaTypeID int `json:"mediatypeid,omitempty"`
SendTo []string `json:"sendto,omitempty"`
Active int `json:"active,omitempty"` // has defined consts, see above
Severity int `json:"severity,omitempty"`
Period string `json:"period,omitempty"`
}
// UserLoginParams struct is used for login requests
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/login#parameters
type UserLoginParams struct {
User string `json:"user"`
Password string `json:"password"`
UserData string `json:"userData,omitempty"`
}
// UserDataObject struct is used to store authenticated user additional info
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/login#return_values
type UserDataObject struct {
DebugMode bool `json:"debug_mode,omitempty"`
GUIAccess int `json:"gui_access,omitempty"`
SessionID string `json:"sessionid,omitempty"`
UserIP string `json:"userip,omitempty"`
}
// UserGetParams struct is used for user get requests
//
// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/get#parameters
type UserGetParams struct {
GetParameters
MediaIDs []int `json:"mediaids,omitempty"`
NediatypeIDs []int `json:"mediatypeids,omitempty"`
UserIDs []int `json:"userids,omitempty"`
UsrgrpIDs []int `json:"usrgrpids,omitempty"`
GetAccess bool `json:"getAccess,omitempty"`
SelectMedias SelectQuery `json:"selectMedias,omitempty"`
SelectMediatypes SelectQuery `json:"selectMediatypes,omitempty"`
SelectUsrgrps SelectQuery `json:"selectUsrgrps,omitempty"`
}
// Structure to store creation result
type userCreateResult struct {
UserIDs []int `json:"userids"`
}
// Structure to store deletion result
type userDeleteResult struct {
UserIDs []int `json:"userids"`
}
// UserGet gets users
func (z *Context) UserGet(params UserGetParams) ([]UserObject, int, error) {
var result []UserObject
status, err := z.request("user.get", params, &result)
if err != nil {
return nil, status, err
}
return result, status, nil
}
// UserCreate creates users
func (z *Context) UserCreate(params []UserObject) ([]int, int, error) {
var result userCreateResult
status, err := z.request("user.create", params, &result)
if err != nil {
return nil, status, err
}
return result.UserIDs, status, nil
}
// UserDelete deletes users
func (z *Context) UserDelete(userIDs []int) ([]int, int, error) {
var result userDeleteResult
status, err := z.request("user.delete", userIDs, &result)
if err != nil {
return nil, status, err
}
return result.UserIDs, status, nil
}
func (z *Context) userLogin(params UserLoginParams) (string, int, error) {
var result string
status, err := z.request("user.login", params, &result)
if err != nil {
return "", status, err
}
return result, status, nil
}
func (z *Context) userLogout() (bool, int, error) {
var result bool
status, err := z.request("user.logout", []string{}, &result)
if err != nil {
return false, status, err
}
return result, status, nil
}