Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/webdav/acp/GrantDeny.py
blob: 52c9b934a900e70c46d04df84dee2dd2dde5e8f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# Copyright 2008 German Aerospace Center (DLR)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Handling of grant and deny clauses in ACEs according to WebDAV ACP specification.
"""


from webdav.acp.Privilege import Privilege
from webdav import Constants
from webdav.Connection import WebdavError


__version__ = "$LastChangedRevision$"


class GrantDeny(object):
    """
    This class provides functionality for handling
    grant and deny clauses in ACEs.
    
    @ivar grantDeny:  Flag indicating whether clause grants or denies.
    @type grantDeny:  C{bool}
    @ivar privileges: Privileges to be granted or denied.
    @type privileges: C{list} of L{Privilege} objects
    """
    
    def __init__(self, domroot=None):
        """
        Constructor should be called with either no parameters 
        (create blank GrantDeny), or one parameter (a DOM tree).
        
        @param domroot:     A DOM tree (default: None).
        @type  domroot:     L{webdav.WebdavResponse.Element} object
        
        @raise WebdavError: When non-valid parameters are passed a L{WebdavError} is raised.
        """
        self.grantDeny  = 0   # 0: deny, 1: grant
        self.privileges = []
        
        if domroot:
            self.grantDeny = (domroot.name == Constants.TAG_GRANT)
            for child in domroot.children:
                if child.name == Constants.TAG_PRIVILEGE and child.ns == Constants.NS_DAV:
                    self.privileges.append(Privilege(domroot=child))
                else:
                    # This shouldn't happen, someone screwed up with the params ...
                    raise WebdavError('Non-privilege tag handed to GrantDeny constructor: %s' \
                        % child.name)
        elif domroot == None:
            # no param ==> blank object
            pass
        else:
            # This shouldn't happen, someone screwed up with the params ...
            raise WebdavError('Non-valid parameters handed to GrantDeny constructor.')

    def __cmp__(self, other):
        """ Compares two GrantDeny instances. """
        if not isinstance(other, GrantDeny):
            return 1
        if self.grantDeny == other.grantDeny:
            equal = 1
            for priv in self.privileges:
                inList = 0
                for otherPriv in other.privileges:
                    if priv == otherPriv:
                        inList = 1
                if inList == 0:
                    equal = 0
            return not equal
        else:
            return 1

    def __repr__(self):
        """ Returns the representation of an instance. """
        representation = '<class GrantDeny: '
        if self.grantDeny:
            representation += 'grant privileges: ['
        else:
            representation += 'deny privileges: ['
        first = 1
        for priv in self.privileges:
            if first:
                representation += '%s' % priv
                first = 0
            else:
                representation += ', %s' % priv
        return '%s]>' % (representation)

    def copy(self, other):
        """
        Copy a GrantDeny object.
        
        @param other: Another grant or deny clause to copy.
        @type  other: L{GrantDeny} object
        
        @raise WebdavError: When an object that is not an L{GrantDeny} is passed 
            a L{WebdavError} is raised.
        """
        if not isinstance(other, GrantDeny):
            raise WebdavError('Non-GrantDeny object passed to copy method: %s' \
                % other)
        self.grantDeny = other.grantDeny
        if other.privileges:
            self.addPrivileges(other.privileges)

    def isGrant(self):
        """
        Returns whether the set of privileges is of type "grant"
        indicating true or false.
        
        @return: Value whether the clause is of grant type.
        @rtype:  C{bool}
        """
        return self.grantDeny

    def isDeny(self):
        """
        Returns whether the set of privileges is of type "deny"
        indicating true or false.
        
        @return: Value whether the clause is of deny type.
        @rtype:  C{bool}
        """
        return not self.grantDeny

    def setGrantDeny(self, grantDeny):
        """
        Sets the set of privileges to given value for grantDeny.
        
        @param grantDeny: Grant/deny value for clause (grant: True/1, deny: False/0).
        @type  grantDeny: C{bool}
        """
        if grantDeny == 0 or grantDeny == 1:
            self.grantDeny = grantDeny

    def setGrant(self):
        """ Sets the set of privileges to type "grant". """
        self.grantDeny = 1

    def setDeny(self):
        """ Sets the set of privileges to type "deny". """
        self.grantDeny = 0

    def isAll(self):
        """
        Checks whether the privileges contained are equal
        to aggregate DAV:all privilege.
        
        @return: Value whether all un-aggregated privileges are present.
        @rtype:  C{bool}
        """
        
        if len(self.privileges) == 1 and self.privileges[0].name == Constants.TAG_ALL:
            return 1
        return 0

    def addPrivilege(self, privilege):
        """
        Adds the passed privilege to list if it's not in it, yet.
        
        @param privilege: A privilege.
        @type  privilege: L{Privilege} object
        """
        inList = False
        for priv in self.privileges:
            if priv == privilege:
                inList = True
        if not inList:
            newPrivilege = Privilege()
            newPrivilege.copy(privilege)
            self.privileges.append(newPrivilege)

    def addPrivileges(self, privileges):
        """
        Adds the list of passed privileges to list.
        
        @param privileges: Several privileges.
        @type  privileges: sequence of L{Privilege} objects
        """
        for priv in privileges:
            self.addPrivilege(priv)

    def delPrivilege(self, privilege):
        """
        Deletes the passed privilege from list if it's in it.
        
        @param privilege: A privilege.
        @type  privilege: L{Privilege} object
        
        @raise WebdavError: A L{WebdavError} is raised if the privilege to be 
            deleted is not present.
        """
        count = 0
        index = 0
        for priv in self.privileges:
            count += 1
            if priv == privilege:
                index = count
        if index:
            self.privileges.pop(index - 1)
        else:
            raise WebdavError('Privilege to be deleted not in list: %s' % privilege)

    def delPrivileges(self, privileges):
        """
        Deletes the list of passed privileges from list.
        
        @param privileges: Several privileges.
        @type  privileges: sequence of L{Privilege} objects
        """
        for priv in privileges:
            self.delPrivilege(priv)

    def toXML(self):
        """
        Returns string of GrantDeny content to valid XML as described in WebDAV ACP.
        """
        assert self.privileges, "GrantDeny object is not initialized or does not contain content!"
        
        if self.isGrant():
            tag = 'D:' + Constants.TAG_GRANT
        else:
            tag = 'D:' + Constants.TAG_DENY
            
        res = ''
        for privilege in self.privileges:
            res += privilege.toXML()
        return '<%s>%s</%s>' % (tag, res, tag)