[Zope-CMF] fixing ownership after site move

Sam Brauer sam@webslingerz.com
Fri, 27 Jun 2003 09:21:58 -0400


After recently putting my first CMF-based site into production, I found 
that the stories are true: after renaming a CMF site, content objects 
owned by users defined in the CMF site's acl_users have their ownership 
info still referrring to the acl_users object at the old name.

I saw mentions of this problem at:
http://www.zopelabs.com/cookbook/1037469388
http://cmf.zope.org/Members/cleath/movePortal/view

But neither of these offer ready-to-use solutions for fixing all content 
objects in the site.

I put together a script that seemed to work for me (I'll include it at 
the end of this message).
You can use it by putting a copy of the script in $ZOPE/Extensions (I 
named it "fixCMFOwnership.py") then set it up as an External Method in 
the root of your CMF site (set the "function" to "handler") after the 
move/rename, then run it (via the "test" tab).  It walks through all of 
the content objects on the site and tries to fix their ownership if they 
appear to suffer from the problem described above.

I'm tempted to post my script to zopelabs, but wanted to run it by the 
CMF list first for feedback.

Here's the script:

# After you rename or move a CMF site, the ownership of content will
# be screwed up (items owned by users in the site's acl_users will
# appear to be unowned, and if you look at the ownership tab for the
# object in the ZMI, it will refer to oldname/acl_users).
# This script will find all such content objects and fix the ownership.
# Create an ExternalMethod in the root of your CMF site that refers to
# this script, then run it (via the "test" tab).
# Sam Brauer - 20030624
# Loosely based on code from
# http://cmf.zope.org/Members/cleath/movePortal/view

from Products.CMFCore import utils

def handler(self):
     buff=[]
     buff.append("Entered fixCMFOwnership...")
     acl_users = self.acl_users
     portal_url = utils.getToolByName(self, 'portal_url')
     root = portal_url.getPortalObject()
     fixOwnership(root, acl_users, buff)
     buff.append("Exiting fixCMFOwnership().")
     return '\n'.join(buff)

def fixOwnership(obj, acl_users, outputbuffer):

     creator = obj.Creator()
     ownerid = obj.owner_info()['id']
     outputbuffer.append("fixOwnership() for '%s' (creator:'%s' and 
ownerid:'%s')" % (obj.getId(), creator, ownerid))

     if(not creator):
         try:
             user = acl_users.getUserById(ownerid)
         except:
             outputbuffer.append("getUserById raised an exception for 
'%s'!" % ownerid)
         else:
             if user:
                 user = user.__of__(acl_users)
                 obj.changeOwnership(user,  recursive=0)
                 obj.manage_addLocalRoles(ownerid, ('Owner',))
                 obj.reindexObject()
                 outputbuffer.append("Changed ownership")
             else:
                 outputbuffer.append("Didn't change ownership")

     if(getattr(obj, 'isPrincipiaFolderish', 0)):
         for child in obj.contentValues():
             fixOwnership(child, acl_users, outputbuffer)



-- 
Sam Brauer
Systems Programmer
sam@webslingerZ.com