Commit 2f47d8c7 authored by Romain Courteaud's avatar Romain Courteaud

Add checked_permission parameter to setCategoryMembership


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@16546 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 7db4f4cc
...@@ -587,7 +587,8 @@ class CategoryTool( UniqueObject, Folder, Base ): ...@@ -587,7 +587,8 @@ class CategoryTool( UniqueObject, Folder, Base ):
security.declareProtected( Permissions.AccessContentsInformation, 'setCategoryMembership' ) security.declareProtected( Permissions.AccessContentsInformation, 'setCategoryMembership' )
def setCategoryMembership(self, context, base_category_list, category_list, base=0, keep_default=1, def setCategoryMembership(self, context, base_category_list, category_list, base=0, keep_default=1,
spec=(), filter=None, **kw ): spec=(), filter=None,
checked_permission=None, **kw ):
""" """
Sets the membership of the context on the specified base_category Sets the membership of the context on the specified base_category
list and for the specified portal_type spec list and for the specified portal_type spec
...@@ -601,28 +602,46 @@ class CategoryTool( UniqueObject, Folder, Base ): ...@@ -601,28 +602,46 @@ class CategoryTool( UniqueObject, Folder, Base ):
spec -- a list or a tuple of portal types spec -- a list or a tuple of portal types
checked_permission -- a string which defined the permission
to filter the object on
""" """
#LOG("set Category 1",0,str(category_list)) # LOG("CategoryTool, setCategoryMembership", 0 ,
# 'category_list: %s' % str(category_list))
# XXX We must use filters in the future # XXX We must use filters in the future
# where_expression = self._buildQuery(spec, filter, kw) # where_expression = self._buildQuery(spec, filter, kw)
portal_type = kw.get('portal_type', ())
if isinstance(portal_type, str):
portal_type = (portal_type,)
if spec is (): if spec is ():
portal_type = kw.get('portal_type', ())
if isinstance(portal_type, str):
portal_type = (portal_type,)
spec = portal_type spec = portal_type
default_dict = {}
self._cleanupCategories(context) self._cleanupCategories(context)
if isinstance(category_list, str): if isinstance(category_list, str):
category_list = (category_list,) category_list = (category_list, )
elif category_list is None: elif category_list is None:
category_list = () category_list = ()
elif isinstance(category_list, (list, tuple)): elif isinstance(category_list, (list, tuple)):
pass pass
else: else:
raise TypeError, 'Category must be of string, tuple of string or list of string type.' raise TypeError, 'Category must be of string, tuple of string ' \
'or list of string type.'
if isinstance(base_category_list, str): if isinstance(base_category_list, str):
base_category_list = [base_category_list] base_category_list = (base_category_list, )
# Build the ckecked_permission filter
if checked_permission is not None:
checkPermission = self.portal_membership.checkPermission
def permissionFilter(obj):
if checkPermission(checked_permission, obj):
return 0
else:
return 1
new_category_list = [] new_category_list = []
default_dict = {}
for path in self._getCategoryList(context): for path in self._getCategoryList(context):
my_base_id = self.getBaseCategoryId(path) my_base_id = self.getBaseCategoryId(path)
if my_base_id not in base_category_list: if my_base_id not in base_category_list:
...@@ -630,17 +649,19 @@ class CategoryTool( UniqueObject, Folder, Base ): ...@@ -630,17 +649,19 @@ class CategoryTool( UniqueObject, Folder, Base ):
# specified list of base_category ids # specified list of base_category ids
new_category_list.append(path) new_category_list.append(path)
else: else:
if spec is (): keep_it = 0
# If spec is (), then we should keep nothing if (spec is not ()) or (checked_permission is not None):
# Everything will be replaced obj = self.unrestrictedTraverse(path, None)
keep_it = 0 if obj is not None:
else: if spec is not ():
# Only keep this if not in our spec # If spec is (), then we should keep nothing
try: # Everything will be replaced
my_type = self.unrestrictedTraverse(path).portal_type # If spec is not (), Only keep this if not in our spec
keep_it = (my_type not in spec) my_type = obj.portal_type
except (KeyError, AttributeError): keep_it = (my_type not in spec)
keep_it = 0 if (not keep_it) and (checked_permission is not None):
keep_it = permissionFilter(obj)
if keep_it: if keep_it:
new_category_list.append(path) new_category_list.append(path)
elif keep_default: elif keep_default:
...@@ -669,13 +690,16 @@ class CategoryTool( UniqueObject, Folder, Base ): ...@@ -669,13 +690,16 @@ class CategoryTool( UniqueObject, Folder, Base ):
default_path_found[path] = 1 default_path_found[path] = 1
new_category_list.append(path) new_category_list.append(path)
else: else:
new_path = base_category_list[0] + '/' + path new_path = '%s/%s' % (base_category_list[0], path)
if new_path not in default_new_category_list: if new_path not in default_new_category_list:
new_category_list.append(new_path) new_category_list.append(new_path)
#LOG("set Category",0,str(new_category_list)) # LOG("CategoryTool, setCategoryMembership", 0 ,
#LOG("set Category",0,str(default_new_category_list)) # 'new_category_list: %s' % str(new_category_list))
# LOG("CategoryTool, setCategoryMembership", 0 ,
# 'default_new_category_list: %s' % str(default_new_category_list))
self._setCategoryList(context, tuple(default_new_category_list + new_category_list)) self._setCategoryList(context, tuple(default_new_category_list + new_category_list))
security.declareProtected( Permissions.AccessContentsInformation, 'setDefaultCategoryMembership' ) security.declareProtected( Permissions.AccessContentsInformation, 'setDefaultCategoryMembership' )
def setDefaultCategoryMembership(self, context, base_category, default_category, def setDefaultCategoryMembership(self, context, base_category, default_category,
spec=(), filter=None, spec=(), filter=None,
...@@ -694,6 +718,9 @@ class CategoryTool( UniqueObject, Folder, Base ): ...@@ -694,6 +718,9 @@ class CategoryTool( UniqueObject, Folder, Base ):
spec -- a list or a tuple of portal types spec -- a list or a tuple of portal types
checked_permission -- a string which defined the permission
to filter the object on
""" """
self._cleanupCategories(context) self._cleanupCategories(context)
if isinstance(default_category, (tuple, list)): if isinstance(default_category, (tuple, list)):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment