-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_users.go
588 lines (520 loc) · 14.7 KB
/
command_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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/1and1/soma/internal/help"
"github.com/1and1/soma/internal/adm"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/auth"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerUsers(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
// users
{
Name: "users",
Usage: "SUBCOMMANDS for users",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a new user",
Action: runtime(cmdUserAdd),
BashComplete: cmpl.UserAdd,
},
{
Name: "delete",
Usage: "Mark a user as deleted",
Action: runtime(cmdUserMarkDeleted),
},
{
Name: "purge",
Usage: "Purge a user marked as deleted",
Action: runtime(cmdUserPurgeDeleted),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Purge all deleted users",
},
},
},
{
Name: "update",
Usage: "Set/change user information",
Action: runtime(cmdUserUpdate),
BashComplete: cmpl.UserUpdate,
},
/*
{
Name: "restore",
Usage: "Restore a user marked as deleted",
Action: cmdUserRestoreDeleted,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Restore all deleted users",
},
},
},
{
Name: "rename",
Usage: "Change a user's username",
Action: cmdUserRename,
},
*/
{
Name: "activate",
Usage: "Activate a deativated user",
Action: cmdUserActivate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Apply administrative force to the activation",
},
},
},
/*
{
Name: "deactivate",
Usage: "Deactivate a user account",
Action: cmdUserDeactivate,
},
*/
{
Name: `password`,
Usage: "SUBCOMMANDS for user passwords",
Subcommands: []cli.Command{
{
Name: `update`,
Usage: `Update the password of one's own user account`,
Action: boottime(cmdUserPasswordUpdate),
Description: help.CmdUserPasswordUpdate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: `reset, r`,
Usage: `Reset the password via activation credentials`,
},
},
},
/*
{
Name: `reset`,
Usage: `Trigger a password reset for a user`,
Action: cmdUserPasswordReset,
},
{
Name: `force`,
Usage: `Forcefully set the password of a user`,
Action: cmdUserPasswordForce,
},
*/
},
}, // end users password
{
Name: "list",
Usage: "List all registered users",
Action: runtime(cmdUserList),
},
{
Name: "show",
Usage: "Show information about a specific user",
Action: runtime(cmdUserShow),
},
{
Name: "synclist",
Usage: "List all registered users suitable for sync",
Action: runtime(cmdUserSync),
},
},
}, // end users
}...,
)
return &app
}
func cmdUserAdd(c *cli.Context) error {
utl.ValidateCliMinArgumentCount(c, 11)
multiple := []string{}
unique := []string{"firstname", "lastname", "employeenr",
"mailaddr", "team", "active", "system"}
required := []string{"firstname", "lastname", "employeenr",
"mailaddr", "team"}
var err error
opts := utl.ParseVariadicArguments(
multiple,
unique,
required,
c.Args().Tail())
// validate
utl.ValidateStringAsEmployeeNumber(opts["employeenr"][0])
utl.ValidateStringAsMailAddress(opts["mailaddr"][0])
req := proto.Request{}
req.User = &proto.User{}
req.User.UserName = c.Args().First()
req.User.FirstName = opts["firstname"][0]
req.User.LastName = opts["lastname"][0]
req.User.TeamId = utl.TryGetTeamByUUIDOrName(Client, opts["team"][0])
req.User.MailAddress = opts["mailaddr"][0]
req.User.EmployeeNumber = opts["employeenr"][0]
req.User.IsDeleted = false
// optional arguments
if _, ok := opts["active"]; ok {
req.User.IsActive, err = strconv.ParseBool(opts["active"][0])
utl.AbortOnError(err, "Syntax error, active argument not boolean")
} else {
req.User.IsActive = true
}
if _, ok := opts["system"]; ok {
req.User.IsSystem, err = strconv.ParseBool(opts["system"][0])
utl.AbortOnError(err, "Syntax error, system argument not boolean")
} else {
req.User.IsSystem = false
}
resp := utl.PostRequestWithBody(Client, req, "/users/")
fmt.Println(resp)
return nil
}
func cmdUserUpdate(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 15)
multiple := []string{}
unique := []string{`username`, "firstname", "lastname", "employeenr",
"mailaddr", "team", `deleted`}
required := []string{`username`, "firstname", "lastname", "employeenr",
"mailaddr", "team", `deleted`}
opts := utl.ParseVariadicArguments(
multiple,
unique,
required,
c.Args().Tail())
// validate
utl.ValidateStringAsEmployeeNumber(opts["employeenr"][0])
utl.ValidateStringAsMailAddress(opts["mailaddr"][0])
if !utl.IsUUID(c.Args().First()) {
return fmt.Errorf(`users update requiress UUID as first argument`)
}
req := proto.NewUserRequest()
req.User.Id = c.Args().First()
req.User.UserName = opts[`username`][0]
req.User.FirstName = opts["firstname"][0]
req.User.LastName = opts["lastname"][0]
req.User.TeamId = utl.TryGetTeamByUUIDOrName(Client, opts["team"][0])
req.User.MailAddress = opts["mailaddr"][0]
req.User.EmployeeNumber = opts["employeenr"][0]
req.User.IsDeleted = utl.GetValidatedBool(opts[`deleted`][0])
path := fmt.Sprintf("/users/%s", req.User.Id)
resp := utl.PutRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
func cmdUserMarkDeleted(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
userId := utl.TryGetUserByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/users/%s", userId)
resp := utl.DeleteRequest(Client, path)
fmt.Println(resp)
return nil
}
func cmdUserPurgeDeleted(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
userId := utl.TryGetUserByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/users/%s", userId)
req := proto.Request{
Flags: &proto.Flags{
Purge: true,
},
}
resp := utl.DeleteRequestWithBody(Client, req, path)
fmt.Println(resp)
return nil
}
/*
func cmdUserRestoreDeleted(c *cli.Context) {
url := getApiUrl()
var (
id uuid.UUID
err error
)
if c.Bool("all") {
utl.ValidateCliArgumentCount(c, 0)
url.Path = fmt.Sprintf("/users")
} else {
switch utl.GetCliArgumentCount(c) {
case 1:
id, err = uuid.FromString(c.Args().First())
utl.AbortOnError(err, "Syntax error, argument not a uuid")
case 2:
utl.ValidateCliArgument(c, 1, "by-name")
id = utl.GetUserIdByName(c.Args().Get(1))
default:
utl.Abort("Syntax error, unexpected argument count")
}
url.Path = fmt.Sprintf("/users/%s", id.String())
}
var req somaproto.ProtoRequestUser
req.Restore = true
_ = utl.PatchRequestWithBody(Client, req, url.String())
}
func cmdUserUpdate(c *cli.Context) {
url := getApiUrl()
var (
id uuid.UUID
err error
)
argSlice := make([]string, 0)
keySlice := []string{"firstname", "lastname", "employeenr", "mailaddr", "team"}
reqSlice := make([]string, 0)
switch utl.GetCliArgumentCount(c) {
case 1, 3, 5, 7, 9, 11:
id, err = uuid.FromString(c.Args().First())
utl.AbortOnError(err, "Syntax error, argument not a uuid")
argSlice = c.Args().Tail()
case 2, 4, 6, 8, 10, 12:
utl.ValidateCliArgument(c, 1, "by-name")
id = utl.GetUserIdByName(c.Args().Tail()[0])
argSlice = c.Args().Tail()[1:]
default:
utl.Abort("Syntax error, unexpected argument count")
}
url.Path = fmt.Sprintf("/users/%s", id.String())
options, opts := utl.ParseVariableArguments(keySlice, reqSlice, argSlice)
var req somaproto.ProtoRequestUser
for _, v := range opts {
switch v {
case "firstname":
req.User.FirstName = options["firstname"]
case "lastname":
req.User.LastName = options["lastname"]
case "employeenr":
utl.ValidateStringAsEmployeeNumber(options["employeenr"])
req.User.EmployeeNumber = options["employeenr"]
case "mailaddr":
utl.ValidateStringAsMailAddress(options["mailaddr"])
req.User.MailAddress = options["mailaddr"]
case "team":
req.User.Team = options["team"]
}
}
_ = utl.PatchRequestWithBody(Client, req, url.String())
}
func cmdUserRename(c *cli.Context) {
url := getApiUrl()
var (
id uuid.UUID
err error
newName string
)
switch utl.GetCliArgumentCount(c) {
case 3:
utl.ValidateCliArgument(c, 2, "to")
id, err = uuid.FromString(c.Args().First())
utl.AbortOnError(err, "Syntax error, argument not a uuid")
newName = c.Args().Get(2)
case 4:
utl.ValidateCliArgument(c, 1, "by-name")
utl.ValidateCliArgument(c, 3, "to")
id = utl.GetUserIdByName(c.Args().Get(1))
newName = c.Args().Get(3)
default:
utl.Abort("Syntax error, unexpected argument count")
}
url.Path = fmt.Sprintf("/users/%s", id.String())
var req somaproto.ProtoRequestUser
req.User.UserName = newName
_ = utl.PatchRequestWithBody(Client, req, url.String())
}
*/
func cmdUserActivate(c *cli.Context) error {
// administrative use, full runtime is available
if c.GlobalIsSet(`admin`) {
utl.ValidateCliArgumentCount(c, 1)
return runtime(cmdUserActivateAdmin)(c)
}
// user trying to activate the account for the first
// time, reduced runtime
utl.ValidateCliArgumentCount(c, 0)
return boottime(cmdUserActivateUser)(c)
}
func cmdUserActivateUser(c *cli.Context) error {
var err error
var password string
var passKey string
var happy bool
var cred *auth.Token
if Cfg.Auth.User == "" {
fmt.Println(`Please specify which account to activate.`)
if Cfg.Auth.User, err = adm.Read(`user`); err != nil {
return err
}
} else {
fmt.Printf("Starting with activation of account '%s' in 2 seconds.\n", Cfg.Auth.User)
fmt.Printf(`Use --user flag to activate a different account.`)
time.Sleep(2 * time.Second)
}
if strings.Contains(Cfg.Auth.User, `:`) {
return fmt.Errorf(`Usernames must not contain : character.`)
}
fmt.Printf("\nPlease provide the password you want to use.\n")
password_read:
password = adm.ReadVerified(`password`)
if happy, err = adm.EvaluatePassword(3, password, Cfg.Auth.User, `soma`); err != nil {
return err
} else if !happy {
password = ""
goto password_read
}
fmt.Printf("\nTo confirm that this is your account, an additional credential is required" +
" this once.\n")
switch Cfg.Activation {
case `ldap`:
fmt.Println(`Please provide your LDAP password to establish ownership.`)
passKey = adm.ReadVerified(`password`)
case `mailtoken`:
fmt.Println(`Please provide the token you received via email.`)
passKey = adm.ReadVerified(`token`)
default:
return fmt.Errorf(`Unknown activation mode`)
}
if cred, err = adm.ActivateAccount(Client, &auth.Token{
UserName: Cfg.Auth.User,
Password: password,
Token: passKey,
}); err != nil {
return err
}
// validate received token
if err = adm.ValidateToken(Client, Cfg.Auth.User, cred.Token); err != nil {
return err
}
// save received token
if err = store.SaveToken(
Cfg.Auth.User,
cred.ValidFrom,
cred.ExpiresAt,
cred.Token,
); err != nil {
return err
}
return nil
}
func cmdUserActivateAdmin(c *cli.Context) error {
return nil
}
/*
func cmdUserDeactivate(c *cli.Context) {
url := getApiUrl()
id := utl.UserIdByUuidOrName(c)
url.Path = fmt.Sprintf("/users/%s", id.String())
var req somaproto.ProtoRequestUser
req.User.IsActive = false
_ = utl.PatchRequestWithBody(Client, req, url.String())
}
*/
func cmdUserList(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp := utl.GetRequest(Client, "/users/")
fmt.Println(resp)
return nil
}
func cmdUserSync(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
resp := utl.GetRequest(Client, `/sync/users/`)
fmt.Println(resp)
return nil
}
func cmdUserShow(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
id := utl.TryGetUserByUUIDOrName(Client, c.Args().First())
path := fmt.Sprintf("/users/%s", id)
resp := utl.GetRequest(Client, path)
fmt.Println(resp)
return nil
}
/*
func cmdUserPasswordReset(c *cli.Context) {
id := utl.UserIdByUuidOrName(c)
path := fmt.Sprintf("/users/%s/password", id.String())
var req somaproto.ProtoRequestUser
req.Credentials.Reset = true
_ = utl.PutRequestWithBody(Client, req, path)
}
func cmdUserPasswordForce(c *cli.Context) {
id := utl.UserIdByUuidOrName(c)
path := fmt.Sprintf("/users/%s/password", id.String())
pass := utl.GetNewPassword()
var req somaproto.ProtoRequestUser
req.Credentials.Force = true
req.Credentials.Password = pass
_ = utl.PutRequestWithBody(Client, req, path)
}
*/
func cmdUserPasswordUpdate(c *cli.Context) error {
var (
err error
password, passKey string
happy bool
cred *auth.Token
)
if Cfg.Auth.User == `` {
fmt.Println(`Please specify for which account the password should be changed.`)
if Cfg.Auth.User, err = adm.Read(`user`); err != nil {
return err
}
} else {
fmt.Printf("Starting with password update of account '%s' in 2 seconds.\n", Cfg.Auth.User)
fmt.Printf(`Use --user flag to switch account account.`)
time.Sleep(2 * time.Second)
}
if strings.Contains(Cfg.Auth.User, `:`) {
return fmt.Errorf(`Usernames must not contain : character.`)
}
fmt.Printf("\nPlease provide the new password you want to set.\n")
password_read:
password = adm.ReadVerified(`password`)
if happy, err = adm.EvaluatePassword(3, password, Cfg.Auth.User, `soma`); err != nil {
return err
} else if !happy {
password = ``
goto password_read
}
if c.Bool(`reset`) {
fmt.Printf("\nTo confirm that you are allowed to reset this account, an additional" +
"credential is required.\n")
switch Cfg.Activation {
case `ldap`:
fmt.Println(`Please provide your LDAP password to establish ownership.`)
passKey = adm.ReadVerified(`password`)
case `mailtoken`:
fmt.Println(`Please provide the token you received via email.`)
passKey = adm.ReadVerified(`token`)
default:
return fmt.Errorf(`Unknown activation mode`)
}
} else {
fmt.Printf("\nPlease provide your currently active/old password.\n")
passKey = adm.ReadVerified(`password`)
}
if cred, err = adm.ChangeAccountPassword(Client, c.Bool(`reset`), &auth.Token{
UserName: Cfg.Auth.User,
Password: password,
Token: passKey,
}); err != nil {
return err
}
// validate received token
if err = adm.ValidateToken(Client, Cfg.Auth.User, cred.Token); err != nil {
return err
}
// save received token
if err = store.SaveToken(
Cfg.Auth.User,
cred.ValidFrom,
cred.ExpiresAt,
cred.Token,
); err != nil {
return err
}
return nil
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix