32
32
# TODO: When creating a new home directory, chown it to its owner
45
def make_svn_repo(login, throw_on_error=True):
46
"""Create a repository for the given user.
48
path = os.path.join(conf.svn_repo_path, login)
50
res = os.system("svnadmin create '%s'" % path)
51
if res != 0 and throw_on_error:
52
raise Exception("Cannot create repository for %s" % login)
53
except Exception, exc:
58
os.system("chown -R www-data:www-data %s" % path)
62
def rebuild_svn_config():
63
"""Build the complete SVN configuration file.
66
res = conn.query("SELECT login, rolenm FROM login;").dictresult()
70
if role not in groups:
72
groups[role].append(r['login'])
73
f = open(conf.svn_conf + ".new", "w")
74
f.write("# IVLE SVN Repositories Configuration\n")
75
f.write("# Auto-generated on %s\n" % time.asctime())
78
for (g,ls) in groups.iteritems():
79
f.write("%s = %s\n" % (g, ",".join(ls)))
83
f.write("[%s:/]\n" % login)
84
f.write("%s = rw\n" % login)
85
#f.write("@tutor = r\n")
86
#f.write("@lecturer = rw\n")
87
#f.write("@admin = rw\n")
90
os.rename(conf.svn_conf + ".new", conf.svn_conf)
92
def make_svn_config(login, throw_on_error=True):
93
"""Add an entry to the apache-svn config file for the given user.
94
Assumes the given user is either a guest or a student.
96
f = open(conf.svn_conf, "a")
97
f.write("[%s:/]\n" % login)
98
f.write("%s = rw\n" % login)
99
#f.write("@tutor = r\n")
100
#f.write("@lecturer = rw\n")
101
#f.write("@admin = rw\n")
105
def make_svn_auth(login, throw_on_error=True):
106
"""Setup svn authentication for the given user.
107
FIXME: create local.auth entry
109
passwd = md5.new(uuid.uuid4().bytes).digest().encode('hex')
110
if os.path.exists(conf.svn_auth_ivle):
115
db.DB().update_user(login, svn_pass=passwd)
117
res = os.system("htpasswd -%smb %s %s %s" % (create,
120
if res != 0 and throw_on_error:
121
raise Exception("Unable to create ivle-auth for %s" % login)
125
def make_jail(username, uid, force=True):
41
def makeuser(username, password, nick, fullname, rolenm, studentid):
42
"""Creates a new user on a pre-installed system.
43
Sets up the following:
44
* User's jail and home directory within the jail.
45
* Subversion repository
46
* Check out Subversion workspace into jail
47
* Database details for user
50
homedir = make_jail(username)
51
make_user_db(username, password, nick, fullname, rolenm, studentid)
52
# TODO: -p password (need to use crypt)
53
if os.system("useradd -d %s '%s'" % (homedir, username)) != 0:
54
raise Exception("Failed to add Unix user account")
56
def make_jail(username, force=True):
126
57
"""Creates a new user's jail space, in the jail directory as configured in
134
65
Returns the path to the user's home directory.
136
Chowns the user's directory within the jail to the given UID.
138
Note: This takes separate username and uid arguments. The UID need not
139
*necessarily* correspond to a Unix username at all, if all you are
140
planning to do is setuid to it. This allows the caller the freedom of
141
deciding the binding between username and uid, if any.
143
67
force: If false, exception if jail already exists for this user.
144
68
If true (default), overwrites it, but preserves home directory.
249
169
raise Exception, errors
251
def make_user_db(throw_on_error = True, **kwargs):
171
def make_user_db(login, password, nick, fullname, rolenm, studentid,
252
173
"""Creates a user's entry in the database, filling in all the fields.
253
All arguments must be keyword args. They are the fields in the table.
254
However, instead of supplying a "passhash", you must supply a
255
"password" argument, which will be hashed internally.
256
Also do not supply a state. All users are created in the "no_agreement"
258
Throws an exception if the user already exists.
174
If force is False, throws an exception if the user already exists.
175
If True, overwrites the user's entry in the DB.
261
dbconn.create_user(**kwargs)
179
# Delete user if it exists
181
dbconn.delete_user(login)
184
dbconn.create_user(login, password, nick, fullname, rolenm, studentid)
264
if kwargs['password']:
265
if os.path.exists(conf.svn_auth_local):
269
res = os.system("htpasswd -%smb %s %s %s" % (create,
273
if res != 0 and throw_on_error:
274
raise Exception("Unable to create local-auth for %s" % kwargs['login'])