forked from odeke-em/drive
/
share.go
292 lines (255 loc) · 5.92 KB
/
share.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"fmt"
"strings"
"sync"
"github.com/odeke-em/log"
)
type AccountType int
const (
UnknownAccountType = 1 << iota
Anyone
User
Domain
Group
)
type Role int
const (
UnknownRole = 1 << iota
Owner
Reader
Writer
Commenter
)
const (
NoopOnShare = 1 << iota
Notify
)
type shareChange struct {
emailMessage string
emails []string
role Role
accountType AccountType
files []*File
revoke bool
notify bool
}
type permission struct {
fileId string
value string
message string
role Role
accountType AccountType
notify bool
}
func (r *Role) String() string {
switch *r {
case Owner:
return "owner"
case Reader:
return "reader"
case Writer:
return "writer"
case Commenter:
return "commenter"
}
return "unknown"
}
func (a *AccountType) String() string {
switch *a {
case Anyone:
return "anyone"
case User:
return "user"
case Domain:
return "domain"
case Group:
return "group"
}
return "unknown"
}
func stringToRole() func(string) Role {
roleMap := make(map[string]Role)
roles := []Role{UnknownRole, Owner, Reader, Writer, Commenter}
for _, role := range roles {
roleMap[role.String()] = role
}
return func(s string) Role {
r, ok := roleMap[strings.ToLower(s)]
if !ok {
return Reader
}
return r
}
}
func stringToAccountType() func(string) AccountType {
accountMap := make(map[string]AccountType)
accounts := []AccountType{UnknownAccountType, Anyone, User, Domain, Group}
for _, account := range accounts {
accountMap[account.String()] = account
}
return func(s string) AccountType {
a, ok := accountMap[strings.ToLower(s)]
if !ok {
return User
}
return a
}
}
var reverseRoleResolve = stringToRole()
var reverseAccountTypeResolve = stringToAccountType()
func (g *Commands) resolveRemotePaths(relToRootPaths []string) (files []*File) {
var wg sync.WaitGroup
wg.Add(len(relToRootPaths))
for _, relToRoot := range relToRootPaths {
go func(p string, wgg *sync.WaitGroup) {
defer wgg.Done()
file, err := g.rem.FindByPath(p)
if err != nil || file == nil {
return
}
files = append(files, file)
}(relToRoot, &wg)
}
wg.Wait()
return files
}
func emailsToIds(g *Commands, emails []string) map[string]string {
emailToIds := make(map[string]string)
var wg sync.WaitGroup
wg.Add(len(emails))
for _, email := range emails {
go func(email string, wgg *sync.WaitGroup) {
defer wgg.Done()
emailId, err := g.rem.idForEmail(email)
if err == nil {
emailToIds[email] = emailId
}
}(email, &wg)
}
wg.Wait()
return emailToIds
}
func (c *Commands) Unshare() (err error) {
return c.share(true)
}
func (c *Commands) Share() (err error) {
return c.share(false)
}
func showPromptShareChanges(logy *log.Logger, change *shareChange) bool {
if len(change.files) < 1 {
return false
}
if change.revoke {
logy.Logf("Revoke access for accountType: \033[92m%s\033[00m for file(s):\n",
change.accountType.String())
for _, file := range change.files {
logy.Logln("+ ", file.Name)
}
logy.Logln()
return promptForChanges()
}
if len(change.emails) < 1 {
return false
}
if change.notify {
logy.Logln("Message:\n\t", change.emailMessage)
}
logy.Logln("Receipients:")
for _, email := range change.emails {
logy.Logf("\t\033[92m+\033[00m %s\n", email)
}
logy.Logln("\nFile(s) to share:")
for _, file := range change.files {
if file == nil {
continue
}
logy.Logf("\t\033[92m+\033[00m %s\n", file.Name)
}
return promptForChanges()
}
func (c *Commands) playShareChanges(change *shareChange) error {
if c.opts.canPrompt() && !showPromptShareChanges(c.log, change) {
return nil
}
for _, file := range change.files {
if change.revoke {
if err := c.rem.deletePermissions(file.Id, change.accountType); err != nil {
return fmt.Errorf("%s: %v", file.Name, err)
}
continue
}
for _, email := range change.emails {
perm := permission{
fileId: file.Id,
value: email,
message: change.emailMessage,
notify: change.notify,
role: change.role,
accountType: change.accountType,
}
_, err := c.rem.insertPermissions(&perm)
if err != nil {
return err
}
}
}
return nil
}
func (c *Commands) share(revoke bool) (err error) {
files := c.resolveRemotePaths(c.opts.Sources)
var role Role
var accountType AccountType
var emails []string
var emailMessage string
// Setup the defaults
role = Reader
accountType = User
meta := *c.opts.Meta
if meta != nil {
emailList, eOk := meta["emails"]
if eOk {
emails = emailList
if false {
emailIdMap := emailsToIds(c, emailList)
c.log.Logln(emailIdMap)
}
}
roleList, rOk := meta["role"]
if rOk && len(roleList) >= 1 {
role = reverseRoleResolve(roleList[0])
}
accountTypeList, aOk := meta["accountType"]
if aOk && len(accountTypeList) >= 1 {
accountType = reverseAccountTypeResolve(accountTypeList[0])
}
emailMessageList, emOk := meta["emailMessage"]
if emOk && len(emailMessageList) >= 1 {
emailMessage = strings.Join(emailMessageList, "\n")
}
}
notify := (c.opts.TypeMask & Notify) != 0
change := shareChange{
accountType: accountType,
emailMessage: emailMessage,
emails: emails,
files: files,
revoke: revoke,
role: role,
notify: notify,
}
return c.playShareChanges(&change)
}