-
Notifications
You must be signed in to change notification settings - Fork 5
/
users.go
205 lines (186 loc) · 4.98 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
package oneandone
import "net/http"
type User struct {
Identity
descField
CreationDate string `json:"creation_date,omitempty"`
Email string `json:"email,omitempty"`
State string `json:"state,omitempty"`
Role *Identity `json:"role,omitempty"`
Api *UserApi `json:"api,omitempty"`
ApiPtr
}
type UserApi struct {
Active bool `json:"active"`
AllowedIps []string `json:"allowed_ips,omitempty"`
UserApiKey
ApiPtr
}
type UserApiKey struct {
Key string `json:"key,omitempty"`
}
type UserRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Password string `json:"password,omitempty"`
Email string `json:"email,omitempty"`
State string `json:"state,omitempty"`
}
// GET /users
func (api *API) ListUsers(args ...interface{}) ([]User, error) {
url, err := processQueryParams(createUrl(api, userPathSegment), args...)
if err != nil {
return nil, err
}
result := []User{}
err = api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
for index, _ := range result {
result[index].api = api
}
return result, nil
}
// POST /users
func (api *API) CreateUser(user *UserRequest) (string, *User, error) {
result := new(User)
url := createUrl(api, userPathSegment)
err := api.Client.Post(url, &user, &result, http.StatusCreated)
if err != nil {
return "", nil, err
}
result.api = api
return result.Id, result, nil
}
// GET /users/{id}
func (api *API) GetUser(user_id string) (*User, error) {
result := new(User)
url := createUrl(api, userPathSegment, user_id)
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// DELETE /users/{id}
func (api *API) DeleteUser(user_id string) (*User, error) {
result := new(User)
url := createUrl(api, userPathSegment, user_id)
err := api.Client.Delete(url, nil, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// PUT /users/{id}
func (api *API) ModifyUser(user_id string, user *UserRequest) (*User, error) {
result := new(User)
url := createUrl(api, userPathSegment, user_id)
err := api.Client.Put(url, &user, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /users/{id}/api
func (api *API) GetUserApi(user_id string) (*UserApi, error) {
result := new(UserApi)
url := createUrl(api, userPathSegment, user_id, "api")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// PUT /users/{id}/api
func (api *API) ModifyUserApi(user_id string, active bool) (*User, error) {
result := new(User)
req := struct {
Active bool `json:"active"`
}{active}
url := createUrl(api, userPathSegment, user_id, "api")
err := api.Client.Put(url, &req, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /users/{id}/api/key
func (api *API) GetUserApiKey(user_id string) (*UserApiKey, error) {
result := new(UserApiKey)
url := createUrl(api, userPathSegment, user_id, "api/key")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// PUT /users/{id}/api/key
func (api *API) RenewUserApiKey(user_id string) (*User, error) {
result := new(User)
url := createUrl(api, userPathSegment, user_id, "api/key")
err := api.Client.Put(url, nil, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /users/{id}/api/ips
func (api *API) ListUserApiAllowedIps(user_id string) ([]string, error) {
result := []string{}
url := createUrl(api, userPathSegment, user_id, "api/ips")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
// POST /users/{id}/api/ips
func (api *API) AddUserApiAlowedIps(user_id string, ips []string) (*User, error) {
result := new(User)
req := struct {
Ips []string `json:"ips"`
}{ips}
url := createUrl(api, userPathSegment, user_id, "api/ips")
err := api.Client.Post(url, &req, &result, http.StatusCreated)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// DELETE /users/{id}/api/ips/{ip}
func (api *API) RemoveUserApiAllowedIp(user_id string, ip string) (*User, error) {
result := new(User)
url := createUrl(api, userPathSegment, user_id, "api/ips", ip)
err := api.Client.Delete(url, nil, &result, http.StatusOK)
if err != nil {
return nil, err
}
result.api = api
return result, nil
}
// GET /users/{id}/api/ips
func (api *API) GetCurrentUserPermissions() (*Permissions, error) {
result := new(Permissions)
url := createUrl(api, userPathSegment, "current_user_permissions")
err := api.Client.Get(url, &result, http.StatusOK)
if err != nil {
return nil, err
}
return result, nil
}
func (u *User) GetState() (string, error) {
in, err := u.api.GetUser(u.Id)
if in == nil {
return "", err
}
return in.State, err
}