forked from tmtk75/kii-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
118 lines (107 loc) · 2.64 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
package kiicli
import (
"fmt"
"io"
"os"
"path"
"strings"
"github.com/tmtk75/cli"
)
type UserCreationRequest struct {
LoginName string `json:"loginName"`
Password string `json:"password"`
}
func CreateUser(loginname string, password string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/users", p.AppId)
headers := p.HttpHeaders("application/json")
req := &UserCreationRequest{loginname, password}
res := HttpPostJson(path, headers, req)
defer res.Body.Close()
io.Copy(os.Stdout, res.Body)
}
func LoginAsUser(username string, password string) {
p := Profile()
dir := path.Join(p.AppId, username)
tokenPath := metaFilePath(dir, "token")
oauth2res := &OAuth2Response{}
if b, _ := exists(tokenPath); b {
oauth2res.LoadFrom(tokenPath)
} else {
headers := p.HttpHeaders("application/json")
req := map[string]string{"username": username, "password": password}
res := HttpPostJson("/oauth2/token", headers, req)
oauth2res.Decode(res)
oauth2res.Save(tokenPath)
}
fmt.Println(oauth2res)
}
func ReadUser(userId string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/users/%v", p.AppId, userId)
headers := p.HttpHeadersWithAuthorization("application/json")
b := HttpGet(path, headers).Bytes()
fmt.Println(string(b))
}
func ListUsers() {
p := Profile()
path := fmt.Sprintf("/apps/%s/users/query", p.AppId)
headers := p.HttpHeadersWithAuthorization("application/vnd.kii.userqueryrequest+json")
body := strings.NewReader(`{"userQuery":{"clause":{"type":"all"}}}`)
b := HttpPost(path, headers, body).Bytes()
fmt.Println(string(b))
}
func DeleteUser(userId string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/users/%v", p.AppId, userId)
headers := p.HttpHeadersWithAuthorization("")
b := HttpDelete(path, headers).Bytes()
fmt.Println(string(b))
}
var UserCommands = []cli.Command{
{
Name: "create",
Usage: "Create a user",
Args: `<loginname> <password>`,
Action: func(c *cli.Context) {
name, _ := c.ArgFor("loginname")
pass, _ := c.ArgFor("password")
CreateUser(name, pass)
},
},
{
Name: "read",
Usage: "Read a user",
Args: `<user-id>`,
Action: func(c *cli.Context) {
uid, _ := c.ArgFor("user-id")
ReadUser(uid)
},
},
{
Name: "list",
Usage: "List users",
Action: func(c *cli.Context) {
ListUsers()
},
},
{
Name: "delete",
Usage: "Delete a user",
Args: `<user-id>`,
Action: func(c *cli.Context) {
uid, _ := c.ArgFor("user-id")
DeleteUser(uid)
},
},
{
Name: "login",
Usage: "Login as a user",
Args: `<loginname> <password>`,
Action: func(c *cli.Context) {
name, _ := c.ArgFor("loginname")
pass, _ := c.ArgFor("password")
LoginAsUser(name, pass)
},
},
}