forked from mittwald/goharbor-client
/
user.go
313 lines (247 loc) · 8.36 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
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
package user
import (
"context"
"errors"
"k8s.io/apimachinery/pkg/util/intstr"
v2client "github.com/testwill/goharbor-client/v5/apiv2/internal/api/client"
"github.com/testwill/goharbor-client/v5/apiv2/internal/api/client/user"
modelv2 "github.com/testwill/goharbor-client/v5/apiv2/model"
"github.com/testwill/goharbor-client/v5/apiv2/pkg/config"
clienterrors "github.com/testwill/goharbor-client/v5/apiv2/pkg/errors"
"github.com/go-openapi/runtime"
)
// RESTClient is a subclient for handling user related actions.
type RESTClient struct {
// The new client of the harbor v2 API
V2Client *v2client.Harbor
// Options contains optional configuration when making API calls.
Options *config.Options
// AuthInfo contains the auth information that is provided on API calls.
AuthInfo runtime.ClientAuthInfoWriter
}
func NewClient(v2Client *v2client.Harbor, opts *config.Options, authInfo runtime.ClientAuthInfoWriter) *RESTClient {
return &RESTClient{
Options: opts,
V2Client: v2Client,
AuthInfo: authInfo,
}
}
type Client interface {
NewUser(ctx context.Context, username, email, realname, password, comments string) error
GetUserByName(ctx context.Context, username string) (*modelv2.UserResp, error)
GetUserByID(ctx context.Context, id int64) (*modelv2.UserResp, error)
ListUsers(ctx context.Context) ([]*modelv2.UserResp, error)
SearchUsers(ctx context.Context, name string) ([]*modelv2.UserSearchRespItem, error)
GetCurrentUserInfo(ctx context.Context) (*modelv2.UserResp, error)
GetCurrentUserPermisisons(ctx context.Context, relative bool, scope string) ([]*modelv2.Permission, error)
SetUserSysAdmin(ctx context.Context, id int64, admin bool) error
DeleteUser(ctx context.Context, id int64) error
UpdateUserProfile(ctx context.Context, id int64, profile *modelv2.UserProfile) error
UpdateUserPassword(ctx context.Context, userID int64, passwordRequest *modelv2.PasswordReq) error
UserExists(ctx context.Context, idOrName intstr.IntOrString) (bool, error)
}
// NewUser creates a new user with the provided values.
func (c *RESTClient) NewUser(ctx context.Context, username, email, realname, password, comments string) error {
params := &user.CreateUserParams{
UserReq: &modelv2.UserCreationReq{
Username: username,
Password: password,
Email: email,
Realname: realname,
Comment: comments,
},
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.User.CreateUser(params, c.AuthInfo)
if err != nil {
return handleSwaggerUserErrors(err)
}
return nil
}
// GetUserByName returns an existing user identified by name.
func (c *RESTClient) GetUserByName(ctx context.Context, username string) (*modelv2.UserResp, error) {
if username == "" {
return nil, errors.New("no username provided")
}
c.Options.PageSize = 100
resp, err := c.ListUsers(ctx)
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
for _, u := range resp {
if u.Username == username {
return u, nil
}
}
return nil, &clienterrors.ErrUserNotFound{}
}
// GetUserByID returns an existing user identified by ID.
func (c *RESTClient) GetUserByID(ctx context.Context, id int64) (*modelv2.UserResp, error) {
if id <= 0 {
return nil, &clienterrors.ErrUserInvalidID{}
}
c.Options.PageSize = 100
params := &user.GetUserParams{
UserID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.User.GetUser(params, c.AuthInfo)
if resp.Payload == nil {
return nil, &clienterrors.ErrUserNotFound{}
}
if resp.Payload.UserID != id {
return nil, &clienterrors.ErrUserInvalidID{}
}
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
return resp.Payload, nil
}
// ListUsers lists and returns all registered Harbor users.
// The maximum number of users listed is bound to the RESTClient's configured PageSize.
func (c *RESTClient) ListUsers(ctx context.Context) ([]*modelv2.UserResp, error) {
params := user.ListUsersParams{
PageSize: &c.Options.PageSize,
Q: &c.Options.Query,
Sort: &c.Options.Sort,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.User.ListUsers(¶ms, c.AuthInfo)
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
return resp.Payload, nil
}
// SearchUsers searches all existing users by the provided username 'name' and returns matching users.
func (c *RESTClient) SearchUsers(ctx context.Context, name string) ([]*modelv2.UserSearchRespItem, error) {
params := &user.SearchUsersParams{
PageSize: &c.Options.PageSize,
Username: name,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.User.SearchUsers(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
if len(resp.Payload) == 0 {
return nil, &clienterrors.ErrUserNotFound{}
}
return resp.Payload, nil
}
// GetCurrentUserInfo returns information of currently active user.
func (c *RESTClient) GetCurrentUserInfo(ctx context.Context) (*modelv2.UserResp, error) {
params := &user.GetCurrentUserInfoParams{
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.User.GetCurrentUserInfo(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
return resp.Payload, nil
}
// GetCurrentUserPermisisons returns the permissions of the currently active user.
func (c *RESTClient) GetCurrentUserPermisisons(ctx context.Context, relative bool, scope string) ([]*modelv2.Permission, error) {
params := &user.GetCurrentUserPermissionsParams{
Relative: &relative,
Scope: &scope,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.User.GetCurrentUserPermissions(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerUserErrors(err)
}
return resp.Payload, nil
}
// SetUserSysAdmin updates a user's administrator privileges.
func (c *RESTClient) SetUserSysAdmin(ctx context.Context, id int64, admin bool) error {
params := &user.SetUserSysAdminParams{
SysadminFlag: &modelv2.UserSysAdminFlag{
SysadminFlag: admin,
},
UserID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.User.SetUserSysAdmin(params, c.AuthInfo)
return handleSwaggerUserErrors(err)
}
// DeleteUser deletes the specified user, first ensuring its existence.
func (c *RESTClient) DeleteUser(ctx context.Context, id int64) error {
params := &user.DeleteUserParams{
UserID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.GetUserByID(ctx, id)
if err != nil {
return err
}
_, err = c.V2Client.User.DeleteUser(params, c.AuthInfo)
return handleSwaggerUserErrors(err)
}
// UpdateUserProfile updates a user identified by id with the specified profile data.
func (c *RESTClient) UpdateUserProfile(ctx context.Context, id int64, profile *modelv2.UserProfile) error {
_, err := c.GetUserByID(ctx, id)
if err != nil {
return err
}
params := &user.UpdateUserProfileParams{
Profile: profile,
UserID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err = c.V2Client.User.UpdateUserProfile(params, c.AuthInfo)
return handleSwaggerUserErrors(err)
}
// UpdateUserPassword updates a user's password from 'old' to 'new'.
// 'old' is an optional parameter when called by an administrator.
func (c *RESTClient) UpdateUserPassword(ctx context.Context, userID int64, passwordRequest *modelv2.PasswordReq) error {
if passwordRequest.NewPassword == "" {
return errors.New("no new password provided")
}
_, err := c.GetUserByID(ctx, userID)
if err != nil {
return err
}
params := &user.UpdateUserPasswordParams{
Password: passwordRequest,
UserID: userID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err = c.V2Client.User.UpdateUserPassword(params, c.AuthInfo)
return handleSwaggerUserErrors(err)
}
// UserExists checks the user with the provided 'idOrName' for existence.
func (c *RESTClient) UserExists(ctx context.Context, idOrName intstr.IntOrString) (bool, error) {
switch idOrName.Type {
default:
return false, nil
case intstr.Int:
if idOrName.Type == intstr.Int {
_, err := c.GetUserByID(ctx, int64(idOrName.IntVal))
if err != nil {
if _, ok := err.(*clienterrors.ErrUserNotFound); ok {
return false, nil
}
return false, err
}
}
case intstr.String:
_, err := c.GetUserByName(ctx, idOrName.StrVal)
if err != nil {
if _, ok := err.(*clienterrors.ErrUserNotFound); ok {
return false, nil
}
return false, err
}
}
return true, nil
}