-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_user.go
235 lines (204 loc) · 5.38 KB
/
cmd_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
package main
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"github.com/urfave/cli"
"golang.org/x/net/context"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func cmdUserList(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory Client %v", err)
}
call := srv.Users.List().
Customer(myAccoutsCustomerID).
MaxResults(int64(ifZero(c.Int("limit"), 100))).
OrderBy("email")
if domain := c.GlobalString("domain"); len(domain) > 0 {
call = call.Domain(domain)
}
r, err := call.Do()
if err != nil {
return fmt.Errorf("unable to retrieve users in domain: %v", err)
}
if optionJSON(c, r.Users) {
return nil
}
for _, u := range r.Users {
// email is default
fmt.Println(u.PrimaryEmail)
}
return nil
}
func cmdUserMembershipList(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %v", err)
}
// visit all groups and check membership
memberKey := c.Args().Get(0)
if len(memberKey) == 0 {
return fmt.Errorf("missing user email in command")
}
if strings.Index(memberKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
memberKey = fmt.Sprintf("%s@%s", memberKey, domain)
}
done := showSpinnerWhile(c)
// get all groups
if c.GlobalBool("v") {
log.Println("[gws] fetching all groups")
}
r, err := srv.Groups.List().
Customer(myAccoutsCustomerID).
MaxResults(int64(ifZero(c.Int("limit"), 100))).
OrderBy("email").Do()
if err != nil {
return fmt.Errorf("unable to retrieve groups in domain: %v", err)
}
checks := make(chan memberCheck, len(r.Groups))
wg := new(sync.WaitGroup)
for _, g := range r.Groups {
if c.GlobalBool("v") {
log.Printf("[gws] is %s member of group %s ?\n", memberKey, g.Email)
}
wg.Add(1)
go func(check memberCheck) {
// Use Email or immutable ID of the group
hasResult, err := srv.Members.HasMember(check.group.Id, check.memberKey).Do()
if err != nil {
check.callError = fmt.Errorf("aborting! unable to check membership of [email:%s] in [group:%s] because [%v]", memberKey, check.group.Email, err)
} else {
check.isMember = hasResult.IsMember
}
checks <- check
wg.Done()
}(memberCheck{
memberKey: memberKey,
group: g,
})
}
wg.Wait()
close(checks)
// collect memberships
membership := []*admin.Group{}
var abortError error = nil
for each := range checks {
if abortError == nil {
if each.callError != nil {
abortError = each.callError
} else {
if each.isMember {
membership = append(membership, each.group)
}
}
}
}
done() // end spinner
if abortError != nil {
return abortError
}
if optionJSON(c, membership) {
return nil
}
for _, g := range membership {
// email is default
fmt.Println(g.Email)
}
return nil
}
type memberCheck struct {
memberKey string
isMember bool
group *admin.Group
callError error
}
func cmdUserInfo(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %v", err)
}
userKey := c.Args().Get(0)
if len(userKey) == 0 {
return fmt.Errorf("missing user email in command")
}
if strings.Index(userKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
userKey = fmt.Sprintf("%s@%s", userKey, domain)
}
r, err := srv.Users.Get(userKey).Do()
if err != nil {
return fmt.Errorf("unable to retrieve [user:%s] because: %v", userKey, err)
}
if optionJSON(c, r) {
return nil
}
fmt.Printf("%s (%s) [tel: %s, 2nd: %s, suspended: %v]\n", r.PrimaryEmail, r.Name.FullName, r.RecoveryPhone, r.RecoveryEmail, r.Suspended)
return nil
}
func cmdUserAlias(c *cli.Context) error {
client := sharedAuthClient(c)
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %v", err)
}
userKey := c.Args().Get(0)
if len(userKey) == 0 {
return fmt.Errorf("missing user email in command")
}
if strings.Index(userKey, "@") == -1 {
domain, err := primaryDomain(c)
if err != nil {
return err
}
userKey = fmt.Sprintf("%s@%s", userKey, domain)
}
r, err := srv.Users.Aliases.List(userKey).Do()
if err != nil {
return fmt.Errorf("unable to retrieve [user:%s] because: %v", userKey, err)
}
if optionJSON(c, r) {
return nil
}
fmt.Printf("%v\n", r)
return nil
}
func cmdUserSuspend(c *cli.Context) error {
srv, err := admin.NewService(context.Background(), option.WithHTTPClient(sharedAuthClient(c)))
if err != nil {
return fmt.Errorf("unable to retrieve directory client %v", err)
}
// user
userKey, err := userKey(c)
if err != nil {
return err
}
reason := c.Args().Get(1)
// prompt
if !promptForYes(c, fmt.Sprintf("Are you sure to suspend user [%s] because [%s] (y/N)? ", userKey, reason)) {
return errors.New("user suspend aborted")
}
user := &admin.User{
Suspended: true,
SuspensionReason: reason,
}
_, err = srv.Users.Patch(userKey, user).Do()
if err != nil {
return fmt.Errorf("unable to suspend [user:%s] because: %v", userKey, err)
}
return nil
}