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

« back to all changes in this revision

Viewing changes to ivle/db.py

  • Committer: William Grant
  • Date: 2009-01-20 01:06:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120010604-gqapkj20jluixic8
ivle.db.create_user: Kill. Unused. Also drop some things that were only used
    by it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
    else:
103
103
        raise DBException("Invalid boolean value returned from DB")
104
104
 
105
 
def _passhash(password):
106
 
    return md5.md5(password).hexdigest()
107
 
 
108
105
class DBException(Exception):
109
106
    """A DBException is for bad conditions in the database or bad input to
110
107
    these methods. If Postgres throws an exception it does not get rebadged.
322
319
    # USER MANAGEMENT FUNCTIONS #
323
320
 
324
321
    login_primary = frozenset(["login"])
325
 
    login_fields_list = [
326
 
        "login", "passhash", "state", "unixid", "email", "nick", "fullname",
327
 
        "rolenm", "studentid", "acct_exp", "pass_exp", "last_login", "svn_pass"
328
 
    ]
329
 
    login_fields = frozenset(login_fields_list)
330
 
 
331
 
    def create_user(self, user_obj=None, dry=False, **kwargs):
332
 
        """Creates a user login entry in the database.
333
 
        Two ways to call this - passing a user object, or passing
334
 
        all fields as separate arguments.
335
 
 
336
 
        Either pass a "user_obj" as the first argument (in which case other
337
 
        fields will be ignored), or pass all fields as arguments.
338
 
 
339
 
        All user fields are to be passed as args. The argument names
340
 
        are the field names of the "login" table of the DB schema.
341
 
        However, instead of supplying a "passhash", you must supply a
342
 
        "password" argument, which will be hashed internally.
343
 
        Also "state" must not given explicitly; it is implicitly set to
344
 
        "no_agreement".
345
 
        Raises an exception if the user already exists, or the dict contains
346
 
        invalid keys or is missing required keys.
347
 
        """
348
 
        if 'passhash' in kwargs:
349
 
            raise DBException("Supplied arguments include passhash (invalid) (1).")
350
 
        # Make a copy of the dict. Change password to passhash (hashing it),
351
 
        # and set 'state' to "no_agreement".
352
 
        if user_obj is None:
353
 
            # Use the kwargs
354
 
            fields = copy.copy(kwargs)
355
 
        else:
356
 
            # Use the user object
357
 
            fields = dict(user_obj)
358
 
        if 'password' in fields:
359
 
            fields['passhash'] = _passhash(fields['password'])
360
 
            del fields['password']
361
 
        if 'role' in fields:
362
 
            # Convert role to rolenm
363
 
            fields['rolenm'] = str(user_obj.role)
364
 
            del fields['role']
365
 
        if user_obj is None:
366
 
            fields['state'] = "no_agreement"
367
 
            # else, we'll trust the user, but it SHOULD be "no_agreement"
368
 
            # (We can't change it because then the user object would not
369
 
            # reflect the DB).
370
 
        if 'local_password' in fields:
371
 
            del fields['local_password']
372
 
        # Execute the query.
373
 
        return self.insert(fields, "login", self.login_fields, dry=dry)
374
322
 
375
323
    def get_user_loginid(self, login, dry=False):
376
324
        """Given a login, returns the integer loginid for this user.