Quantcast
Channel: Planet Plone - Where Developers And Integrators Write
Viewing all articles
Browse latest Browse all 3535

UW Oshkosh How-To's: Create many users with a PloneFormGen form

$
0
0

This is probably the most elegant way to mass-create local users in a Plone site, since it doesn't require that you use the ZMI nor add code on the server's file system.

  1. Create a PloneFormGen form (ie. add a form folder)
  2. Click on the QuickEdit tab.
  3. In the Toolbox on the right, click on "More Fields" and drag a "Lines field" into the form.
  4. Give the lines field the title "New Account Records" (the ID of field will automatically default to "new-account-records"... this is important!)
  5. In the Toolbox, click on "Actions" and drag a "Custom Script Adapter" into the "actions" area below the "field" area.
  6. Give the new custom script adapter any title you like, e.g. "Custom script to create users".
  7. For "Proxy role" select "Manager".
  8. In the Script Body, paste the script below.
  9. You can remove the other fields in the form.

 

Here is what the form will look like.

create-users-form.jpg

 

Here is what the form looks like in quickedit mode:

create-users-form-quickedit.jpg

Testing the Form

To test the form, you could enter lines like this in the lines field:

Bob Dylan,bob@dylan.us,Reviewers
Petula Clark,pclark@wi.us,Administrators

Each new user will receive an email message containing a link that lets them set their password.

 

Script Code

Here is the script:

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request. 
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
# 
# Return value is not processed

lines = fields['new-account-records']
pr = context.portal_registration
pg = context.portal_groups
for line in lines:
  tokens = line.split(',')
  if len(tokens) == 3:
    fullname,email,groupId = tokens
    id = (email.split('@'))[0]
  elif len(tokens) == 4:
    fullname,email,groupId,id = tokens
  else:
    raise ValueError, 'Each line should contain: fullname,email,groupId[,id]. The offending line is: ''%s''' % line
  email = email.strip()
  properties = { 'username' : id, 'fullname' : fullname, 'email' : email }
  password=pr.generatePassword()
  pr.addMember(id,password,properties=properties)
  context.plone_log("create-user-accounts","Created username %s, full name %s, email %s" % (id, fullname, email))
  pr.registeredNotify(id)
  #pr.mailPassword(id, request)
  context.plone_log("create-user-accounts","Sent email notification to email %s" % (email))
  if groupId:
    group = pg.getGroupById(groupId)
    group.addMember(id)
    context.plone_log("create-user-accounts","Added username %s to group %s" % (id, groupId))

It is also available at https://github.com/uwosh/Scripts/blob/master/create-user-accounts-ploneformgen.py

 


Viewing all articles
Browse latest Browse all 3535

Trending Articles