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

« back to all changes in this revision

Viewing changes to ivle/makeuser.py

  • Committer: me at id
  • Date: 2009-01-15 05:32:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1158
ivle.database.User: Add an 'active_enrolments' property, which returns a list
     of nicely ordered active enrolments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
import filecmp
46
46
import logging
47
47
import ivle.conf
 
48
import ivle.db
48
49
import ivle.pulldown_subj
49
50
 
50
 
from ivle.database import ProjectGroup
51
 
 
52
51
def chown_to_webserver(filename):
53
52
    """
54
53
    Chowns a file so the web server user owns it.
79
78
    """
80
79
    users = store.find(ivle.database.User)
81
80
    groups = {}
82
 
    # TODO: Populate groups with per-offering tutors/lecturers/etc.
 
81
    for u in users:
 
82
        role = str(u.role)
 
83
        if role not in groups:
 
84
            groups[role] = []
 
85
        groups[role].append(u.login)
83
86
    f = open(ivle.conf.svn_conf + ".new", "w")
84
87
    f.write("# IVLE SVN Repositories Configuration\n")
85
88
    f.write("# Auto-generated on %s\n" % time.asctime())
102
105
def rebuild_svn_group_config(store):
103
106
    """Build the complete SVN configuration file for groups
104
107
    """
 
108
    conn = ivle.db.DB()
 
109
    groups = conn.get_all('project_group',
 
110
        ['groupid', 'groupnm', 'projectsetid'])
105
111
    f = open(ivle.conf.svn_group_conf + ".new", "w")
106
112
    f.write("# IVLE SVN Group Repositories Configuration\n")
107
113
    f.write("# Auto-generated on %s\n" % time.asctime())
108
114
    f.write("\n")
109
 
    for group in store.find(ProjectGroup):
110
 
        offering = group.project_set.offering
111
 
        reponame = "_".join([offering.subject.short_name,
112
 
                             offering.semester.year,
113
 
                             offering.semester.semester,
114
 
                             group.name])
 
115
    for g in groups:
 
116
        projectsetid = g['projectsetid']
 
117
        offeringinfo = conn.get_offering_info(projectsetid)
 
118
        subj_short_name = offeringinfo['subj_short_name']
 
119
        year = offeringinfo['year']
 
120
        semester = offeringinfo['semester']
 
121
        reponame = "_".join([subj_short_name, year, semester, g['groupnm']])
115
122
        f.write("[%s:/]\n"%reponame)
116
 
        for user in group.members:
117
 
            f.write("%s = rw\n" % user.login)
 
123
        users = conn.get_projectgroup_members(g['groupid'])
 
124
        for u in users:
 
125
            f.write("%s = rw\n"%u['login'])
118
126
        f.write("\n")
119
127
    f.close()
120
128
    os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
193
201
        raise Exception("Must run make_jail as root")
194
202
    
195
203
    # tempdir is for putting backup homes in
196
 
    tempdir = os.path.join(ivle.conf.jail_src_base, '__temp__')
 
204
    tempdir = os.path.join(ivle.conf.jail_base, '__temp__')
197
205
    if not os.path.exists(tempdir):
198
206
        os.makedirs(tempdir)
199
207
    elif not os.path.isdir(tempdir):
223
231
        # Change the ownership of all the files to the right unixid
224
232
        logging.debug("chown %s's home directory files to uid %d"
225
233
            %(user.login, user.unixid))
226
 
        os.spawnvp(os.P_WAIT, 'chown', ['chown', '-R', '%d:%d' % (user.unixid,
227
 
                                        user.unixid), userhomedir])
 
234
        os.chown(userhomedir, user.unixid, user.unixid)
 
235
        for root, dirs, files in os.walk(userhomedir):
 
236
            for fsobj in dirs + files:
 
237
                os.chown(os.path.join(root, fsobj), user.unixid, user.unixid)
228
238
    else:
229
239
        # No user jail exists
230
240
        # Set up the user's home directory
234
244
        # Chmod to rwxr-xr-x (755)
235
245
        os.chmod(userhomedir, 0755)
236
246
 
237
 
    make_conf_py(user.login, userdir, user.svn_pass)
 
247
    make_conf_py(user.login, userdir, ivle.conf.jail_system, user.svn_pass)
238
248
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
239
249
 
240
250
    return userhomedir
241
251
 
242
 
def make_conf_py(username, user_jail_dir, svn_pass):
 
