Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / sqlalchemy / permissions.py @ 7759260d

History | View | Annotate | Download (5.5 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 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

    
42
READ = 0
43
WRITE = 1
44

    
45

    
46
class Permissions(XFeatures, Groups, Public):
47
    
48
    def __init__(self, **params):
49
        XFeatures.__init__(self, **params)
50
        Groups.__init__(self, **params)
51
        Public.__init__(self, **params)
52
    
53
    def access_grant(self, path, access, members=()):
54
        """Grant members with access to path.
55
           Members can also be '*' (all),
56
           or some group specified as 'owner:group'."""
57
        
58
        if not members:
59
            return
60
        feature = self.xfeature_create(path)
61
        if feature is None:
62
            return
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
        self.xfeature_destroy(path)
70
        self.access_grant(path, READ, permissions.get('read', []))
71
        self.access_grant(path, WRITE, permissions.get('write', []))
72
    
73
    def access_clear(self, path):
74
        """Revoke access to path (both permissions and public)."""
75
        
76
        self.xfeature_destroy(path)
77
        self.public_unset(path)
78
    
79
    def access_check(self, path, access, member):
80
        """Return true if the member has this access to the path."""
81
        
82
        if access == READ and self.public_get(path) is not None:
83
            return True
84
        
85
        r = self.xfeature_inherit(path)
86
        if not r:
87
            return False
88
        fpath, feature = r
89
        members = self.feature_get(feature, access)
90
        if member in members or '*' in members:
91
            return True
92
        for owner, group in self.group_parents(member):
93
            if owner + ':' + group in members:
94
                return True
95
        return False
96
    
97
    def access_inherit(self, path):
98
        """Return the inherited or assigned (path, permissions) pair for path."""
99
        
100
        r = self.xfeature_inherit(path)
101
        if not r:
102
            return (path, {})
103
        fpath, feature = r
104
        permissions = self.feature_dict(feature)
105
        if READ in permissions:
106
            permissions['read'] = permissions[READ]
107
            del(permissions[READ])
108
        if WRITE in permissions:
109
            permissions['write'] = permissions[WRITE]
110
            del(permissions[WRITE])
111
        return (fpath, permissions)
112
    
113
    def access_list(self, path):
114
        """List all permission paths inherited by or inheriting from path."""
115
        
116
        return [x[0] for x in self.xfeature_list(path) if x[0] != path]
117
    
118
    def access_list_paths(self, member, prefix=None):
119
        """Return the list of paths granted to member."""
120
        
121
        xfeatures_xfeaturevals =  self.xfeatures.join(self.xfeaturevals)
122
        
123
        selectable = (self.groups.c.owner + ':' + self.groups.c.name)
124
        member_groups = select([selectable.label('value')],
125
            self.groups.c.member == member)
126
        
127
        members = select([literal(member).label('value')])
128
        any = select([literal('*').label('value')])
129
        
130
        u = union(member_groups, members, any).alias()
131
        inner_join = join(xfeatures_xfeaturevals, u,
132
                    self.xfeaturevals.c.value == u.c.value)
133
        s = select([self.xfeatures.c.path], from_obj=[inner_join]).distinct()
134
        if prefix:
135
            s = s.where(self.xfeatures.c.path.like(self.escape_like(prefix) + '%', escape='\\'))
136
        r = self.conn.execute(s)
137
        l = [row[0] for row in r.fetchall()]
138
        r.close()
139
        return l
140
    
141
    def access_list_shared(self, prefix=''):
142
        """Return the list of shared paths."""
143
        
144
        s = select([self.xfeatures.c.path],
145
            self.xfeatures.c.path.like(self.escape_like(prefix) + '%', escape='\\')).order_by(self.xfeatures.c.path.asc())
146
        r = self.conn.execute(s)
147
        l = [row[0] for row in r.fetchall()]
148
        r.close()
149
        return l