Back to index

plone3  3.1.7
MemberDataTool.py
Go to the documentation of this file.
00001 from Products.CMFCore import MemberDataTool
00002 from Products.CMFCore.MemberDataTool import MemberDataTool as BaseTool
00003 from Products.CMFPlone import ToolNames
00004 from Globals import InitializeClass
00005 from AccessControl import ClassSecurityInfo
00006 from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
00007 from Products.CMFCore.utils import getToolByName
00008 from Products.CMFPlone.PloneBaseTool import PloneBaseTool
00009 from Products.CMFCore.permissions import ManagePortal
00010 
00011 class MemberDataTool(PloneBaseTool, BaseTool):
00012 
00013     meta_type = ToolNames.MemberDataTool
00014     security = ClassSecurityInfo()
00015     toolicon = 'skins/plone_images/user.gif'
00016 
00017     __implements__ = (PloneBaseTool.__implements__, BaseTool.__implements__, )
00018 
00019 
00020     def __init__(self):
00021         BaseTool.__init__(self)
00022         self.portraits=BTreeFolder2(id='portraits')
00023 
00024     def _getPortrait(self, member_id):
00025         "return member_id's portrait if you can "
00026         return self.portraits.get(member_id, None)
00027 
00028     def _setPortrait(self, portrait, member_id):
00029         " store portrait which must be a raw image in _portrais "
00030         if self.portraits.has_key(member_id):
00031             self.portraits._delObject(member_id)
00032         self.portraits._setObject(id= member_id, object=portrait)
00033 
00034     def _deletePortrait(self, member_id):
00035         " remove member_id's portrait "
00036         if self.portraits.has_key(member_id):
00037             self.portraits._delObject(member_id)
00038 
00039     security.declarePrivate('pruneMemberDataContents')
00040     def pruneMemberDataContents(self):
00041         '''
00042         Compare the user IDs stored in the member data
00043         tool with the list in the actual underlying acl_users
00044         and delete anything not in acl_users
00045         '''
00046         BaseTool.pruneMemberDataContents(self)
00047         membertool= getToolByName(self, 'portal_membership')
00048         portraits   = self.portraits
00049         user_list = membertool.listMemberIds()
00050 
00051         for tuple in portraits.items():
00052             member_id = tuple[0]
00053             member_obj  = tuple[1]
00054             if member_id not in user_list:
00055                 self.portraits._delObject(member_id)
00056 
00057     security.declareProtected(ManagePortal, 'purgeMemberDataContents')
00058     def purgeMemberDataContents(self):
00059         '''
00060         Delete ALL MemberData information. This is required for us as we change the
00061         MemberData class.
00062         '''
00063         membertool= getToolByName(self, 'portal_membership')
00064         members   = self._members
00065 
00066         for tuple in members.items():
00067             member_name = tuple[0]
00068             member_obj  = tuple[1]
00069             del members[member_name]
00070 
00071         return "Done."
00072 
00073     security.declarePrivate("updateMemberDataContents")
00074     def updateMemberDataContents(self,):
00075         """Update former MemberData objects to new MemberData objects
00076         """
00077         count = 0
00078         membertool= getToolByName(self, 'portal_membership')
00079         members   = self._members
00080         properties = self.propertyIds()
00081 
00082         # Scan members for old MemberData
00083         for member_name, member_obj in members.items():
00084             values = {}
00085             if getattr(member_obj, "_is_new_kind", None):
00086                 continue        # Do not have to upgrade that object
00087 
00088             # Have to upgrade. Create the values mapping.
00089             for pty_name in properties:
00090                 user_value = getattr( member_obj, pty_name, _marker )
00091                 if user_value <> _marker:
00092                     values[pty_name] = user_value
00093 
00094             # Wrap a new user object of the RIGHT class
00095             u = self.acl_users.getUserById(member_name, None)
00096             if not u:
00097                 continue                # User is not in main acl_users anymore
00098             self.wrapUser(u)
00099 
00100             # Set its properties
00101             mbr = self._members.get(member_name, None)
00102             if not mbr:
00103                 raise RuntimeError, "Error while upgrading user '%s'." % (member_name, )
00104             mbr.setProperties(values, force_local = 1)
00105             count += 1
00106 
00107         return count
00108 
00109 
00110     security.declarePrivate( 'searchMemberDataContents' )
00111     def searchMemberDataContents( self, search_param, search_term ):
00112         """
00113         Search members.
00114         This is the same as CMFCore except that it doesn't check term case.
00115         """
00116         res = []
00117 
00118         search_term = search_term.strip().lower()
00119 
00120         if search_param == 'username':
00121             search_param = 'id'
00122 
00123         mtool   = getToolByName(self, 'portal_membership')
00124 
00125         for member_id in self._members.keys():
00126 
00127             user_wrapper = mtool.getMemberById( member_id )
00128 
00129             if user_wrapper is not None:
00130                 memberProperty = user_wrapper.getProperty
00131                 searched = memberProperty( search_param, None )
00132 
00133                 if searched is not None:
00134                     if searched.strip().lower().find(search_term) != -1:
00135 
00136                         res.append( { 'username': memberProperty( 'id' )
00137                                       , 'email' : memberProperty( 'email', '' )
00138                                       }
00139                                     )
00140         return res
00141 
00142     security.declarePublic( 'searchFulltextForMembers' )
00143     def searchFulltextForMembers(self, s):
00144         """search for members which do have string 's' in name, email or full name (if defined)
00145 
00146         this is mainly used for the localrole form
00147         """
00148 
00149         s=s.strip().lower()
00150 
00151         portal = self.portal_url.getPortalObject()
00152         mu = self.portal_membership
00153         is_manager = mu.checkPermission('Manage portal', self)
00154 
00155         res = []
00156         for member in mu.listMembers():
00157             u = member.getUser()
00158             if not (member.listed or is_manager):
00159                 continue
00160             if u.getUserName().lower().find(s) != -1 \
00161                 or member.getProperty('fullname').lower().find(s) != -1 \
00162                 or member.getProperty('email').lower().find(s) != -1:
00163                     res.append(member)
00164         return res
00165 
00166 MemberDataTool.__doc__ = BaseTool.__doc__
00167 
00168 InitializeClass(MemberDataTool)
00169 
00170 _marker = []  # Create a new marker object.