-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_admin_users.go
281 lines (251 loc) · 7.9 KB
/
service_admin_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
package keycloak
import (
"context"
"net/http"
"path"
)
type AdminUsersService struct {
c *AdminAPIClient
}
func (c *AdminAPIClient) UsersService() *AdminUsersService {
us := new(AdminUsersService)
us.c = c
return us
}
// List attempts to retrieve a list of users from
func (us *AdminUsersService) List(ctx context.Context, email, firstName, lastName, username, search string, first, max int, mutators ...APIRequestMutator) (Users, error) {
var (
resp *http.Response
err error
users = make(Users, 0)
)
resp, err = us.c.callAdminRealms(
ctx,
http.MethodGet,
kcPathPartUsers,
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
NonZeroQueryMutator("email", email, nil, true),
NonZeroQueryMutator("firstName", firstName, nil, true),
NonZeroQueryMutator("lastName", lastName, nil, true),
NonZeroQueryMutator("username", username, nil, true),
NonZeroQueryMutator("search", search, nil, true),
QueryMutator("first", first, true),
NonZeroQueryMutator("max", max, 20, true),
)...)
if err = handleResponse(resp, http.StatusOK, &users, err); err != nil {
return nil, err
}
return users, nil
}
// Count attempts to get a count of all users currently in a keycloak realm
func (us *AdminUsersService) Count(ctx context.Context, mutators ...APIRequestMutator) (int, error) {
var (
resp *http.Response
count int
err error
)
resp, err = us.c.callAdminRealms(ctx, http.MethodGet, path.Join(kcPathPartUsers, kcPathPartCount), nil, mutators...)
// ok to not check handle response separately as zero-val for int is same whether or not there is an error
return count, handleResponse(resp, http.StatusOK, &count, err)
}
// Get attempts to query for a specific user based on their InstallDocument
func (us *AdminUsersService) Get(ctx context.Context, userID string, mutators ...APIRequestMutator) (*User, error) {
var (
resp *http.Response
err error
user = new(User)
)
resp, err = us.c.callAdminRealms(
ctx,
http.MethodGet,
path.Join(kcPathPartUsers, userID),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, user, err); err != nil {
return nil, err
}
return user, nil
}
// Create attempts to add a user to a keycloak realm
func (us *AdminUsersService) Create(ctx context.Context, user *UserCreate, mutators ...APIRequestMutator) ([]string, error) {
var (
resp *http.Response
err error
)
resp, err = us.c.callAdminRealms(
ctx,
http.MethodPost,
kcPathPartUsers,
user,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
HeaderMutator(httpHeaderContentType, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, nil, err); err != nil {
return nil, err
}
return parseResponseLocations(resp)
}
// Update attempts to push an updated user definition
func (us *AdminUsersService) Update(ctx context.Context, userID string, user *User, mutators ...APIRequestMutator) error {
resp, err := us.c.callAdminRealms(
ctx,
http.MethodPut,
path.Join(kcPathPartUsers, userID),
user,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
HeaderMutator(httpHeaderContentType, httpHeaderValueJSON, true),
)...,
)
return handleResponse(resp, http.StatusOK, nil, err)
}
// Delete attempts to delete a user from the keycloak realm
func (us *AdminUsersService) Delete(ctx context.Context, userID string, mutators ...APIRequestMutator) error {
resp, err := us.c.callAdminRealms(ctx, http.MethodDelete, path.Join(kcPathPartUsers, userID), nil, mutators...)
return handleResponse(resp, http.StatusOK, nil, err)
}
type AdminUserGroupsService struct {
c *AdminAPIClient
userID string
}
func (c *AdminAPIClient) UserGroupsService(userID string) *AdminUserGroupsService {
gs := new(AdminUserGroupsService)
gs.c = c
gs.userID = userID
return gs
}
func (us *AdminUsersService) GroupsService(userID string) *AdminUserGroupsService {
return us.c.UserGroupsService(userID)
}
// List attempts to return the list of groups the provided User is a member of
func (gs *AdminUserGroupsService) List(ctx context.Context, mutators ...APIRequestMutator) (Groups, error) {
var (
resp *http.Response
err error
groups = make(Groups, 0)
)
resp, err = gs.c.callAdminRealms(
ctx,
http.MethodGet,
path.Join(kcPathPartUsers, gs.userID, kcPathPartGroups),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, &groups, err); err != nil {
return nil, err
}
return groups, nil
}
// Add attempts to add the service user to the specified group
func (gs *AdminUserGroupsService) Add(ctx context.Context, groupID string, mutators ...APIRequestMutator) error {
resp, err := gs.c.callAdminRealms(ctx, http.MethodPut, path.Join(kcPathPartUsers, gs.userID, kcPathPartGroups, groupID), nil, mutators...)
return handleResponse(resp, http.StatusOK, nil, err)
}
// Remove attempts to remove the service user from the specified group
func (gs *AdminUserGroupsService) Remove(ctx context.Context, groupID string, mutators ...APIRequestMutator) error {
resp, err := gs.c.callAdminRealms(ctx, http.MethodDelete, path.Join(kcPathPartUsers, gs.userID, kcPathPartGroups, groupID), nil, mutators...)
return handleResponse(resp, http.StatusOK, nil, err)
}
type AdminUserRoleMappingsService struct {
c *AdminAPIClient
userID string
}
func (c *AdminAPIClient) UserRoleMappingsService(userID string) *AdminUserRoleMappingsService {
rms := new(AdminUserRoleMappingsService)
rms.c = c
rms.userID = userID
return rms
}
func (us *AdminUsersService) RoleMappingService(userID string) *AdminUserRoleMappingsService {
return us.c.UserRoleMappingsService(userID)
}
func (rms *AdminUserRoleMappingsService) Get(ctx context.Context, mutators ...APIRequestMutator) (*RoleMapping, error) {
var (
resp *http.Response
err error
roleMapping = new(RoleMapping)
)
resp, err = rms.c.callAdminRealms(
ctx,
http.MethodGet,
path.Join(kcPathPartUsers, rms.userID, kcPathPartRoleMappings),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, roleMapping, err); err != nil {
return nil, err
}
return roleMapping, nil
}
type AdminUserRoleMappingRealmsService struct {
c *AdminAPIClient
userID string
}
func (c *AdminAPIClient) UserRoleMappingRealmsService(userID string) *AdminUserRoleMappingRealmsService {
rms := new(AdminUserRoleMappingRealmsService)
rms.c = c
rms.userID = userID
return rms
}
func (rms *AdminUserRoleMappingsService) RealmsService() *AdminUserRoleMappingRealmsService {
return rms.c.UserRoleMappingRealmsService(rms.userID)
}
func (rms *AdminUserRoleMappingRealmsService) List(ctx context.Context, mutators ...APIRequestMutator) (Roles, error) {
var (
resp *http.Response
err error
roles = make(Roles, 0)
)
resp, err = rms.c.callAdminRealms(
ctx,
http.MethodGet,
path.Join(kcPathPartUsers, rms.userID, kcPathPartRoleMappings, kcPathPartRealm),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, &roles, err); err != nil {
return nil, err
}
return roles, nil
}
func (rms *AdminUserRoleMappingRealmsService) Available(ctx context.Context, mutators ...APIRequestMutator) (Roles, error) {
var (
resp *http.Response
err error
roles = make(Roles, 0)
)
resp, err = rms.c.callAdminRealms(
ctx,
http.MethodGet,
path.Join(kcPathPartUsers, rms.userID, kcPathPartRoleMappings, kcPathPartRealm, kcPathPartAvailable),
nil,
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)...,
)
if err = handleResponse(resp, http.StatusOK, &roles, err); err != nil {
return nil, err
}
return roles, nil
}