1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
# Code to talk to the PostgreSQL database.
23
# (This is the Data Access Layer).
24
# All DB code should be in this module to ensure portability if we want to
25
# change the DB implementation.
26
# This means no SQL strings should be outside of this module. Add functions
27
# here to perform the activities needed, and place the SQL code for those
30
# CAUTION to editors of this module.
31
# All string inputs must be sanitized by calling _escape before being
32
# formatted into an SQL query string.
39
from common import caps
42
"""Wrapper around pg.escape_string. Prepares the Python value for use in
43
SQL. Returns a string, which may be safely placed verbatim into an SQL
45
Handles the following types:
46
* str: Escapes the string, and also quotes it.
47
* int/long/float: Just converts to an unquoted string.
48
* bool: Returns as "TRUE" or "FALSE", unquoted.
49
* NoneType: Returns "NULL", unquoted.
50
* common.caps.Role: Returns the role as a quoted, lowercase string.
51
Raises a DBException if val has an unsupported type.
53
# "E'" is postgres's way of making "escape" strings.
54
# Such strings allow backslashes to escape things. Since escape_string
55
# converts a single backslash into two backslashes, it needs to be fed
57
# Ref: http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html
58
# WARNING: PostgreSQL-specific code
61
elif isinstance(val, str):
62
return "E'" + pg.escape_string(val) + "'"
63
elif isinstance(val, bool):
64
return "TRUE" if val else "FALSE"
65
elif isinstance(val, int) or isinstance(val, long) \
66
or isinstance(val, float):
68
elif isinstance(val, caps.Role):
69
return _escape(str(val))
71
raise DBException("Attempt to insert an unsupported type "
74
def _passhash(password):
75
return md5.md5(password).hexdigest()
77
class DBException(Exception):
78
"""A DBException is for bad conditions in the database or bad input to
79
these methods. If Postgres throws an exception it does not get rebadged.
80
This is only for additional exceptions."""
84
"""An IVLE database object. This object provides an interface to
85
interacting with the IVLE database without using any external SQL.
87
Most methods of this class have an optional dry argument. If true, they
88
will return the SQL query string and NOT actually execute it. (For
91
Methods may throw db.DBException, or any of the pg exceptions as well.
92
(In general, be prepared to catch exceptions!)
95
"""Connects to the database and creates a DB object.
96
Takes no parameters - gets all the DB info from the configuration."""
98
self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
99
port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
106
# GENERIC DB FUNCTIONS #
109
def check_dict(dict, tablefields, disallowed=frozenset([]), must=False):
110
"""Checks that a dict does not contain keys that are not fields
111
of the specified table.
112
dict: A mapping from string keys to values; the keys are checked to
113
see that they correspond to login table fields.
114
tablefields: Collection of strings for field names in the table.
115
Only these fields will be allowed.
116
disallowed: Optional collection of strings for field names that are
118
must: If True, the dict MUST contain all fields in tablefields.
119
If False, it may contain any subset of the fields.
120
Returns True if the dict is valid, False otherwise.
122
allowed = frozenset(tablefields) - frozenset(disallowed)
123
dictkeys = frozenset(dict.keys())
125
return allowed == dictkeys
127
return allowed.issuperset(dictkeys)
129
def insert(self, dict, tablename, tablefields, disallowed=frozenset([]),
131
"""Inserts a new row in a table, using data from a supplied
132
dictionary (which will be checked by check_dict).
133
dict: Dictionary mapping column names to values. The values may be
134
any of the following types:
135
str, int, long, float, NoneType.
136
tablename: String, name of the table to insert into. Will NOT be
137
escaped - must be a valid identifier.
138
tablefields, disallowed: see check_dict.
139
dry: Returns the SQL query as a string, and does not execute it.
140
Raises a DBException if the dictionary contains invalid fields.
142
if not DB.check_dict(dict, tablefields, disallowed):
143
raise DBException("Supplied dictionary contains invalid fields.")
144
# Build two lists concurrently: field names and values, as SQL strings
147
for k,v in dict.items():
149
values.append(_escape(v))
150
if len(fieldnames) == 0: return
151
fieldnames = ', '.join(fieldnames)
152
values = ', '.join(values)
153
query = ("INSERT INTO %s (%s) VALUES (%s);"
154
% (tablename, fieldnames, values))
158
def update(self, primarydict, updatedict, tablename, tablefields,
159
primary_keys, disallowed_update=frozenset([]), dry=False):
160
"""Updates a row in a table, matching against primarydict to find the
161
row, and using the data in updatedict (which will be checked by
163
primarydict: Dict mapping column names to values. The keys should be
164
the table's primary key. Only rows which match this dict's values
166
updatedict: Dict mapping column names to values. The columns will be
167
updated with the given values for the matched rows.
168
tablename, tablefields, disallowed_update: See insert.
169
primary_keys: Collection of strings which together form the primary
170
key for this table. primarydict must contain all of these as keys,
173
if (not (DB.check_dict(primarydict, primary_keys, must=True)
174
and DB.check_dict(updatedict, tablefields, disallowed_update))):
175
raise DBException("Supplied dictionary contains invalid or "
177
# Make a list of SQL fragments of the form "field = 'new value'"
178
# These fragments are ALREADY-ESCAPED
180
for k,v in updatedict.items():
181
setlist.append("%s = %s" % (k, _escape(v)))
183
for k,v in primarydict.items():
184
wherelist.append("%s = %s" % (k, _escape(v)))
185
if len(setlist) == 0 or len(wherelist) == 0:
187
# Join the fragments into a comma-separated string
188
setstring = ', '.join(setlist)
189
wherestring = ' AND '.join(wherelist)
190
# Build the whole query as an UPDATE statement
191
query = ("UPDATE %s SET %s WHERE %s;"
192
% (tablename, setstring, wherestring))
196
def delete(self, primarydict, tablename, primary_keys, dry=False):
197
"""Deletes a row in the table, matching against primarydict to find
199
primarydict, tablename, primary_keys: See update.
201
if not DB.check_dict(primarydict, primary_keys, must=True):
202
raise DBException("Supplied dictionary contains invalid or "
205
for k,v in primarydict.items():
206
wherelist.append("%s = %s" % (k, _escape(v)))
207
if len(wherelist) == 0:
209
wherestring = ' AND '.join(wherelist)
210
query = ("DELETE FROM %s WHERE %s;" % (tablename, wherestring))
214
def get_single(self, primarydict, tablename, getfields, primary_keys,
215
error_notfound="No rows found", dry=False):
216
"""Retrieves a single row from a table, returning it as a dictionary
217
mapping field names to values. Matches against primarydict to find the
219
primarydict, tablename, primary_keys: See update/delete.
220
getfields: Collection of strings; the field names which will be
221
returned as keys in the dictionary.
222
error_notfound: Error message if 0 rows match.
223
Raises a DBException if 0 rows match, with error_notfound as the msg.
224
Raises an AssertError if >1 rows match (this should not happen if
225
primary_keys is indeed the primary key).
227
if not DB.check_dict(primarydict, primary_keys, must=True):
228
raise DBException("Supplied dictionary contains invalid or "
231
for k,v in primarydict.items():
232
wherelist.append("%s = %s" % (k, _escape(v)))
233
if len(getfields) == 0 or len(wherelist) == 0:
235
# Join the fragments into a comma-separated string
236
getstring = ', '.join(getfields)
237
wherestring = ' AND '.join(wherelist)
238
# Build the whole query as an SELECT statement
239
query = ("SELECT %s FROM %s WHERE %s;"
240
% (getstring, tablename, wherestring))
242
result = self.db.query(query)
243
# Expecting exactly one
244
if result.ntuples() != 1:
245
# It should not be possible for ntuples to be greater than 1
246
assert (result.ntuples() < 1)
247
raise DBException(error_notfound)
248
# Return as a dictionary
249
return result.dictresult()[0]
251
def get_all(self, tablename, getfields, dry=False):
252
"""Retrieves all rows from a table, returning it as a list of
253
dictionaries mapping field names to values.
254
tablename, getfields: See get_single.
256
if len(getfields) == 0:
258
getstring = ', '.join(getfields)
259
query = ("SELECT %s FROM %s;" % (getstring, tablename))
261
return self.db.query(query).dictresult()
263
# USER MANAGEMENT FUNCTIONS #
265
login_primary = frozenset(["login"])
266
login_fields_list = [
267
"login", "passhash", "state", "unixid", "email", "nick", "fullname",
268
"rolenm", "studentid", "acct_exp", "pass_exp", "last_login"
270
login_fields = frozenset(login_fields_list)
271
# Do not return passhash when reading from the DB
272
login_getfields = login_fields - frozenset(["passhash"])
274
def create_user(self, dry=False, **kwargs):
275
"""Creates a user login entry in the database.
276
All user fields are to be passed as args. The argument names
277
are the field names of the "login" table of the DB schema.
278
However, instead of supplying a "passhash", you must supply a
279
"password" argument, which will be hashed internally.
280
Also "state" must not given explicitly; it is implicitly set to
282
Raises an exception if the user already exists, or the dict contains
283
invalid keys or is missing required keys.
285
if 'passhash' in kwargs:
286
raise DBException("Supplied arguments include passhash (invalid).")
287
# Make a copy of the dict. Change password to passhash (hashing it),
288
# and set 'state' to "no_agreement".
289
kwargs = copy.copy(kwargs)
290
if 'password' in kwargs:
291
kwargs['passhash'] = _passhash(kwargs['password'])
292
del kwargs['password']
293
kwargs['state'] = "no_agreement"
295
return self.insert(kwargs, "login", self.login_fields, dry=dry)
297
def update_user(self, login, dry=False, **kwargs):
298
"""Updates fields of a particular user. login is the name of the user
299
to update. The dict contains the fields which will be modified, and
300
their new values. If any value is omitted from the dict, it does not
301
get modified. login and studentid may not be modified.
302
Passhash may be modified by supplying a "password" field, in
303
cleartext, not a hashed password.
305
Note that no checking is done. It is expected this function is called
306
by a trusted source. In particular, it allows the password to be
307
changed without knowing the old password. The caller should check
308
that the user knows the existing password before calling this function
311
if 'passhash' in kwargs:
312
raise DBException("Supplied arguments include passhash (invalid).")
313
if "password" in kwargs:
314
kwargs = copy.copy(kwargs)
315
kwargs['passhash'] = _passhash(kwargs['password'])
316
del kwargs['password']
317
return self.update({"login": login}, kwargs, "login",
318
self.login_fields, self.login_primary, ["login", "studentid"],
321
def get_user(self, login, dry=False):
322
"""Given a login, returns a dictionary of the user's DB fields,
323
excluding the passhash field.
325
Raises a DBException if the login is not found in the DB.
327
return self.get_single({"login": login}, "login",
328
self.login_getfields, self.login_primary,
329
error_notfound="get_user: No user with that login name", dry=dry)
331
def get_users(self, dry=False):
332
"""Returns a list of all users. The list elements are a dictionary of
333
the user's DB fields, excluding the passhash field.
335
return self.get_all("login", self.login_getfields, dry=dry)
337
def user_authenticate(self, login, password, dry=False):
338
"""Performs a password authentication on a user. Returns True if
339
"passhash" is the correct passhash for the given login, False
341
Also returns False if the login does not exist (so if you want to
342
differentiate these cases, use get_user and catch an exception).
344
query = ("SELECT login FROM login "
345
"WHERE login = '%s' AND passhash = %s;"
346
% (login, _escape(_passhash(password))))
348
result = self.db.query(query)
349
# If one row was returned, succeed.
350
# Otherwise, fail to authenticate.
351
return result.ntuples() == 1
354
"""Close the DB connection. Do not call any other functions after
355
this. (The behaviour of doing so is undefined).