~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to bin/ivle-makeuser

  • Committer: William Grant
  • Date: 2009-05-05 06:46:58 UTC
  • mto: (1165.3.65 submissions-admin)
  • mto: This revision was merged to the branch mainline in revision 1247.
  • Revision ID: grantw@unimelb.edu.au-20090505064658-o913x4ooxxbjfo1q
Avoid clobbering the submission owner's privileges if they have offering privs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import os
32
32
import getopt
33
33
 
34
 
import ivle.makeuser
 
34
if os.getuid() != 0:
 
35
    print "Must run %s as root." % os.path.basename(sys.argv[0])
 
36
    sys.exit(1)
 
37
 
 
38
from ivle.config import Config
 
39
from ivle.database import get_store, User
 
40
from ivle.pulldown_subj import enrol_user
35
41
 
36
42
# Requireds and optionals will be used to display the usage message
37
43
# AND do argument processing
38
44
# The names here must correspond to the fields in the database.
39
 
requireds = ["login", "fullname", "rolenm"]
 
45
requireds = ["login", "fullname"]
40
46
optionals = [
41
47
    ('p', 'password', "Cleartext password for this user"),
42
48
    ('n', 'nick', "Display name (defaults to <fullname>)"),
54
60
        print t + (' ' * max(28 - len(t), 2)) + desc
55
61
    sys.exit(1)
56
62
 
57
 
if os.getuid() != 0:
58
 
    print "Must run %s as root." % os.path.basename(sys.argv[0])
59
 
    sys.exit(1)
60
 
 
61
63
shorts = ''.join([o[0] + ":" for o in optionals])
62
64
longs = [o[1] + "=" for o in optionals]
63
65
opts, args = getopt.gnu_getopt(sys.argv[1:], shorts, longs)
66
68
# Get the dictionary of fields from opts and args
67
69
user = {}
68
70
for i in range(0, len(requireds)):
69
 
    user[requireds[i]] = args[i]
 
71
    user[requireds[i]] = unicode(args[i])
70
72
for short, long, _ in optionals:
71
73
    try:
72
 
        user[long] = opts['-' + short]
 
74
        user[long] = unicode(opts['-' + short])
73
75
    except KeyError:
74
76
        try:
75
 
            user[long] = opts['--' + long]
 
77
            user[long] = unicode(opts['--' + long])
76
78
        except KeyError:
77
79
            pass
78
80
login = user['login']
79
81
if 'nick' not in user:
80
82
    user['nick'] = user['fullname']
81
83
 
 
84
store = get_store(Config())
 
85
 
82
86
try:
83
87
    # Make the user's database entry
84
 
    ivle.makeuser.make_user_db(**user)
 
88
    userobj = User(**user)
 
89
    store.add(userobj)
 
90
    enrol_user(store, userobj)
 
91
    store.commit()
85
92
except Exception, message:
86
93
    print "Error: " + str(message)
87
94
    sys.exit(1)