Back to index

plone3  3.1.7
folder_localrole_delete.py
Go to the documentation of this file.
00001 ## Script (Python) "folder_localrole_delete"
00002 ##parameters=member_ids=(), member_role_ids=[]
00003 ##title=Delete local roles
00004 ##
00005 
00006 from Products.CMFPlone.utils import transaction_note
00007 from Products.CMFPlone import PloneMessageFactory as _
00008 
00009 pm = context.portal_membership
00010 pu = context.plone_utils
00011 
00012 reindex=False
00013 # first look for members
00014 if len(member_ids)>0:
00015     reindex=True
00016     pm.deleteLocalRoles( obj=context,
00017                          member_ids=member_ids,
00018                       reindex=False,
00019                       REQUEST=context.REQUEST)
00020 
00021 def parseMemberRoleString( s ):
00022     sidx = s.find('((')
00023     eidx = s.find('))')
00024     if sidx == -1 or eidx == -1:
00025         return None
00026     return s[:sidx], s[sidx+2:eidx]
00027 
00028 #
00029 # second look for certain roles
00030 #
00031 if len(member_role_ids)>0:
00032     reindex=True
00033 
00034     # get all local roles 
00035     local_roles=context.acl_users.getLocalRolesForDisplay(context)
00036 
00037     # sort members first
00038     members={}
00039     for s in member_role_ids:
00040         member_role = parseMemberRoleString(s)
00041         if member_role is None: 
00042             continue
00043         member,role=member_role
00044         members.setdefault(member,[]).append(role)
00045 
00046     # now process the dictionary of the form {member: roles to delete}
00047     for member_id, roles_to_delete in members.items():
00048         # filter the actual member roles from the local roles
00049         roles_for_member=[]
00050         for role in local_roles:
00051             if role[3]==member_id:
00052                 roles_for_member.extend(list(role[1]))
00053 
00054         newRoles=[r for r in roles_for_member if r not in roles_to_delete] 
00055 
00056         # delete all roles for that member
00057         pm.deleteLocalRoles( obj=context,
00058                              member_ids=(member_id,),
00059                           reindex=False,
00060                           REQUEST=context.REQUEST)
00061 
00062         # add the other roles again
00063         for role in newRoles:
00064             pm.setLocalRoles( obj=context,
00065                           member_ids=(member_id,),
00066                           member_role=role,
00067                        reindex=False)
00068 
00069 if reindex:
00070     context.reindexObjectSecurity()
00071 
00072 transaction_note('Modified sharing for folder %s at %s' % (context.title_or_id(), context.absolute_url()))
00073 context.plone_utils.addPortalMessage(_(u'Local roles deleted.'))
00074 
00075 context.REQUEST.RESPONSE.redirect(context.absolute_url() + '/folder_localrole_form')