-
Notifications
You must be signed in to change notification settings - Fork 137
/
rule.go
312 lines (296 loc) · 8.06 KB
/
rule.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package sharing
import (
"strings"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
)
const (
// ActionRuleNone is used when an add/update/remove should not be
// replicated to the other cozys
ActionRuleNone = "none"
// ActionRulePush is used when an add/update/remove should be replicated
// only if it happened on the owner's cozy
ActionRulePush = "push"
// ActionRuleSync is used when an add/update/remove should be always replicated
ActionRuleSync = "sync"
// ActionRuleRevoke is used when a remove should revoke the sharing
ActionRuleRevoke = "revoke"
)
// Rule describes how the sharing behave when a document matching the rule is
// added, updated or deleted.
type Rule struct {
Title string `json:"title"`
DocType string `json:"doctype"`
Mime string `json:"mime,omitempty"`
Selector string `json:"selector,omitempty"`
Values []string `json:"values"`
Local bool `json:"local,omitempty"`
Add string `json:"add"`
Update string `json:"update"`
Remove string `json:"remove"`
}
// FilesByID returns true if the rule is for the files by doctype and the
// selector is an id (not a referenced_by). With such a rule, the identifiers
// must be xored before being sent to another cozy instance.
func (r Rule) FilesByID() bool {
if r.DocType != consts.Files {
return false
}
return r.Selector == "" || r.Selector == "id" || r.Selector == "_id"
}
// ValidateRules returns an error if the rules are invalid (the doctype is
// missing for example)
func (s *Sharing) ValidateRules() error {
if len(s.Rules) == 0 {
return ErrNoRules
}
for i, rule := range s.Rules {
if rule.Title == "" || len(rule.Values) == 0 {
return ErrInvalidRule
}
if permission.CheckDoctypeName(rule.DocType, false) != nil {
return ErrInvalidRule
}
if rule.DocType == consts.Files {
for _, val := range rule.Values {
if val == consts.RootDirID ||
val == consts.TrashDirID ||
val == consts.SharedWithMeDirID {
return ErrInvalidRule
}
}
// XXX Currently, we only support one file/folder per rule for the id selector
if rule.Selector == "" || rule.Selector == "id" || rule.Selector == "_id" {
if len(rule.Values) > 1 {
return ErrInvalidRule
}
}
if rule.Selector == couchdb.SelectorReferencedBy {
// For a referenced_by rule, values should be "doctype/docid"
for _, val := range rule.Values {
parts := strings.SplitN(val, "/", 2)
if len(parts) != 2 {
return ErrInvalidRule
}
}
}
} else if permission.CheckWritable(rule.DocType) != nil {
return ErrInvalidRule
}
if rule.Add == "" {
s.Rules[i].Add = ActionRuleNone
rule.Add = s.Rules[i].Add
}
rule.Add = strings.ToLower(rule.Add)
if rule.Add != ActionRuleNone &&
rule.Add != ActionRulePush &&
rule.Add != ActionRuleSync {
return ErrInvalidRule
}
if rule.Update == "" {
s.Rules[i].Update = ActionRuleNone
rule.Update = s.Rules[i].Update
}
rule.Update = strings.ToLower(rule.Update)
if rule.Update != ActionRuleNone &&
rule.Update != ActionRulePush &&
rule.Update != ActionRuleSync {
return ErrInvalidRule
}
if rule.Remove == "" {
s.Rules[i].Remove = ActionRuleNone
rule.Remove = s.Rules[i].Remove
}
rule.Remove = strings.ToLower(rule.Remove)
if rule.Remove != ActionRuleNone &&
rule.Remove != ActionRulePush &&
rule.Remove != ActionRuleSync &&
rule.Remove != ActionRuleRevoke {
return ErrInvalidRule
}
}
return nil
}
// Accept returns true if the document matches the rule criteria
func (r Rule) Accept(doctype string, doc map[string]interface{}) bool {
if r.Local || doctype != r.DocType {
return false
}
var obj interface{} = doc
if r.Selector == "" || r.Selector == "id" {
obj = doc["_id"]
} else if doctype == consts.Files && r.Selector == couchdb.SelectorReferencedBy {
if o, k := doc[couchdb.SelectorReferencedBy].([]map[string]interface{}); k {
refs := make([]string, len(o))
for i, ref := range o {
refs[i] = ref["type"].(string) + "/" + ref["id"].(string)
}
obj = refs
}
} else {
keys := strings.Split(r.Selector, ".")
for _, key := range keys {
if o, k := obj.(map[string]interface{}); k {
obj = o[key]
} else {
obj = nil
break
}
}
}
if val, ok := obj.(string); ok {
for _, v := range r.Values {
if v == val {
return true
}
}
}
if val, ok := obj.([]string); ok {
for _, vv := range val {
for _, v := range r.Values {
if v == vv {
return true
}
}
}
}
return false
}
// TriggerArgs returns the string that can be used as an argument to create a
// trigger for this rule. The result can be an empty string if the rule doesn't
// need a trigger (a local or one-shot rule).
func (r Rule) TriggerArgs() string {
if r.Local {
return ""
}
verbs := make([]string, 1, 3)
// We always need the CREATED verb to have io.cozy.shared for all the
// shared documents, as the io.cozy.shared documents are needed to
// accept the updates and deletes later.
verbs[0] = "CREATED"
if r.Update == ActionRuleSync || r.Update == ActionRulePush {
verbs = append(verbs, "UPDATED")
}
if r.Remove == ActionRuleSync || r.Remove == ActionRulePush || r.Remove == ActionRuleRevoke {
verbs = append(verbs, "DELETED")
}
if len(verbs) == 0 {
return ""
}
args := r.DocType + ":" + strings.Join(verbs, ",")
if len(r.Values) > 0 {
args += ":" + strings.Join(r.Values, ",")
if r.Selector != "" && r.Selector != "id" {
args += ":" + r.Selector
}
}
return args
}
// FirstBitwardenOrganizationRule returns the first not-local rules for the
// com.bitwarden.organizations doctype.
func (s *Sharing) FirstBitwardenOrganizationRule() *Rule {
for i, rule := range s.Rules {
if !rule.Local && rule.DocType == consts.BitwardenOrganizations {
return &s.Rules[i]
}
}
return nil
}
// FirstFilesRule returns the first not-local rules for the files doctype.
func (s *Sharing) FirstFilesRule() *Rule {
for i, rule := range s.Rules {
if !rule.Local && rule.DocType == consts.Files {
return &s.Rules[i]
}
}
return nil
}
func (s *Sharing) findRuleForNewDirectory(dir *vfs.DirDoc) (*Rule, int) {
for i, rule := range s.Rules {
if rule.Local || rule.DocType != consts.Files {
continue
}
if rule.Selector != couchdb.SelectorReferencedBy {
return &s.Rules[i], i
}
if len(dir.ReferencedBy) == 0 {
continue
}
allFound := true
for _, ref := range dir.ReferencedBy {
if !rule.hasReferencedBy(ref) {
allFound = false
break
}
}
if allFound {
return &s.Rules[i], i
}
}
return nil, 0
}
func (s *Sharing) findRuleForNewFile(file *vfs.FileDoc) (*Rule, int) {
for i, rule := range s.Rules {
if rule.Local || rule.DocType != consts.Files {
continue
}
if rule.Selector != couchdb.SelectorReferencedBy {
return &s.Rules[i], i
}
if len(file.ReferencedBy) == 0 {
continue
}
allFound := true
for _, ref := range file.ReferencedBy {
if !rule.hasReferencedBy(ref) {
allFound = false
break
}
}
if allFound {
return &s.Rules[i], i
}
}
return nil, 0
}
func (s *Sharing) hasExplicitRuleForFile(file *vfs.FileDoc) bool {
for _, rule := range s.Rules {
if rule.Local || rule.DocType != consts.Files {
continue
}
if rule.Selector != "" {
continue
}
for _, id := range rule.Values {
if id == file.DocID {
return true
}
}
}
return false
}
// HasSync returns true if the rule has a sync behaviour
func (r *Rule) HasSync() bool {
return r.Add == ActionRuleSync || r.Update == ActionRuleSync ||
r.Remove == ActionRuleSync
}
// HasPush returns true if the rule has a push behaviour
func (r *Rule) HasPush() bool {
return r.Add == ActionRulePush || r.Update == ActionRulePush ||
r.Remove == ActionRulePush
}
// hasReferencedBy returns true if the rule matches a file that has this reference
func (r *Rule) hasReferencedBy(ref couchdb.DocReference) bool {
if r.Selector != couchdb.SelectorReferencedBy {
return false
}
v := ref.Type + "/" + ref.ID
for _, val := range r.Values {
if val == v {
return true
}
}
return false
}