Back to index

moin  1.9.0~rc2
create.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - create a user account
00004 
00005 @copyright: 2006 MoinMoin:ThomasWaldmann
00006 @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from MoinMoin.script import MoinScript
00010 
00011 class PluginScript(MoinScript):
00012     """\
00013 Purpose:
00014 ========
00015 This tool allows you to create user accounts via a command line interface.
00016 
00017 Detailed Instructions:
00018 ======================
00019 General syntax: moin [options] account create [create-options]
00020 
00021 [options] usually should be:
00022     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00023 
00024 [create-options] see below:
00025     1. Verify that you have specified the right options.
00026        This script does no verification of email addresses or the like.
00027 
00028     2. To create a normal user 'JohnSmith' with an alias of 'JSmith' and an
00029        email of 'john@smith.com'
00030        moin ... account create --name JohnSmith --alias JSmith --email john@smith.com
00031 """
00032 
00033     def __init__(self, argv, def_values):
00034         MoinScript.__init__(self, argv, def_values)
00035         self.parser.add_option(
00036             "--name", metavar="NAME", dest="uname",
00037             help="Set the wiki user name to NAME."
00038         )
00039         self.parser.add_option(
00040             "--alias", metavar="ALIAS", dest="ualiasname",
00041             help="Set the wiki user alias name to ALIAS (e.g. the real name if NAME is cryptic)."
00042         )
00043         self.parser.add_option(
00044             "--email", metavar="EMAIL", dest="email",
00045             help="Set the user's email address to EMAIL."
00046         )
00047         self.parser.add_option(
00048             "--password", metavar="PASSWORD", dest="password",
00049             help="Set the user's password to PASSWORD."
00050         )
00051 
00052     def mainloop(self):
00053         # we don't expect non-option arguments
00054         if len(self.args) != 0:
00055             self.parser.error("incorrect number of arguments")
00056 
00057         flags_given = self.options.uname and self.options.email
00058         if not flags_given:
00059             self.parser.print_help()
00060             import sys
00061             sys.exit(1)
00062 
00063         self.init_request()
00064         request = self.request
00065 
00066         from MoinMoin import user
00067         if user.User(request, name=self.options.uname).exists():
00068             print 'This username "%s" exists already!' % self.options.uname
00069             return
00070         # Email should be unique - see also MoinMoin.action.newaccount
00071         if self.options.email and request.cfg.user_email_unique:
00072             if user.get_by_email_address(request, self.options.email):
00073                 print 'This emailaddress "%s" belongs to someone else!' % self.options.email
00074                 return
00075         u = user.User(request, None, self.options.uname, password=self.options.password)
00076         u.email = self.options.email
00077         u.aliasname = self.options.ualiasname or ''
00078         print " %-20s %-25s %-35s" % (u.id, u.name, u.email),
00079         u.save()
00080         print "- created."