~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 02:29:04 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:1148
ivle.makeuser: svn auth manipulation functions now use storm as much as
    possible.
usrmgt-server: All actions now get a store, and the store is committed if
    the action returns succes. We use this to pass stores into ivle.makeuser.

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.
106
105
def rebuild_svn_group_config(store):
107
106
    """Build the complete SVN configuration file for groups
108
107
    """
 
108
    conn = ivle.db.DB()
 
109
    groups = conn.get_all('project_group',
 
110
        ['groupid', 'groupnm', 'projectsetid'])
109
111
    f = open(ivle.conf.svn_group_conf + ".new", "w")
110
112
    f.write("# IVLE SVN Group Repositories Configuration\n")
111
113
    f.write("# Auto-generated on %s\n" % time.asctime())
112
114
    f.write("\n")
113
 
    for group in store.find(ProjectGroup):
114
 
        offering = group.project_set.offering
115
 
        reponame = "_".join([offering.subject.short_name,
116
 
                             offering.semester.year,
117
 
                             offering.semester.semester,
118
 
                             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']])
119
122
        f.write("[%s:/]\n"%reponame)
120
 
        for user in group.members:
121
 
            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'])
122
126
        f.write("\n")
123
127
    f.close()
124
128
    os.rename(ivle.conf.svn_group_conf + ".new", ivle.conf.svn_group_conf)
178
182
    return (to_add, to_remove)
179
183
 
180
184
 
181
 
def make_jail(user, force=True):
 
185
def make_jail(username, uid, force=True, svn_pass=None):
182
186
    """Creates a new user's jail space, in the jail directory as configured in
183
187
    conf.py.
184
188
 
189
193
 
190
194
    Chowns the user's directory within the jail to the given UID.
191
195
 
 
196
    Note: This takes separate username and uid arguments. The UID need not
 
197
    *necessarily* correspond to a Unix username at all, if all you are
 
198
    planning to do is setuid to it. This allows the caller the freedom of
 
199
    deciding the binding between username and uid, if any.
 
200
 
192
201
    force: If false, exception if jail already exists for this user.
193
202
    If true (default), overwrites it, but preserves home directory.
 
203
 
 
204
    svn_pass: If provided this will be a string, the randomly-generated
 
205
    Subversion password for this user (if you happen to already have it).
 
206
    If not provided, it will be read from the database.
194
207
    """
195
208
    # MUST run as root or some of this may fail
196
209
    if os.getuid() != 0:
203
216
    elif not os.path.isdir(tempdir):
204
217
        os.unlink(tempdir)
205
218
        os.mkdir(tempdir)
206
 
    userdir = os.path.join(ivle.conf.jail_src_base, user.login)
 
219
    userdir = os.path.join(ivle.conf.jail_src_base, username)
207
220
    homedir = os.path.join(userdir, 'home')
208
 
    userhomedir = os.path.join(homedir, user.login)   # Return value
 
221
    userhomedir = os.path.join(homedir, username)   # Return value
209
222
 
210
223
    if os.path.exists(userdir):
211
224
        if not force:
226
239
        shutil.move(homebackup, homedir)
227
240
        # Change the ownership of all the files to the right unixid
228
241
        logging.debug("chown %s's home directory files to uid %d"
229
 
            %(user.login, user.unixid))
230
 
        os.chown(userhomedir, user.unixid, user.unixid)
 
242
            %(username, uid))
 
243
        os.chown(userhomedir, uid, uid)
231
244
        for root, dirs, files in os.walk(userhomedir):
232
245
            for fsobj in dirs + files:
233
 
                os.chown(os.path.join(root, fsobj), user.unixid, user.unixid)
 
246
                os.chown(os.path.join(root, fsobj), uid, uid)
234
247
    else:
235
248
        # No user jail exists
236
249
        # Set up the user's home directory
237
250
        os.makedirs(userhomedir)
238
251
        # Chown (and set the GID to the same as the UID).
239
 
        os.chown(userhomedir, user.unixid, user.unixid)
 
252
        os.chown(userhomedir, uid, uid)
240
253
        # Chmod to rwxr-xr-x (755)
241
254
        os.chmod(userhomedir, 0755)
242
255
 
243
 
    make_conf_py(user.login, userdir, ivle.conf.jail_system, user.svn_pass)
244
 
    make_etc_passwd(user.login, userdir, ivle.conf.jail_system, user.unixid)
 
256
    # There are 2 special files which need to be generated specific to this
 
257
    # user: ${python_site_packages}/lib/conf/conf.py and /etc/passwd.
 
258
    # "__" username "__" users are exempt (special)
 
259
    if not (username.startswith("__") and username.endswith("__")):
 
260
        make_conf_py(username, userdir, ivle.conf.jail_system, svn_pass)
 
261
        make_etc_passwd(username, userdir, ivle.conf.jail_system, uid)
245
262
 
246
263
    return userhomedir
247
264
 
248
 
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass):
 
265
def make_conf_py(username, user_jail_dir, staging_dir, svn_pass=None):
249
266
    """
250
267
    Creates (overwriting any existing file, and creating directories) a
251
268
    file ${python_site_packages}/ivle/conf/conf.py in a given user's jail.
262
279
            ivle.conf.python_site_packages[1:], "ivle/conf/conf.py")
263
280
    os.makedirs(os.path.dirname(conf_path))
264
281
 
 
282
    # If svn_pass isn't supplied, grab it from the DB
 
283
    if svn_pass is None:
 
284
        dbconn = ivle.db.DB()
 
285
        svn_pass = dbconn.get_user(username).svn_pass
 
286
        dbconn.close()
 
287
 
265
288
    # Read the contents of the template conf file
266
289
    try:
267
290
        template_conf_file = open(template_conf_path, "r")
304
327
                      % (username, unixid, unixid, username))
305
328
    passwd_file.close()
306
329
 
 
330
def make_user_db(throw_on_error = True, **kwargs):
 
331
    """Creates a user's entry in the database, filling in all the fields.
 
332
    All arguments must be keyword args. They are the fields in the table.
 
333
    However, instead of supplying a "passhash", you must supply a
 
334
    "password" argument, which will be hashed internally.
 
335
    Also do not supply a state. All users are created in the "no_agreement"
 
336
    state.
 
337
    Also pulls the user's subjects using the configured subject pulldown
 
338
    module, and adds enrolments to the DB.
 
339
    Throws an exception if the user already exists.
 
340
    """
 
341
    dbconn = ivle.db.DB()
 
342
    dbconn.create_user(**kwargs)
 
343
    dbconn.close()
 
344
 
 
345
    # Pulldown subjects and add enrolments
 
346
    ivle.pulldown_subj.enrol_user(kwargs['login'])
 
347
 
307
348
def mount_jail(login):
308
349
    # This is where we'll mount to...
309
350
    destdir = os.path.join(ivle.conf.jail_base, login)