1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#!/usr/bin/env python
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# Program: MakeUser
# Author: Matt Giuca
# Date: 9/1/2008
# Script to create a new user. This can also be done through the
# administration interface.
# This script wraps common.makeuser.
# It also creates a unix account which common.makeuser does not.
# (This script may not be appropriate for production on a multi-node
# environment).
import sys
import os
import getopt
from ivle.database import get_store, User
from ivle.pulldown_subj import enrol_user
# Requireds and optionals will be used to display the usage message
# AND do argument processing
# The names here must correspond to the fields in the database.
requireds = ["login", "fullname", "rolenm"]
optionals = [
('p', 'password', "Cleartext password for this user"),
('n', 'nick', "Display name (defaults to <fullname>)"),
('e', 'email', "Email address"),
('s', 'studentid', "Student ID")
]
if len(sys.argv) <= 3:
# Nicely format the usage message using the optionals
print ("Usage: %s [OPTIONS] %s\n OPTIONS"
% (os.path.basename(sys.argv[0]),
' '.join(['<%s>' % x for x in requireds])))
for short, long, desc in optionals:
t = " -%s | --%s" % (short, long)
print t + (' ' * max(28 - len(t), 2)) + desc
sys.exit(1)
if os.getuid() != 0:
print "Must run %s as root." % os.path.basename(sys.argv[0])
sys.exit(1)
shorts = ''.join([o[0] + ":" for o in optionals])
longs = [o[1] + "=" for o in optionals]
opts, args = getopt.gnu_getopt(sys.argv[1:], shorts, longs)
opts = dict(opts)
# Get the dictionary of fields from opts and args
user = {}
for i in range(0, len(requireds)):
user[requireds[i]] = unicode(args[i])
for short, long, _ in optionals:
try:
user[long] = unicode(opts['-' + short])
except KeyError:
try:
user[long] = unicode(opts['--' + long])
except KeyError:
pass
login = user['login']
if 'nick' not in user:
user['nick'] = user['fullname']
store = get_store()
try:
# Make the user's database entry
userobj = User(**user)
store.add(userobj)
enrol_user(store, userobj)
store.commit()
except Exception, message:
print "Error: " + str(message)
sys.exit(1)
print "Successfully created user %s (%s)." % (login, user['fullname'])
|