This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
user.go
92 lines (88 loc) · 2.69 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
package cli
import (
"fmt"
"github.com/digitalrebar/provision/v4/models"
"github.com/spf13/cobra"
)
func init() {
addRegistrar(registerUser)
}
func registerUser(app *cobra.Command) {
op := &ops{
name: "users",
singleName: "user",
example: func() models.Model { return &models.User{} },
}
op.addCommand(&cobra.Command{
Use: "password [id] [password]",
Short: "Set the password for this id",
Long: "Set the password for this id",
Args: func(c *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("%v needs 2 args", c.UseLine())
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
pwd := &models.UserPassword{Password: args[1]}
res := &models.User{}
if err := Session.Req().Put(pwd).UrlFor("users", args[0], "password").Do(res); err != nil {
return generateError(err, "Error: putUserPassword: %v", err)
}
return prettyPrint(res)
},
})
op.addCommand(&cobra.Command{
Use: "passwordhash [password]",
Short: "Get a password hash for a password",
Long: "Get a password hash for a password. This can be used in content packages.",
Args: func(c *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("%v needs 1 arg", c.UseLine())
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
res := &models.User{}
if err := res.ChangePassword(args[0]); err != nil {
return generateError(err, "Error: generating password: %v", err)
}
fmt.Printf("%s\n", string(res.PasswordHash))
return nil
},
})
tokenArgs := []string{}
op.addCommand(&cobra.Command{
Use: "token [id] [ttl [ttl]] [scope [scope]] [action [action]] [specific [specific]]",
Short: "Get a login token for this user with optional parameters",
Long: "Creates a time-bound token for the specified user.",
Args: func(c *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("%v needs at least 1 arg", c.UseLine())
}
tokenArgs = []string{}
index := 2
for index < len(args) {
switch args[index-1] {
case "scope", "action", "specific", "ttl":
tokenArgs = append(tokenArgs, args[index-1], args[index])
index += 2
default:
return fmt.Errorf("%v does not support %s", c.UseLine(), args[index-1])
}
}
if index-1 != len(args) {
return fmt.Errorf("%v needs at least 1 and pairs arg", c.UseLine())
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
res := &models.UserToken{}
if err := Session.Req().UrlFor("users", args[0], "token").Params(tokenArgs...).Do(res); err != nil {
return generateError(err, "Error: getToken: %v", err)
}
return prettyPrint(res)
},
})
op.command(app)
}