Personal tools
You are here: Home Docs Plone User Import

Plone User Import

by David Siedband last modified Nov 18, 2013 05:56 AM

In this example, we're importing users from a ZSQL query, selectUsersAll() 

That could easily be replaced with another source of users (e.g. CSV file, etc.)

Note that we are doing duplicate-detection based on email address and username.  

We are also sending welcome (password reset) messages to the new users.

from Products.CMFCore.utils import getToolByName

existing_user_emails = []
existing_user_ids = []

regtool = getToolByName(context, 'portal_registration')

mt = getToolByName(context, 'portal_membership')
users = mt.listMemberIds()

mf = context.people

for user in users:
  member = mt.getMemberById(user)
  email = member.getProperty('email').lower()

  existing_user_emails.append(email)
  existing_user_ids.append(user)

iusers = context.selectUsersAll()

for i in iusers:
  email_match = False
  username_match = False
  if i.email in existing_user_emails:
    print "[---] duplicate email %s"  % i.email
    email_match = True
  if i.username in existing_user_ids:
    print "[---] duplicate user %s"  % i.username
    username_match = True

  if not (email_match or username_match):
    print "[+++] adding user %s %s [%s]" % (i.nameFirst, i.nameLast, i.username)
    fullname = "%s %s" % (i.nameFirst, i.nameLast)
    email = i.email.strip()
    username = i.username
    
    properties = {
        'username' : i.username,
        'fullname' : fullname,
        'email' : email,
        }
    
    try:
      passwd = regtool.generatePassword()
      regtool.addMember(username, passwd, properties=properties)
      
      
      mf.invokeFactory(type_name='Person', id=username, userid=username, firstName=i.nameFirst, lastName=i.nameLast)
      ob = mf[username]
      ob.manage_setLocalRoles(username, ["Owner",])
      ob.reindexObjectSecurity()
      
      regtool.registeredNotify(username)

    except:
      print "[---] user creation failed %s"  % i.username
  
  else:
    print "[---] duplicate user %s %s [%s]" % (i.nameFirst, i.nameLast, i.username)

return printed
 
Document Actions