345
345
def get_user(self, login, dry=False):
346
"""Given a login, returns a User object containing details looked up
346
"""Given a login, returns a dictionary of the user's DB fields,
347
excluding the passhash field.
349
349
Raises a DBException if the login is not found in the DB.
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)
355
return userdict # Query string
356
# Package into a User object
357
return user.User(**userdict)
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.
362
userdicts = self.get_all("login", self.login_getfields, dry=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)
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.
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).
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]
384
return _passhash(password) == passhash
373
# If one row was returned, succeed.
374
# Otherwise, fail to authenticate.
375
return result.ntuples() == 1
389
378
"""Close the DB connection. Do not call any other functions after