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.
- Create a PloneFormGen form (ie. add a form folder)
- Click on the QuickEdit tab.
- In the Toolbox on the right, click on "More Fields" and drag a "Lines field" into the form.
- Give the lines field the title "New Account Records" (the ID of field will automatically default to "new-account-records"... this is important!)
- In the Toolbox, click on "Actions" and drag a "Custom Script Adapter" into the "actions" area below the "field" area.
- Give the new custom script adapter any title you like, e.g. "Custom script to create users".
- For "Proxy role" select "Manager".
- In the Script Body, paste the script below.
- You can remove the other fields in the form.
Here is what the form will look like.
Here is what the form looks like in quickedit mode:
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