/
acl.go
52 lines (44 loc) · 1.26 KB
/
acl.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
// Copyright 2012 The AEGo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package acl adds Access Control to entities
*/
package acl
import (
"appengine"
"appengine/datastore"
"github.com/gaego/ds"
"strings"
)
type Perm struct{}
func get(c appengine.Context, key *datastore.Key) (p *Perm, err error) {
p = &Perm{}
err = ds.Get(c, key, p)
return
}
func put(c appengine.Context, key *datastore.Key) (p *Perm, err error) {
p = &Perm{}
_, err = ds.Put(c, key, p)
return
}
func genID(objKey *datastore.Key, groupId, perm string) string {
return objKey.String() + "|" + groupId + "|" + strings.ToLower(perm)
}
func genKey(c appengine.Context, groupId, perm string, objKey *datastore.Key) *datastore.Key {
return datastore.NewKey(c, "Perm", genID(objKey, groupId, perm), 0, nil)
}
func Auth(c appengine.Context, groupId, perm string, objKey *datastore.Key) error {
key := genKey(c, groupId, perm, objKey)
if _, err := put(c, key); err != nil {
return err
}
return nil
}
func Can(c appengine.Context, groupId, perm string, objKey *datastore.Key) (bool, error) {
key := genKey(c, groupId, perm, objKey)
if _, err := get(c, key); err != nil {
return false, err
}
return true, nil
}