~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/idmclient/permcheck.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package idmclient
 
5
 
 
6
import (
 
7
        "time"
 
8
 
 
9
        "gopkg.in/errgo.v1"
 
10
)
 
11
 
 
12
// PermChecker provides a way to query ACLs using the identity client.
 
13
type PermChecker struct {
 
14
        cache *GroupCache
 
15
}
 
16
 
 
17
// NewPermChecker returns a permission checker
 
18
// that uses the given identity client to check permissions.
 
19
//
 
20
// It will cache results for at most cacheTime.
 
21
func NewPermChecker(c *Client, cacheTime time.Duration) *PermChecker {
 
22
        return &PermChecker{
 
23
                cache: NewGroupCache(c, cacheTime),
 
24
        }
 
25
}
 
26
 
 
27
// NewPermCheckerWithCache returns a new PermChecker using
 
28
// the given cache for its group queries.
 
29
func NewPermCheckerWithCache(cache *GroupCache) *PermChecker {
 
30
        return &PermChecker{
 
31
                cache: cache,
 
32
        }
 
33
}
 
34
 
 
35
// Allow reports whether the given ACL admits the user with the given
 
36
// name. If the user does not exist and the ACL does not allow username
 
37
// or everyone, it will return (false, nil).
 
38
func (c *PermChecker) Allow(username string, acl []string) (bool, error) {
 
39
        if len(acl) == 0 {
 
40
                return false, nil
 
41
        }
 
42
        for _, name := range acl {
 
43
                if name == "everyone" || name == username {
 
44
                        return true, nil
 
45
                }
 
46
        }
 
47
        groups, err := c.cache.groupMap(username)
 
48
        if err != nil {
 
49
                return false, errgo.Mask(err)
 
50
        }
 
51
        for _, a := range acl {
 
52
                if groups[a] {
 
53
                        return true, nil
 
54
                }
 
55
        }
 
56
        return false, nil
 
57
}
 
58
 
 
59
// CacheEvict evicts username from the cache.
 
60
func (c *PermChecker) CacheEvict(username string) {
 
61
        c.cache.CacheEvict(username)
 
62
}
 
63
 
 
64
// CacheEvictAll evicts everything from the cache.
 
65
func (c *PermChecker) CacheEvictAll() {
 
66
        c.cache.CacheEvictAll()
 
67
}