1 #!/usr/bin/env python
   2 """
   3 MoinMoin - user creation tool
   4 GPL code written by Chris Gaskett 20050127
   5 parts from moin_usercheck-jh-new.py
   6 
   7 Currently only creates one user at a time. If you want to create from a list please
   8 let Chris Gaskett know the format (i.e. just name and email address?
   9 or name, email address and password?) or any other requirements.
  10 
  11 
  12 How to use this tool?
  13 
  14 =====================
  15 
  16 Before starting, making a backup of your wiki might be a good idea.
  17 
  18 Example:
  19  moin_createuser JaneDoe jane@example.com --config .
  20 
  21 The example above would not actually save the changes, to do that specify --save, i.e.
  22  moin_createuser JaneDoe jane@example.com --config . --save
  23 
  24 Bugs
  25 =====
  26  * Help page doesn't work well and isn't complete, doesn't show Chris Gaskett as author
  27  * Only tested under moin version 1.2.3
  28 """
  29 __version__ = "0.1"
  30 
  31 from MoinMoin.scripts import _util
  32 config = None
  33 
  34 
  35 #############################################################################
  36 ### Main program
  37 #############################################################################
  38 
  39 class MoinCreateUser(_util.Script):
  40     def __init__(self):
  41         _util.Script.__init__(self, __name__, "username email [options]")
  42 
  43         # --config=DIR
  44         self.parser.add_option(
  45             "--config", metavar="DIR", dest="configdir",
  46             help="Path to moin_config.py (or its directory)"
  47         )
  48 
  49         self._addFlag("save",
  50             "Will allow the other"
  51             " options to save user account back to disk."
  52             " If not specified, no settings will be changed permanently."
  53         )
  54 
  55 
  56     def _addFlag(self, name, help):
  57         self.parser.add_option("--" + name,
  58             action="store_true", dest=name, default=0, help=help)
  59 
  60     def assertSafeToCreateUser(self, username, email_address):
  61         """ Throw an exception if it's not safe to create a user with given
  62         properties, otherwise return 0.
  63 
  64         May need modification for site specific requirements.
  65         """
  66         from MoinMoin import user, wikiutil
  67 
  68         #
  69         # check username
  70         #
  71         if not wikiutil.isStrictWikiname(username):
  72             raise Exception, "User name %s is not a wikiname" % username
  73         # isSystemPage needs a request object so can't use?
  74         if wikiutil.isTemplatePage(username) or wikiutil.isFormPage(username):
  75             raise Exception, "User name %s is a reserved name" % username
  76         # not a group? (moin 1.3 has a function for this)
  77         import re
  78         if re.search(config.page_group_regex, username, re.UNICODE) is not None:
  79             raise Exception, "User name %s is a group name, not allowed" % username
  80         # unique?
  81         uid = user.getUserId(username)
  82         if uid is not None:
  83             u = user.User(None, id=uid)
  84             raise Exception, "User already exists: %s %s" % (u.name, u.email)
  85 
  86         #
  87         # check email
  88         #
  89         # correct structure?
  90         if not re.match(".+@.+\..{2,}", email_address):
  91             raise Exception, "Email address %s isn't valid" % email_address
  92         # unique?
  93         for uid in user.getUserList():
  94             u = user.User(None, id=uid)
  95             if u.email == email_address:
  96                 raise Exception, "Email address duplicate:  %s %s" % (u.name, u.email)
  97 
  98         # it seems to be safe to add this user
  99         return 0
 100 
 101     def randomPassword(self):
 102         """ Return an 8 character random alphanumeric password. """
 103         import string, random
 104         password_characters = string.letters + string.digits
 105         return ''.join([random.choice(password_characters) for dummy in range(8)])
 106 
 107     def createUser(self, username, email_address, password=None):
 108         """ Actually create the user, and save if the --save flag was specified.
 109 
 110         Use a random password if password is None.
 111         (User can get their password (encrypted) by email through the login interface)
 112         """
 113         from MoinMoin import user
 114 
 115         self.assertSafeToCreateUser(username, email_address)
 116 
 117         password = password or self.randomPassword()
 118         # don't pass the username argument so we can get a new id
 119         u = user.User(None, auth_username=username, password=password)
 120         u.name = username
 121         u.email = email_address
 122         print "New user %s has id %s and password %s" % (u.name, u.id, password)
 123 
 124         # if necessary add site-specific settings for the new user here
 125 
 126         if self.options.save:
 127             u.save()
 128 
 129     def mainloop(self):
 130         """ moin-createuser's main code.
 131         """
 132         import os, sys
 133 
 134         if len(self.args) != 2:
 135             print "Error: requires name and email address arguments"
 136             self.parser.print_help()
 137             sys.exit(1)
 138 
 139         #
 140         # Load the configuration, code taken from moin_usercheck-jh-new.py
 141         #
 142         configdir = self.options.configdir
 143         if configdir:
 144             if os.path.isfile(configdir): configdir = os.path.dirname(configdir)
 145             if not os.path.isdir(configdir):
 146                 _util.fatal("Bad path %r given to --config parameter" % configdir)
 147             configdir = os.path.abspath(configdir)
 148             sys.path[0:0] = [configdir]
 149             os.chdir(configdir)
 150 
 151         global config
 152         from MoinMoin import config
 153         if config.default_config:
 154             _util.fatal("You have to be in the directory containing moin_config.py, "
 155                 "or use the --config option!")
 156 
 157         # real work begins here
 158 
 159         (username, email_address) = self.args
 160 
 161         self.createUser(username, email_address.lower())
 162 
 163         if self.options.save:
 164             print "Any changes were saved."
 165         else:
 166             print "Changes were not saved; to make them permanent use the --save flag."
 167 
 168 
 169 def run():
 170     MoinCreateUser().run()
 171 
 172 if __name__ == "__main__":
 173     run()

I modified this to work with 1.3, see NickWelch/moin_createuser_1.3.py -- NickWelch 2005-04-02 00:02:29

MoinMoin: CreateMultipleUserProfiles/moin_createuser.py (last edited 2007-10-29 19:13:56 by localhost)