#format wiki {{{#!python #!/usr/bin/env python """ MoinMoin - user creation tool GPL code written by Chris Gaskett 20050127 parts from moin_usercheck-jh-new.py Currently only creates one user at a time. If you want to create from a list please let Chris Gaskett know the format (i.e. just name and email address? or name, email address and password?) or any other requirements. How to use this tool? ===================== Before starting, making a backup of your wiki might be a good idea. Example: moin_createuser JaneDoe jane@example.com --config . The example above would not actually save the changes, to do that specify --save, i.e. moin_createuser JaneDoe jane@example.com --config . --save Bugs ===== * Help page doesn't work well and isn't complete, doesn't show Chris Gaskett as author * Only tested under moin version 1.2.3 """ __version__ = "0.1" from MoinMoin.scripts import _util config = None ############################################################################# ### Main program ############################################################################# class MoinCreateUser(_util.Script): def __init__(self): _util.Script.__init__(self, __name__, "username email [options]") # --config=DIR self.parser.add_option( "--config", metavar="DIR", dest="configdir", help="Path to moin_config.py (or its directory)" ) self._addFlag("save", "Will allow the other" " options to save user account back to disk." " If not specified, no settings will be changed permanently." ) def _addFlag(self, name, help): self.parser.add_option("--" + name, action="store_true", dest=name, default=0, help=help) def assertSafeToCreateUser(self, username, email_address): """ Throw an exception if it's not safe to create a user with given properties, otherwise return 0. May need modification for site specific requirements. """ from MoinMoin import user, wikiutil # # check username # if not wikiutil.isStrictWikiname(username): raise Exception, "User name %s is not a wikiname" % username # isSystemPage needs a request object so can't use? if wikiutil.isTemplatePage(username) or wikiutil.isFormPage(username): raise Exception, "User name %s is a reserved name" % username # not a group? (moin 1.3 has a function for this) import re if re.search(config.page_group_regex, username, re.UNICODE) is not None: raise Exception, "User name %s is a group name, not allowed" % username # unique? uid = user.getUserId(username) if uid is not None: u = user.User(None, id=uid) raise Exception, "User already exists: %s %s" % (u.name, u.email) # # check email # # correct structure? if not re.match(".+@.+\..{2,}", email_address): raise Exception, "Email address %s isn't valid" % email_address # unique? for uid in user.getUserList(): u = user.User(None, id=uid) if u.email == email_address: raise Exception, "Email address duplicate: %s %s" % (u.name, u.email) # it seems to be safe to add this user return 0 def randomPassword(self): """ Return an 8 character random alphanumeric password. """ import string, random password_characters = string.letters + string.digits return ''.join([random.choice(password_characters) for dummy in range(8)]) def createUser(self, username, email_address, password=None): """ Actually create the user, and save if the --save flag was specified. Use a random password if password is None. (User can get their password (encrypted) by email through the login interface) """ from MoinMoin import user self.assertSafeToCreateUser(username, email_address) password = password or self.randomPassword() # don't pass the username argument so we can get a new id u = user.User(None, auth_username=username, password=password) u.name = username u.email = email_address print "New user %s has id %s and password %s" % (u.name, u.id, password) # if necessary add site-specific settings for the new user here if self.options.save: u.save() def mainloop(self): """ moin-createuser's main code. """ import os, sys if len(self.args) != 2: print "Error: requires name and email address arguments" self.parser.print_help() sys.exit(1) # # Load the configuration, code taken from moin_usercheck-jh-new.py # configdir = self.options.configdir if configdir: if os.path.isfile(configdir): configdir = os.path.dirname(configdir) if not os.path.isdir(configdir): _util.fatal("Bad path %r given to --config parameter" % configdir) configdir = os.path.abspath(configdir) sys.path[0:0] = [configdir] os.chdir(configdir) global config from MoinMoin import config if config.default_config: _util.fatal("You have to be in the directory containing moin_config.py, " "or use the --config option!") # real work begins here (username, email_address) = self.args self.createUser(username, email_address.lower()) if self.options.save: print "Any changes were saved." else: print "Changes were not saved; to make them permanent use the --save flag." def run(): MoinCreateUser().run() if __name__ == "__main__": run() }}} I modified this to work with 1.3, see [[NickWelch/moin_createuser_1.3.py]] -- NickWelch <>