root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / permissions.py @ 235a4227
History | View | Annotate | Download (6.7 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 sqlalchemy.sql import select, literal |
35 |
from sqlalchemy.sql.expression import join, union |
36 |
|
37 |
from xfeatures import XFeatures |
38 |
from groups import Groups |
39 |
from public import Public |
40 |
|
41 |
from dbworker import ESCAPE_CHAR |
42 |
|
43 |
|
44 |
READ = 0
|
45 |
WRITE = 1
|
46 |
|
47 |
|
48 |
class Permissions(XFeatures, Groups, Public): |
49 |
|
50 |
def __init__(self, **params): |
51 |
XFeatures.__init__(self, **params)
|
52 |
Groups.__init__(self, **params)
|
53 |
Public.__init__(self, **params)
|
54 |
|
55 |
def access_grant(self, path, access, members=()): |
56 |
"""Grant members with access to path.
|
57 |
Members can also be '*' (all),
|
58 |
or some group specified as 'owner:group'."""
|
59 |
|
60 |
if not members: |
61 |
return
|
62 |
feature = self.xfeature_create(path)
|
63 |
self.feature_setmany(feature, access, members)
|
64 |
|
65 |
def access_set(self, path, permissions): |
66 |
"""Set permissions for path. The permissions dict
|
67 |
maps 'read', 'write' keys to member lists."""
|
68 |
|
69 |
r = permissions.get('read', [])
|
70 |
w = permissions.get('write', [])
|
71 |
if not r and not w: |
72 |
self.xfeature_destroy(path)
|
73 |
return
|
74 |
feature = self.xfeature_create(path)
|
75 |
self.feature_clear(feature, READ)
|
76 |
self.feature_clear(feature, WRITE)
|
77 |
if r:
|
78 |
self.feature_setmany(feature, READ, r)
|
79 |
if w:
|
80 |
self.feature_setmany(feature, WRITE, w)
|
81 |
|
82 |
def access_get(self, path): |
83 |
"""Get permissions for path."""
|
84 |
|
85 |
feature = self.xfeature_get(path)
|
86 |
if not feature: |
87 |
return {}
|
88 |
permissions = self.feature_dict(feature)
|
89 |
if READ in permissions: |
90 |
permissions['read'] = permissions[READ]
|
91 |
del(permissions[READ])
|
92 |
if WRITE in permissions: |
93 |
permissions['write'] = permissions[WRITE]
|
94 |
del(permissions[WRITE])
|
95 |
return permissions
|
96 |
|
97 |
def access_members(self, path): |
98 |
feature = self.xfeature_get(path)
|
99 |
if not feature: |
100 |
return []
|
101 |
permissions = self.feature_dict(feature)
|
102 |
members = set()
|
103 |
members.update(permissions.get(READ, [])) |
104 |
members.update(permissions.get(WRITE, [])) |
105 |
for m in set(members): |
106 |
parts = m.split(':', 1) |
107 |
if len(parts) != 2: |
108 |
continue
|
109 |
user, group = parts |
110 |
members.remove(m) |
111 |
members.update(self.group_members(user, group))
|
112 |
return list(members) |
113 |
|
114 |
def access_clear(self, path): |
115 |
"""Revoke access to path (both permissions and public)."""
|
116 |
|
117 |
self.xfeature_destroy(path)
|
118 |
self.public_unset(path)
|
119 |
|
120 |
def access_clear_bulk(self, paths): |
121 |
"""Revoke access to path (both permissions and public)."""
|
122 |
|
123 |
self.xfeature_destroy_bulk(paths)
|
124 |
self.public_unset_bulk(paths)
|
125 |
|
126 |
def access_check(self, path, access, member): |
127 |
"""Return true if the member has this access to the path."""
|
128 |
|
129 |
feature = self.xfeature_get(path)
|
130 |
if not feature: |
131 |
return False |
132 |
members = self.feature_get(feature, access)
|
133 |
if member in members or '*' in members: |
134 |
return True |
135 |
for owner, group in self.group_parents(member): |
136 |
if owner + ':' + group in members: |
137 |
return True |
138 |
return False |
139 |
|
140 |
def access_inherit(self, path): |
141 |
"""Return the paths influencing the access for path."""
|
142 |
|
143 |
# r = self.xfeature_inherit(path)
|
144 |
# if not r:
|
145 |
# return []
|
146 |
# # Compute valid.
|
147 |
# return [x[0] for x in r if x[0] in valid]
|
148 |
|
149 |
# Only keep path components.
|
150 |
parts = path.rstrip('/').split('/') |
151 |
valid = [] |
152 |
for i in range(1, len(parts)): |
153 |
subp = '/'.join(parts[:i + 1]) |
154 |
valid.append(subp) |
155 |
if subp != path:
|
156 |
valid.append(subp + '/')
|
157 |
return [x for x in valid if self.xfeature_get(x)] |
158 |
|
159 |
def access_list_paths(self, member, prefix=None): |
160 |
"""Return the list of paths granted to member."""
|
161 |
|
162 |
xfeatures_xfeaturevals = self.xfeatures.join(self.xfeaturevals) |
163 |
|
164 |
selectable = (self.groups.c.owner + ':' + self.groups.c.name) |
165 |
member_groups = select([selectable.label('value')],
|
166 |
self.groups.c.member == member)
|
167 |
|
168 |
members = select([literal(member).label('value')])
|
169 |
any = select([literal('*').label('value')]) |
170 |
|
171 |
u = union(member_groups, members, any).alias()
|
172 |
inner_join = join(xfeatures_xfeaturevals, u, |
173 |
self.xfeaturevals.c.value == u.c.value)
|
174 |
s = select([self.xfeatures.c.path], from_obj=[inner_join]).distinct()
|
175 |
if prefix:
|
176 |
s = s.where(self.xfeatures.c.path.like(
|
177 |
self.escape_like(prefix) + '%', escape=ESCAPE_CHAR |
178 |
)) |
179 |
r = self.conn.execute(s)
|
180 |
l = [row[0] for row in r.fetchall()] |
181 |
r.close() |
182 |
return l
|
183 |
|
184 |
def access_list_shared(self, prefix=''): |
185 |
"""Return the list of shared paths."""
|
186 |
|
187 |
s = select([self.xfeatures.c.path],
|
188 |
self.xfeatures.c.path.like(self.escape_like(prefix) + '%', |
189 |
escape=ESCAPE_CHAR |
190 |
) |
191 |
).order_by(self.xfeatures.c.path.asc())
|
192 |
r = self.conn.execute(s)
|
193 |
l = [row[0] for row in r.fetchall()] |
194 |
r.close() |
195 |
return l
|