This repository was archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuser.go
More file actions
153 lines (140 loc) · 3.52 KB
/
user.go
File metadata and controls
153 lines (140 loc) · 3.52 KB
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
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))
}
func ListUserBuckets(userId string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/users/%v/buckets", p.AppId, userId)
headers := p.HttpHeadersWithAuthorization("")
b := HttpGet(path, headers).Bytes()
fmt.Println(string(b))
}
func DeleteUserBucket(userId, bucketId string) {
p := Profile()
path := fmt.Sprintf("/apps/%s/users/%v/buckets/%v", p.AppId, userId, bucketId)
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)
},
},
{
Name: "list-buckets",
Usage: "List buckets",
Args: `<user-id>`,
Action: func(c *cli.Context) {
uid, _ := c.ArgFor("user-id")
ListUserBuckets(uid)
},
},
{
Name: "delete-bucket",
Usage: "Delete a user bucket",
Args: `<user-id> <bucket-id>`,
Action: func(c *cli.Context) {
uid, _ := c.ArgFor("user-id")
bid, _ := c.ArgFor("bucket-id")
DeleteUserBucket(uid, bid)
},
},
}