Revision d50ed8d4 snf-pithos-backend/pithos/backends/lib/sqlite/permissions.py

b/snf-pithos-backend/pithos/backends/lib/sqlite/permissions.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
41 41

  
42 42

  
43 43
class Permissions(XFeatures, Groups, Public):
44
    
44

  
45 45
    def __init__(self, **params):
46 46
        XFeatures.__init__(self, **params)
47 47
        Groups.__init__(self, **params)
48 48
        Public.__init__(self, **params)
49
    
49

  
50 50
    def access_grant(self, path, access, members=()):
51 51
        """Grant members with access to path.
52 52
           Members can also be '*' (all),
53 53
           or some group specified as 'owner:group'."""
54
        
54

  
55 55
        if not members:
56 56
            return
57 57
        feature = self.xfeature_create(path)
58 58
        self.feature_setmany(feature, access, members)
59
    
59

  
60 60
    def access_set(self, path, permissions):
61 61
        """Set permissions for path. The permissions dict
62 62
           maps 'read', 'write' keys to member lists."""
63
        
63

  
64 64
        r = permissions.get('read', [])
65 65
        w = permissions.get('write', [])
66 66
        if not r and not w:
......
73 73
            self.feature_setmany(feature, READ, r)
74 74
        if w:
75 75
            self.feature_setmany(feature, WRITE, w)
76
    
76

  
77 77
    def access_get(self, path):
78 78
        """Get permissions for path."""
79
        
79

  
80 80
        feature = self.xfeature_get(path)
81 81
        if not feature:
82 82
            return {}
......
88 88
            permissions['write'] = permissions[WRITE]
89 89
            del(permissions[WRITE])
90 90
        return permissions
91
    
91

  
92 92
    def access_members(self, path):
93 93
        feature = self.xfeature_get(path)
94 94
        if not feature:
......
105 105
            members.remove(m)
106 106
            members.update(self.group_members(user, group))
107 107
        return members
108
    
108

  
109 109
    def access_clear(self, path):
110 110
        """Revoke access to path (both permissions and public)."""
111
        
111

  
112 112
        self.xfeature_destroy(path)
113 113
        self.public_unset(path)
114
    
114

  
115 115
    def access_clear_bulk(self, paths):
116 116
        """Revoke access to path (both permissions and public)."""
117
        
117

  
118 118
        self.xfeature_destroy_bulk(paths)
119 119
        self.public_unset_bulk(paths)
120
    
120

  
121 121
    def access_check(self, path, access, member):
122 122
        """Return true if the member has this access to the path."""
123
        
123

  
124 124
        feature = self.xfeature_get(path)
125 125
        if not feature:
126 126
            return False
......
131 131
            if owner + ':' + group in members:
132 132
                return True
133 133
        return False
134
    
134

  
135 135
    def access_inherit(self, path):
136 136
        """Return the paths influencing the access for path."""
137
        
137

  
138 138
#         r = self.xfeature_inherit(path)
139 139
#         if not r:
140 140
#             return []
141 141
#         # Compute valid.
142 142
#         return [x[0] for x in r if x[0] in valid]
143
        
143

  
144 144
        # Only keep path components.
145 145
        parts = path.rstrip('/').split('/')
146 146
        valid = []
......
150 150
            if subp != path:
151 151
                valid.append(subp + '/')
152 152
        return [x for x in valid if self.xfeature_get(x)]
153
    
153

  
154 154
    def access_list_paths(self, member, prefix=None):
155 155
        """Return the list of paths granted to member."""
156
        
156

  
157 157
        q = ("select distinct path from xfeatures inner join "
158 158
             "   (select distinct feature_id, key from xfeaturevals inner join "
159 159
             "      (select owner || ':' || name as value from groups "
......
166 166
            p += (self.escape_like(prefix) + '%',)
167 167
        self.execute(q, p)
168 168
        return [r[0] for r in self.fetchall()]
169
    
169

  
170 170
    def access_list_shared(self, prefix=''):
171 171
        """Return the list of shared paths."""
172
        
172

  
173 173
        q = "select path from xfeatures where path like ? escape '\\'"
174 174
        self.execute(q, (self.escape_like(prefix) + '%',))
175 175
        return [r[0] for r in self.fetchall()]

Also available in: Unified diff