Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / sqlite / permissions.py @ 5e068361

History | View | Annotate | Download (5.3 kB)

1
# Copyright 2011-2012 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
           Members can also be '*' (all),
53
           or some group specified as 'owner:group'."""
54
        
55
        if not members:
56
            return
57
        feature = self.xfeature_create(path)
58
        self.feature_setmany(feature, access, members)
59
    
60
    def access_set(self, path, permissions):
61
        """Set permissions for path. The permissions dict
62
           maps 'read', 'write' keys to member lists."""
63
        
64
        r = permissions.get('read', [])
65
        w = permissions.get('write', [])
66
        if not r and not w:
67
            self.xfeature_destroy(path)
68
            return
69
        feature = self.xfeature_create(path)
70
        self.feature_clear(feature)
71
        if r:
72
            self.feature_setmany(feature, READ, r)
73
        if w:
74
            self.feature_setmany(feature, WRITE, w)
75
    
76
    def access_clear(self, path):
77
        """Revoke access to path (both permissions and public)."""
78
        
79
        self.xfeature_destroy(path)
80
        self.public_unset(path)
81
    
82
    def access_check(self, path, access, member):
83
        """Return true if the member has this access to the path."""
84
        
85
        if access == READ and self.public_get(path) is not None:
86
            return True
87
        
88
        feature = self.xfeature_get(path)
89
        if not feature:
90
            return False
91
        members = self.feature_get(feature, access)
92
        if member in members or '*' in members:
93
            return True
94
        for owner, group in self.group_parents(member):
95
            if owner + ':' + group in members:
96
                return True
97
        return False
98
    
99
    def access_inherit(self, path):
100
        """Return the paths influencing the access for path."""
101
        
102
        r = self.xfeature_inherit(path)
103
        if not r:
104
            return []
105
        
106
        def get_permissions(feature):
107
            permissions = self.feature_dict(feature)
108
            if READ in permissions:
109
                permissions['read'] = permissions[READ]
110
                del(permissions[READ])
111
            if WRITE in permissions:
112
                permissions['write'] = permissions[WRITE]
113
                del(permissions[WRITE])
114
            return permissions
115
        
116
        # Only keep path components.
117
        parts = path.rstrip('/').split('/')
118
        valid = []
119
        for i in range(1, len(parts)):
120
            subp = '/'.join(parts[:i + 1])
121
            valid.append(subp)
122
            valid.append(subp + '/')
123
        return [(x[0], get_permissions(x[1])) for x in r if x[0] in valid]
124
    
125
    def access_list_paths(self, member, prefix=None):
126
        """Return the list of paths granted to member."""
127
        
128
        q = ("select distinct path from xfeatures inner join "
129
             "   (select distinct feature_id, key from xfeaturevals inner join "
130
             "      (select owner || ':' || name as value from groups "
131
             "       where member = ? union select ? union select '*') "
132
             "    using (value)) "
133
             "using (feature_id)")
134
        p = (member, member)
135
        if prefix:
136
            q += " where path like ? escape '\\'"
137
            p += (self.escape_like(prefix) + '%',)
138
        self.execute(q, p)
139
        return [r[0] for r in self.fetchall()]
140
    
141
    def access_list_shared(self, prefix=''):
142
        """Return the list of shared paths."""
143
        
144
        q = "select path from xfeatures where path like ? escape '\\'"
145
        self.execute(q, (self.escape_like(prefix) + '%',))
146
        return [r[0] for r in self.fetchall()]