forked from juju/idmclient
/
permcheck.go
77 lines (68 loc) · 1.86 KB
/
permcheck.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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package idmclient
import (
"time"
"github.com/juju/utils/cache"
"gopkg.in/errgo.v1"
"github.com/juju/idmclient/params"
)
// PermChecker provides a way to query ACLs using the identity client.
type PermChecker struct {
cache *cache.Cache
client *Client
}
// NewPermChecker returns a permission checker
// that uses the given identity client to check permissions.
//
// It will cache results for at most cacheTime.
func NewPermChecker(c *Client, cacheTime time.Duration) *PermChecker {
return &PermChecker{
cache: cache.New(cacheTime),
client: c,
}
}
// Allow reports whether the given ACL admits the user with the given
// name. If the user does not exist and the ACL does not allow username
// or everyone, it will return (false, nil).
func (c *PermChecker) Allow(username string, acl []string) (bool, error) {
if len(acl) == 0 {
return false, nil
}
for _, name := range acl {
if name == "everyone" || name == username {
return true, nil
}
}
groups0, err := c.cache.Get(username, func() (interface{}, error) {
groups, err := c.client.UserGroups(¶ms.UserGroupsRequest{
Username: params.Username(username),
})
if err != nil && errgo.Cause(err) != params.ErrNotFound {
return nil, errgo.Mask(err)
}
groupMap := make(map[string]bool)
for _, g := range groups {
groupMap[g] = true
}
return groupMap, nil
})
if err != nil {
return false, errgo.Notef(err, "cannot fetch groups")
}
groups := groups0.(map[string]bool)
for _, a := range acl {
if groups[a] {
return true, nil
}
}
return false, nil
}
// CacheEvict evicts username from the cache.
func (c *PermChecker) CacheEvict(username string) {
c.cache.Evict(username)
}
// CacheEvictAll evicts everything from the cache.
func (c *PermChecker) CacheEvictAll() {
c.cache.EvictAll()
}