Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / permissions.py @ 6f4bce7b

History | View | Annotate | Download (4.2 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
# 
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
# 
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
# 
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
# 
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from xfeatures import XFeatures
35
from groups import Groups
36
from public import Public
37

    
38

    
39
READ = 0
40
WRITE = 1
41

    
42

    
43
class Permissions(XFeatures, Groups, Public):
44
    
45
    def __init__(self, **params):
46
        XFeatures.__init__(self, **params)
47
        Groups.__init__(self, **params)
48
        Public.__init__(self, **params)
49
    
50
    def access_grant(self, path, access, members=()):
51
        """Grant members with access to path."""
52
        
53
        feature = self.xfeature_create(path)
54
        if feature is None:
55
            return
56
        self.feature_setmany(feature, access, members)
57
    
58
    def access_revoke_all(self, path):
59
        """Revoke access to path."""
60
        
61
        self.xfeature_destroy(path)
62
    
63
    def access_check(self, path, access, member):
64
        """Return true if the member has this access to the path."""
65
        
66
        if access == READ and self.public_check(path):
67
            return True
68
        
69
        r = self.xfeature_inherit(path)
70
        if not r:
71
            return False
72
        fpath, feature = r
73
        members = self.feature_get(feature, access)
74
        if member in members or '*' in members:
75
            return True
76
        for owner, group in self.group_parents(self, member):
77
            if owner + ':' + group in members:
78
                return True
79
        return True
80
    
81
    def access_inherit(self, path):
82
        """Return the inherited or assigned (path, permissions) pair for path."""
83
        
84
        r = self.xfeature_inherit(path)
85
        if not r:
86
            return (path, {})
87
        fpath, feature = r
88
        return (fpath, self.feature_dict(feature))
89
    
90
    def access_list(self, path):
91
        """List all permission paths inherited by or inheriting from path."""
92
        
93
        return [x[0] for x in self.xfeature_list(path) if x[0] != path]
94
    
95
    def access_list_paths(self, member, prefix=None):
96
        """Return the list of paths granted to member."""
97
        
98
        q = ("select distinct path from xfeatures inner join "
99
             "   (select distinct feature_id, key from xfeaturevals inner join "
100
             "      (select owner || ':' || name as value from members "
101
             "       where member = ? union select ?) "
102
             "    using (value)) "
103
             "using (feature_id)")
104
        p = (member, member)
105
        if prefix:
106
            q += " where path like ?"
107
            p += (prefix + '%',)
108
        self.execute(q, p)
109
        return [r[0] for r in self.fetchall()]
110
    
111
    def access_list_shared(self, prefix=''):
112
        """Return the list of shared paths."""
113
        
114
        q = "select path from xfeatures where path like ?"
115
        self.execute(q, (prefix + '%',))
116
        return [r[0] for r in self.fetchall()]