Please login or register to participate.
Wiki Page

HowTo: Add users programatically?

.

Many times while setting up a site we need to create dummy users. This  explains how to achieve this.

from Products.CMFCore.utils import getToolByName
membership = getToolByName(portal, 'portal_membership')
portalgroups = getToolByName(portal, 'portal_groups')
userid = "user123"
password = "password123"
roles = ('Member',)
domains = ()
properties = {'fullname':'firstname lastname','email':'fname@abc.com'}
membership.addMember(userid,password,roles,domains)
#OR 
membership.addMember(userid,password,roles,domains,properties)
Comments (5)
everythingability Mar 11, 2010 01:48 PM
I would love a few more examples like this, for example, how to create content programmatically in a given space.

p.s Can these scripts be created in the ZMF as PythonScripts and run from there?
dhiraj Mar 11, 2010 01:53 PM
Yes, I believe the above script is intended to be run as an "External Method". Another way is to create a "run" script, that you run while invoking the server - the script executes and then the server stops.

Have a look at ./bin/instance --help for more details.
everythingability Mar 17, 2010 08:30 PM
I like the through-the-ZMI approach. Now, where would I put that script and then would it's name in the ZMI just be "whatever" if it was "whatever.py" on disk or would it be "externals.whatever" or something?
everythingability Mar 17, 2010 08:31 PM
In the above script, portal isn't defined...
everythingability Mar 17, 2010 08:42 PM
If I was logged in to the machine, how would I run the python interpreter so that all the right modules were loaded... I'm guessing something like...

>> cd /home/cynin
>>python2.4
import sys
sys.path.insert(0, "path_to_somewhere")
from Products.CMFCore.utils import getToolByName
 
Loading