[rt-users] Some local RT enhancements
John Goerzen
jgoerzen at complete.org
Mon Jul 22 12:17:06 EDT 2002
On Mon, Jul 22, 2002 at 11:06:33AM +0100, Michael Thompson wrote:
> I'd be interested in seeing the code.
I've received several requests, so it's attached. I'll put it up in a
Subversion repository soon.
--
John Goerzen <jgoerzen at complete.org> GPG: 0x8A1D9A1F www.complete.org
-------------- next part --------------
#!/usr/bin/python2.2
import cgitb
cgitb.enable()
import cgi, re, os, sys, smtplib, PoPy
RTADMINPATH = "/usr/share/request-tracker/bin/rtadmin"
print "Content-Type: text/html"
print
def getwebpage(errmsg = ""):
return """<HTML><HEAD><TITLE>bugs.complete.org new user</TITLE></HEAD>
<BODY><H1>bugs.complete.org new user</H1><P>
This will create a new account for you on the Complete.Org bug-tracking
system. You will be assigned a random password, which will be e-mailed
to you at the address you specify. Your username must be between 1
and 8 characters; contain lowercase letters, numbers, dashes, or
underscores only; and start with a letter.
<P>
<PRE><FONT COLOR="red">%s</FONT></PRE><P>
You may go <A HREF="http://bugs.complete.org/">back to the bugs homepage</A> or
<A HREF="http://www.complete.org/">to Complete.Org</A>.
<P>
<FORM METHOD="post">
Username: <INPUT TYPE="text" NAME="username" size="8"><BR>
E-mail address: <INPUT TYPE="test" NAME="email" size="30"><BR>
Real Name: <INPUT TYPE="text" NAME="realname" size="30"><BR>
<INPUT TYPE="submit" name="submit" value="Submit">
</FORM>
""" % cgi.escape(errmsg)
def success(data):
global username, email, realname, password
msg = """From: Bugs.Complete.Org Account Creator <bugs-admin at bugs.complete.org>
To: %(email)s
Subject: Bugs.Complete.Org Account %(username)s Created
Your account has been created:
Username: %(username)s
Password: %(password)s
Real name: %(realname)s
You may log in at http://bugs.complete.org/.
If you have any questions, please reply to this message or e-mail
bugs-admin at bugs.complete.org.
""" % globals()
smtp = smtplib.SMTP('localhost')
smtp.sendmail('bugs-admin at bugs.complete.org', email, msg)
smtp.quit()
print """<HTML><HEAD><TITLE>Bugs.Complete.Org account created</TITLE>
</HEAD><BODY><H1>Bugs.Complete.Org account created</H1><P>
Your account has been created. You have been sent an e-mail with your new
password. You can now log in at
<A HREF="http://bugs.complete.org/">bugs.complete.org</A> with the
following information:
<P>
Username: <B>%s</B><BR>
Password: As e-mailed to you<BR>
<P>
If you have any questions, please e-mail
<A HREF="mailto:bugs-admin at bugs.complete.org">bugs-admin at bugs.complete.org</A>.
<P>
The output of the command was:
<PRE>%s</PRE>
<P>
</BODY></HTML>
""" % (cgi.escape(username), data)
sys.exit(0)
form = cgi.FieldStorage()
if (not form.has_key("submit")):
print getwebpage()
sys.exit(0)
if (not form.has_key("username")) or len(form.getvalue("username")) == 0:
print getwebpage("Username was missing")
sys.exit(0)
if (not form.has_key("email")) or len(form.getvalue("email")) == 0:
print getwebpage("E-mail address was missing")
sys.exit(0)
if (not form.has_key("realname")) or len(form.getvalue("realname")) == 0:
print getwebpage("Real name was missing")
sys.exit(0)
username = form.getvalue("username")
email = form.getvalue("email")
realname = form.getvalue("realname")
if len(username) > 8 or len(username) < 1:
print getwebpage("Username must be between 1 and 8 characters.")
sys.exit(0)
if not re.search("^[a-z]", username):
print getwebpage("Username must start with a letter")
sys.exit(0)
if re.search("[^a-z0-9_-]", username):
print getwebpage("Username contained an invalid letter.")
sys.exit(0)
if not re.search("@", email):
print getwebpage("Email invalid.")
sys.exit(0)
badchars = ";\"'\\&~!#$%^&*()|{}[]"
for testchar in badchars:
if username.find(testchar) != -1:
print getwebpage("Invalid characters in username.")
sys.exit(0)
if email.find(testchar) != -1:
print getwebpage("Invalid characters in e-mail.")
sys.exit(0)
if realname.find(testchar) != -1:
print getwebpage("Invalid characters in real name.")
sys.exit(0)
passwordfd = os.popen("pwgen -ncN 1")
password = passwordfd.readline().strip()
userfd = os.popen(RTADMINPATH + " --list-users")
userlist = []
for line in userfd.xreadlines():
line = line.strip()
userlist.append(line.split()[0])
#print userlist
if username in userlist:
print getwebpage("Username '%s' already exists; please choose another." %\
username)
sys.exit(0)
piper, pipew = os.pipe()
pid = os.fork()
if not pid:
# Child.
os.dup2(pipew, sys.stdout.fileno())
if email in userlist:
# Already exists as non-prived, promote.
conn = PoPy.connect('user=rt dbname=rt')
cur = conn.cursor()
cur.execute("update users set name = '%(newname)s' where name = '%(oldname)s'",
{'newname': username, 'oldname': email})
conn.commit()
conn.close()
os.execvp(RTADMINPATH, [RTADMINPATH, '--user', username,
'--realname', realname,
'--emailaddress', email,
'--password', password,
'--privileged', '1'])
else:
os.execvp(RTADMINPATH, [RTADMINPATH, '--user', username,
'--create', '--realname', realname,
'--emailaddress', email,
'--password', password,
'--privileged', '1'])
print getwebpage("Exec failed!")
sys.exit(0)
# Parent.
data = ""
while 1:
newdata = os.read(piper, 1024)
data += newdata
if len(newdata) < 1024:
break
if data == "User created\n":
success(data)
sys.exit(0)
else:
datalist = data.strip().split("\n")
for line in datalist:
if not re.search("^User .+:\s+The new value has been set\.", line):
print getwebpage("Subprocess failure [%s]:\n" % line + data)
sys.exit(0)
success(data)
sys.exit(0)
More information about the rt-users
mailing list