-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpermcache2.go
47 lines (42 loc) · 1.03 KB
/
permcache2.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
package main
import (
"log"
"strings"
"context"
"github.com/samkumar/reqcache"
"github.com/pborman/uuid"
)
type CollectionQuery struct {
uu uuid.Array
}
var permcache = reqcache.NewLRUCache(1024, queryCollection, nil)
func hasPermission(ctx context.Context, session *LoginSession, uuidBytes uuid.UUID) bool {
checkPublicGroup()
coll, err := permcache.Get(ctx, CollectionQuery{uu: uuidBytes.Array()})
if err != nil {
log.Fatalf("error getting from permcache: %v", err)
}
if session == nil {
for _, p := range publicGroup.Prefixes {
if strings.HasPrefix(coll.(string), p) {
return true
}
}
return false
}
for pfx, _ := range session.Prefixes {
if strings.HasPrefix(coll.(string), pfx) {
return true
}
}
return false
}
func queryCollection(ctx context.Context, key interface{}) (interface{}, uint64, error) {
query := key.(CollectionQuery)
s := btrdbConn.StreamFromUUID(query.uu.UUID())
coll, err := s.Collection(ctx)
if err != nil {
return nil, 0, err
}
return coll, 1, nil
}