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
"""Wrapper around pg.escape_string. Escapes the string for use in SQL, and
40
also quotes it to make sure that every string used in a query is quoted.
42
return "'" + pg.escape_string(str) + "'"
45
"""An IVLE database object. This object provides an interface to
46
interacting with the IVLE database without using any external SQL.
48
Most methods of this class have an optional dry argument. If true, they
49
will return the SQL query string and NOT actually execute it. (For
53
"""Connects to the database and creates a DB object.
54
Takes no parameters - gets all the DB info from the configuration."""
55
self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
56
port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
59
def _passhash(password):
60
return md5.md5(password).hexdigest()
62
def create_user(self, login, password, nick, fullname, rolenm, studentid,
64
"""Creates a user login entry in the database.
65
Arguments are the same as those in the "login" table of the schema.
66
The exception is "password", which is a cleartext password. makeuser
67
will hash the password.
69
passhash = DB._passhash(password)
70
query = ("INSERT INTO login (login, passhash, nick, fullname, "
71
"rolenm, studentid) VALUES (%s, %s, %s, %s, %s, %s);" %
72
(_escape(login), _escape(passhash), _escape(nick),
73
_escape(fullname), _escape(rolenm), _escape(studentid)))
77
def drop_user(self, login, dry=False):
78
"""Deletes a user login entry from the database."""
79
query = "DELETE FROM login WHERE login = %s;" % _escape(login)
84
"""Close the DB connection. Do not call any other functions after
85
this. (The behaviour of doing so is undefined).