252
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass):
243
253
    """
244
254
    Creates (overwriting any existing file, and creating directories) a
245
255
    file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
246
256
    username: Username.
247
257
    user_jail_dir: User's jail dir, ie. ivle.conf.jail_base + username
248
 
    svn_pass: User's SVN password.
 
258
    staging_dir: The dir with the staging copy of the jail. (With the
 
259
        template conf.py file).
 
260
    svn_pass: As with make_jail. User's SVN password, but if not supplied,
 
261
        will look up in the DB.
249
262
    """
 
263
    template_conf_path = os.path.join(staging_dir,
 
264
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
250
265
    conf_path = os.path.join(user_jail_dir,
251
266
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
252
267
    os.makedirs(os.path.dirname(conf_path))
253
268
 
254
 
    # In the "in-jail" version of conf, we don't need MOST of the details
255
 
    # (it would be a security risk to have them here).
256
 
    # So we just write root_dir, and jail_base is "/".
257
 
    # (jail_base being "/" means "jail-relative" paths are relative to "/"
258
 
    # when inside the jail.)
 
269
    # Read the contents of the template conf file
 
270
    try:
 
271
        template_conf_file = open(template_conf_path, "r")
 
272
        template_conf_data = template_conf_file.read()
 
273
        template_conf_file.close()
 
274
    except:
 
275
        # Couldn't open template conf.py for some reason
 
276
        # Just treat it as empty file
 
277
        template_conf_data = ("# Warning: Problem building config script.\n"
 
278
                              "# Could not find template conf.py file.\n")
259
279
 
260
 
    # XXX: jail_base is wrong and shouldn't be here. Unfortunately, jail code
261
 
    #      uses ivle.studpath.url_to_{local,jailpaths}, both of which use
262
 
    #      jail_base. Note that they don't use the bits of the return value
263
 
    #      that depend on jail_base, so it can be any string.
264
280
    conf_file = open(conf_path, "w")
265
 
    conf_file.write("""# IVLE jail configuration
266
 
 
267
 
# In URL space, where in the site is IVLE located. (All URLs will be prefixed
268
 
# with this).
269
 
# eg. "/" or "/ivle".
270
 
root_dir = %(root_dir)r
271
 
 
272
 
# This value is not relevant inside the jail, but must remain for now. See
273
 
# the XXX in ivle.makeuser.make_conf_py.
274
 
jail_base = '/'
275
 
 
276
 
# The hostname for serving publicly accessible pages
277
 
public_host = %(public_host)r
278
 
 
279
 
# The URL under which the Subversion repositories are located.
280
 
svn_addr = %(svn_addr)r
281
 
 
282
 
# The login name for the owner of the jail
283
 
login = %(username)r
284
 
 
285
 
# The subversion-only password for the owner of the jail
286
 
svn_pass = %(svn_pass)r
287
 
""" % {'root_dir': ivle.conf.root_dir,
288
 
       'public_host': ivle.conf.public_host,
289
 
       'svn_addr': ivle.conf.svn_addr,
290
 
       'username': username,
291
 
       'svn_pass': svn_pass,
292
 
      })
 
281
    conf_file.write(template_conf_data)
 
282
    conf_file.write("\n# The login name for the owner of the jail\n")
 
283
    conf_file.write("login = %s\n" % repr(username))
 
284
    conf_file.write("\n")
 
285
    conf_file.write("# The subversion-only password for the owner of "
 
286
        "the jail\n")
 
287
    conf_file.write("svn_pass = %s\n" % repr(svn_pass))
293
288
    conf_file.close()
294
289
 
295
290
    # Make this file world-readable
313
308
                      % (username, unixid, unixid, username))
314
309
    passwd_file.close()
315
310
 
 
311
def make_user_db(throw_on_error = True, **kwargs):
 
312
    """Creates a user's entry in the database, filling in all the fields.
 
313
    All arguments must be keyword args. They are the fields in the table.
 
314
    However, instead of supplying a "passhash", you must supply a
 
315
    "password" argument, which will be hashed internally.
 
316
    Also do not supply a state. All users are created in the "no_agreement"
 
317
    state.
 
318
    Also pulls the user's subjects using the configured subject pulldown
 
319
    module, and adds enrolments to the DB.
 
320
    Throws an exception if the user already exists.
 
321
    """
 
322
    dbconn = ivle.db.DB()
 
323
    dbconn.create_user(**kwargs)
 
324
    dbconn.close()
 
325
 
 
326
    # Pulldown subjects and add enrolments
 
327
    ivle.pulldown_subj.enrol_user(kwargs['login'])
 
328
 
316
329
def mount_jail(login):
317
330
    # This is where we'll mount to...
318
331
    destdir = os.path.join(ivle.conf.jail_base, login)