Updating Plone user roles programatically
Today for a site using Plone Translation Hub I had to programatically update the roles for all users who had logged in since March 9th, given that this group was known to be responsible enough to be worthy of the "Reviewer" role. One of the site managers asked that I simply run a "SQL query" to do this–if only it were that easy! Actually it’s not that much harder with the ZODB, but it took me a while to find the "userFolderEditUser" method that I needed to do this, which was frustrating. I ended up simply using a temporary script object placed in the root of the portal:
recent_date=DateTime('2007/03/09')
total=0
recent=0
mship = context.portal_membership
acl_users = context.acl_users
for user in mship.listMembers():
login_time=DateTime(user.last_login_time)
roles = list(user.getRoles())
if login_time.greaterThanEqualTo(recent_date) and 'Reviewer' not in roles:
roles.append('Reviewer')
acl_users.userFolderEditUser(user.id, None, roles, user.getDomains())
recent += 1
total += 1
print "Recent users: " + str(recent)
print "Total users: " + str(total)
return printed