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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-02-18 23:59:17 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:498
apps/userservice: Now writes to req.session to avoid having to log out.
www/media/common/tos.js: Refreshes the page after receiving an accept
response.
    (This doesn't seem to wait long enough; I still need to do a manual
    refresh to get in).

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
import md5
37
37
import copy
38
38
 
39
 
from common import (caps, user)
 
39
from common import caps
40
40
 
41
41
def _escape(val):
42
42
    """Wrapper around pg.escape_string. Prepares the Python value for use in
343
343
            dry=dry)
344
344
 
345
345
    def get_user(self, login, dry=False):
346
 
        """Given a login, returns a User object containing details looked up
347
 
        in the DB.
 
346
        """Given a login, returns a dictionary of the user's DB fields,
 
347
        excluding the passhash field.
348
348
 
349
349
        Raises a DBException if the login is not found in the DB.
350
350
        """
351
 
        userdict = self.get_single({"login": login}, "login",
 
351
        return self.get_single({"login": login}, "login",
352
352
            self.login_getfields, self.login_primary,
353
353
            error_notfound="get_user: No user with that login name", dry=dry)
354
 
        if dry:
355
 
            return userdict     # Query string
356
 
        # Package into a User object
357
 
        return user.User(**userdict)
358
354
 
359
355
    def get_users(self, dry=False):
360
 
        """Returns a list of all users in the DB, as User objects.
 
356
        """Returns a list of all users. The list elements are a dictionary of
 
357
        the user's DB fields, excluding the passhash field.
361
358
        """
362
 
        userdicts = self.get_all("login", self.login_getfields, dry=dry)
363
 
        if dry:
364
 
            return userdicts    # Query string
365
 
        # Package into User objects
366
 
        return [user.User(**userdict) for userdict in userdicts]
 
359
        return self.get_all("login", self.login_getfields, dry=dry)
367
360
 
368
361
    def user_authenticate(self, login, password, dry=False):
369
362
        """Performs a password authentication on a user. Returns True if
370
363
        "passhash" is the correct passhash for the given login, False
371
 
        if the passhash does not match the password in the DB,
372
 
        and None if the passhash in the DB is NULL.
 
364
        otherwise.
373
365
        Also returns False if the login does not exist (so if you want to
374
366
        differentiate these cases, use get_user and catch an exception).
375
367
        """
376
 
        query = "SELECT passhash FROM login WHERE login = '%s';" % login
 
368
        query = ("SELECT login FROM login "
 
369
            "WHERE login = '%s' AND passhash = %s;"
 
370
            % (login, _escape(_passhash(password))))
377
371
        if dry: return query
378
372
        result = self.db.query(query)
379
 
        if result.ntuples() == 1:
380
 
            # Valid username. Check password.
381
 
            passhash = result.getresult()[0][0]
382
 
            if passhash is None:
383
 
                return None
384
 
            return _passhash(password) == passhash
385
 
        else:
386
 
            return False
 
373
        # If one row was returned, succeed.
 
374
        # Otherwise, fail to authenticate.
 
375
        return result.ntuples() == 1
387
376
 
388
377
    def close(self):
389
378
        """Close the DB connection. Do not call any other functions after