50
50
def _passhash(password):
51
51
return md5.md5(password).hexdigest()
53
class DBException(Exception):
54
"""A DBException is for bad conditions in the database or bad input to
55
these methods. If Postgres throws an exception it does not get rebadged.
56
This is only for additional exceptions."""
54
60
"""An IVLE database object. This object provides an interface to
55
61
interacting with the IVLE database without using any external SQL.
57
63
Most methods of this class have an optional dry argument. If true, they
58
64
will return the SQL query string and NOT actually execute it. (For
59
65
debugging purposes).
67
Methods may throw db.DBException, or any of the pg exceptions as well.
68
(In general, be prepared to catch exceptions!)
61
70
def __init__(self):
62
71
"""Connects to the database and creates a DB object.
81
90
if dry: return query
82
91
self.db.query(query)
84
def update_user(self, login, new_password=None, new_nick=None,
85
new_fullname=None, new_rolenm=None, dry=False):
93
def update_user(self, login, password=None, nick=None,
94
fullname=None, rolenm=None, dry=False):
86
95
"""Updates fields of a particular user. login is the name of the user
87
96
to update. The other arguments are optional fields which may be
88
97
modified. If None or omitted, they do not get modified. login and
89
studentid may not be modified."""
98
studentid may not be modified.
100
Note that no checking is done. It is expected this function is called
101
by a trusted source. In particular, it allows the password to be
102
changed without knowing the old password. The caller should check
103
that the user knows the existing password before calling this function
90
106
# Make a list of SQL fragments of the form "field = 'new value'"
91
107
# These fragments are ALREADY-ESCAPED
93
if new_password is not None:
94
setlist.append("passhash = " + _escape(_passhash(new_password)))
95
if new_nick is not None:
96
setlist.append("nick = " + _escape(new_nick))
97
if new_fullname is not None:
98
setlist.append("fullname = " + _escape(new_fullname))
99
if new_rolenm is not None:
100
setlist.append("rolenm = " + _escape(new_rolenm))
109
if password is not None:
110
setlist.append("passhash = " + _escape(_passhash(password)))
112
setlist.append("nick = " + _escape(nick))
113
if fullname is not None:
114
setlist.append("fullname = " + _escape(fullname))
115
if rolenm is not None:
116
setlist.append("rolenm = " + _escape(rolenm))
101
117
if len(setlist) == 0:
103
119
# Join the fragments into a comma-separated string
108
124
if dry: return query
109
125
self.db.query(query)
111
def drop_user(self, login, dry=False):
127
def delete_user(self, login, dry=False):
112
128
"""Deletes a user login entry from the database."""
113
129
query = "DELETE FROM login WHERE login = %s;" % _escape(login)
114
130
if dry: return query
115
131
self.db.query(query)
133
def get_user(self, login, dry=False):
134
"""Given a login, returns a dictionary of the user's DB fields,
135
excluding the passhash field.
137
Raises a DBException if the login is not found in the DB.
139
query = ("SELECT login, nick, fullname, rolenm, studentid FROM login "
140
"WHERE login = %s;" % _escape(login))
142
result = self.db.query(query)
143
# Expecting exactly one
144
if result.ntuples() != 1:
145
# It should not be possible for ntuples to be greater than 1
146
assert (result.ntuples() < 1)
147
raise DBException("get_user: No user with that login name")
148
# Return as a dictionary
149
return result.dictresult()[0]
151
def user_authenticate(self, login, password, dry=False):
152
"""Performs a password authentication on a user. Returns True if
153
"password" is the correct password for the given login, False
154
otherwise. "password" is cleartext.
155
Also returns False if the login does not exist (so if you want to
156
differentiate these cases, use get_user and catch an exception).
158
query = ("SELECT login FROM login "
159
"WHERE login = '%s' AND passhash = %s;"
160
% (login, _escape(_passhash(password))))
162
result = self.db.query(query)
163
# If one row was returned, succeed.
164
# Otherwise, fail to authenticate.
165
return result.ntuples() == 1
118
168
"""Close the DB connection. Do not call any other functions after
119
169
this. (The behaviour of doing so is undefined).