@@ -54,6 +54,28 @@ def test(self, db, permission, classname, property, userid, itemid):
5454 # we have a winner
5555 return 1
5656
57+ def searchable(self, db, permission, classname, property):
58+ """ A Permission is searchable for the given permission if it
59+ doesn't include a check method and otherwise matches the
60+ given parameters.
61+ """
62+ if permission != self.name:
63+ return 0
64+
65+ # are we checking the correct class
66+ if self.klass != classname:
67+ return 0
68+
69+ # what about property?
70+ if not self._properties_dict[property]:
71+ return 0
72+
73+ if self.check:
74+ return 0
75+
76+ return 1
77+
78+
5779 def __repr__(self):
5880 return '<Permission 0x%x %r,%r,%r,%r>'%(id(self), self.name,
5981 self.klass, self.properties, self.check)
@@ -175,6 +197,44 @@ def hasPermission(self, permission, userid, classname=None,
175197 return 1
176198 return 0
177199
200+ def roleHasSearchPermission(self, rolename, classname, property):
201+ """ for each of the user's Roles, check the permissions
202+ """
203+ for perm in self.role[rolename].permissions:
204+ # permission match?
205+ for p in 'View', 'Search':
206+ if perm.searchable(self.db, p, classname, property):
207+ return 1
208+ return 0
209+
210+ def hasSearchPermission(self, userid, classname, property):
211+ '''Look through all the Roles, and hence Permissions, and
212+ see if "permission" exists given the constraints of
213+ classname and property.
214+
215+ A search permission is granted if we find a 'View' or
216+ 'Search' permission for the user which does *not* include
217+ a check function. If such a permission is found, the user may
218+ search for the given property in the given class.
219+
220+ Note that classname *and* property are mandatory arguments.
221+
222+ Contrary to hasPermission, the search will *not* match if
223+ there are additional constraints (namely a search function)
224+ on a Permission found.
225+
226+ Concerning property, the Permission matched must have
227+ either no properties listed or the property must appear in
228+ the list.
229+ '''
230+ for rolename in self.db.user.get_roles(userid):
231+ if not rolename or not self.role.has_key(rolename):
232+ continue
233+ # for each of the user's Roles, check the permissions
234+ if self.roleHasSearchPermission (rolename, classname, property):
235+ return 1
236+ return 0
237+
178238 def addPermission(self, **propspec):
179239 ''' Create a new Permission with the properties defined in
180240 'propspec'. See the Permission class for the possible
@@ -208,4 +268,22 @@ def addPermissionToRole(self, rolename, permission, classname=None,
208268 role = self.role[rolename.lower()]
209269 role.permissions.append(permission)
210270
271+ # Convenience methods for removing non-allowed properties from a
272+ # filterspec or sort/group list
273+
274+ def filterFilterspec(self, userid, classname, filterspec):
275+ """ Return a filterspec that has all non-allowed properties removed.
276+ """
277+ return dict ([(k, v) for k, v in filterspec.iteritems()
278+ if self.hasSearchPermission(userid,classname,k)])
279+
280+ def filterSortspec(self, userid, classname, sort):
281+ """ Return a sort- or group-list that has all non-allowed properties
282+ removed.
283+ """
284+ if isinstance(sort, tuple) and sort[0] in '+-':
285+ sort = [sort]
286+ return [(d, p) for d, p in sort
287+ if self.hasSearchPermission(userid,classname,p)]
288+
211289# vim: set filetype=python sts=4 sw=4 et si :
0 commit comments