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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: stevenbird
  • Date: 2008-02-18 08:39:01 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:494
more cleanups of installation instructions based on silly misunderstandings

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
# Module: Database
19
19
# Author: Matt Giuca
20
 
# Date:   1/2/2008
 
20
# Date:   15/2/2008
21
21
 
22
22
# Code to talk to the PostgreSQL database.
23
23
# (This is the Data Access Layer).
34
34
import pg
35
35
import conf
36
36
import md5
37
 
 
38
 
def _escape(str):
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.
 
37
import copy
 
38
 
 
39
from common import caps
 
40
 
 
41
def _escape(val):
 
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
 
44
    query.
 
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.
41
52
    """
42
53
    # "E'" is postgres's way of making "escape" strings.
43
54
    # Such strings allow backslashes to escape things. Since escape_string
45
56
    # into E mode.
46
57
    # Ref: http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html
47
58
    # WARNING: PostgreSQL-specific code
48
 
    return "E'" + pg.escape_string(str) + "'"
 
59
    if val is None:
 
60
        return "NULL"
 
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):
 
67
        return str(val)
 
68
    elif isinstance(val, caps.Role):
 
69
        return _escape(str(val))
 
70
    else:
 
71
        raise DBException("Attempt to insert an unsupported type "
 
72
            "into the database")
49
73
 
50
74
def _passhash(password):
51
75
    return md5.md5(password).hexdigest()
70
94
    def __init__(self):
71
95
        """Connects to the database and creates a DB object.
72
96
        Takes no parameters - gets all the DB info from the configuration."""
 
97
        self.open = False
73
98
        self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
74
99
                port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
 
100
        self.open = True
 
101
 
 
102
    def __del__(self):
 
103
        if self.open:
 
104
            self.db.close()
 
105
 
 
106
    # GENERIC DB FUNCTIONS #
 
107
 
 
108
    @staticmethod
 
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
 
117
            not allowed.
 
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.
 
121
        """
 
122
        allowed = frozenset(tablefields) - frozenset(disallowed)
 
123
        dictkeys = frozenset(dict.keys())
 
124
        if must:
 
125
            return allowed == dictkeys
 
126
        else:
 
127
            return allowed.issuperset(dictkeys)
 
128
 
 
129
    def insert(self, dict, tablename, tablefields, disallowed=frozenset([]),
 
130
        dry=False):
 
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.
 
141
        """
 
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
 
145
        fieldnames = []
 
146
        values = []
 
147
        for k,v in dict.items():
 
148
            fieldnames.append(k)
 
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))
 
155
        if dry: return query
 
156
        self.db.query(query)
 
157
 
 
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
 
162
        check_dict).
 
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
 
165
            will be updated.
 
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,
 
171
            and only these keys.
 
172
        """
 
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 "
 
176
                " missing fields.")
 
177
        # Make a list of SQL fragments of the form "field = 'new value'"
 
178
        # These fragments are ALREADY-ESCAPED
 
179
        setlist = []
 
180
        for k,v in updatedict.items():
 
181
            setlist.append("%s = %s" % (k, _escape(v)))
 
182
        wherelist = []
 
183
        for k,v in primarydict.items():
 
184
            wherelist.append("%s = %s" % (k, _escape(v)))
 
185
        if len(setlist) == 0 or len(wherelist) == 0:
 
186
            return
 
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))
 
193
        if dry: return query
 
194
        self.db.query(query)
 
195
 
 
196
    def delete(self, primarydict, tablename, primary_keys, dry=False):
 
197
        """Deletes a row in the table, matching against primarydict to find
 
198
        the row.
 
199
        primarydict, tablename, primary_keys: See update.
 
200
        """
 
201
        if not DB.check_dict(primarydict, primary_keys, must=True):
 
202
            raise DBException("Supplied dictionary contains invalid or "
 
203
                " missing fields.")
 
204
        wherelist = []
 
205
        for k,v in primarydict.items():
 
206
            wherelist.append("%s = %s" % (k, _escape(v)))
 
207
        if len(wherelist) == 0:
 
208
            return
 
209
        wherestring = ' AND '.join(wherelist)
 
210
        query = ("DELETE FROM %s WHERE %s;" % (tablename, wherestring))
 
211
        if dry: return query
 
212
        self.db.query(query)
 
213
 
 
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
 
218
        row.
 
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).
 
226
        """
 
227
        if not DB.check_dict(primarydict, primary_keys, must=True):
 
228
            raise DBException("Supplied dictionary contains invalid or "
 
229
                " missing fields.")
 
230
        wherelist = []
 
231
        for k,v in primarydict.items():
 
232
            wherelist.append("%s = %s" % (k, _escape(v)))
 
233
        if len(getfields) == 0 or len(wherelist) == 0:
 
234
            return
 
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))
 
241
        if dry: return query
 
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]
 
250
 
 
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.
 
255
        """
 
256
        if len(getfields) == 0:
 
257
            return
 
258
        getstring = ', '.join(getfields)
 
259
        query = ("SELECT %s FROM %s;" % (getstring, tablename))
 
260
        if dry: return query
 
261
        return self.db.query(query).dictresult()
 
262
 
 
263
    def start_transaction(self, dry=False):
 
264
        """Starts a DB transaction.
 
265
        Will not commit any changes until self.commit() is called.
 
266
        """
 
267
        query = "START TRANSACTION;"
 
268
        if dry: return query
 
269
        self.db.query(query)
 
270
 
 
271
    def commit(self, dry=False):
 
272
        """Commits (ends) a DB transaction.
 
273
        Commits all changes since the call to start_transaction.
 
274
        """
 
275
        query = "COMMIT;"
 
276
        if dry: return query
 
277
        self.db.query(query)
 
278
 
 
279
    def rollback(self, dry=False):
 
280
        """Rolls back (ends) a DB transaction, undoing all changes since the
 
281
        call to start_transaction.
 
282
        """
 
283
        query = "ROLLBACK;"
 
284
        if dry: return query
 
285
        self.db.query(query)
75
286
 
76
287
    # USER MANAGEMENT FUNCTIONS #
77
288
 
78
 
    def create_user(self, login, password, nick, fullname, rolenm, studentid,
79
 
        dry=False):
 
289
    login_primary = frozenset(["login"])
 
290
    login_fields_list = [
 
291
        "login", "passhash", "state", "unixid", "email", "nick", "fullname",
 
292
        "rolenm", "studentid", "acct_exp", "pass_exp", "last_login"
 
293
    ]
 
294
    login_fields = frozenset(login_fields_list)
 
295
    # Do not return passhash when reading from the DB
 
296
    login_getfields = login_fields - frozenset(["passhash"])
 
297
 
 
298
    def create_user(self, dry=False, **kwargs):
80
299
        """Creates a user login entry in the database.
81
 
        Arguments are the same as those in the "login" table of the schema.
82
 
        The exception is "password", which is a cleartext password. makeuser
83
 
        will hash the password.
84
 
        Raises an exception if the user already exists.
 
300
        All user fields are to be passed as args. The argument names
 
301
        are the field names of the "login" table of the DB schema.
 
302
        However, instead of supplying a "passhash", you must supply a
 
303
        "password" argument, which will be hashed internally.
 
304
        Also "state" must not given explicitly; it is implicitly set to
 
305
        "no_agreement".
 
306
        Raises an exception if the user already exists, or the dict contains
 
307
        invalid keys or is missing required keys.
85
308
        """
86
 
        passhash = _passhash(password)
87
 
        query = ("INSERT INTO login (login, passhash, nick, fullname, "
88
 
            "rolenm, studentid) VALUES (%s, %s, %s, %s, %s, %s);" %
89
 
            (_escape(login), _escape(passhash), _escape(nick),
90
 
            _escape(fullname), _escape(rolenm), _escape(studentid)))
91
 
        if dry: return query
92
 
        self.db.query(query)
 
309
        if 'passhash' in kwargs:
 
310
            raise DBException("Supplied arguments include passhash (invalid).")
 
311
        # Make a copy of the dict. Change password to passhash (hashing it),
 
312
        # and set 'state' to "no_agreement".
 
313
        kwargs = copy.copy(kwargs)
 
314
        if 'password' in kwargs:
 
315
            kwargs['passhash'] = _passhash(kwargs['password'])
 
316
            del kwargs['password']
 
317
        kwargs['state'] = "no_agreement"
 
318
        # Execute the query.
 
319
        return self.insert(kwargs, "login", self.login_fields, dry=dry)
93
320
 
94
 
    def update_user(self, login, password=None, nick=None,
95
 
        fullname=None, rolenm=None, dry=False):
 
321
    def update_user(self, login, dry=False, **kwargs):
96
322
        """Updates fields of a particular user. login is the name of the user
97
 
        to update. The other arguments are optional fields which may be
98
 
        modified. If None or omitted, they do not get modified. login and
99
 
        studentid may not be modified.
 
323
        to update. The dict contains the fields which will be modified, and
 
324
        their new values. If any value is omitted from the dict, it does not
 
325
        get modified. login and studentid may not be modified.
 
326
        Passhash may be modified by supplying a "password" field, in
 
327
        cleartext, not a hashed password.
100
328
 
101
329
        Note that no checking is done. It is expected this function is called
102
330
        by a trusted source. In particular, it allows the password to be
104
332
        that the user knows the existing password before calling this function
105
333
        with a new one.
106
334
        """
107
 
        # Make a list of SQL fragments of the form "field = 'new value'"
108
 
        # These fragments are ALREADY-ESCAPED
109
 
        setlist = []
110
 
        if password is not None:
111
 
            setlist.append("passhash = " + _escape(_passhash(password)))
112
 
        if nick is not None:
113
 
            setlist.append("nick = " + _escape(nick))
114
 
        if fullname is not None:
115
 
            setlist.append("fullname = " + _escape(fullname))
116
 
        if rolenm is not None:
117
 
            setlist.append("rolenm = " + _escape(rolenm))
118
 
        if len(setlist) == 0:
119
 
            return
120
 
        # Join the fragments into a comma-separated string
121
 
        setstring = ', '.join(setlist)
122
 
        # Build the whole query as an UPDATE statement
123
 
        query = ("UPDATE login SET %s WHERE login = %s;"
124
 
            % (setstring, _escape(login)))
125
 
        if dry: return query
126
 
        self.db.query(query)
127
 
 
128
 
    def delete_user(self, login, dry=False):
129
 
        """Deletes a user login entry from the database."""
130
 
        query = "DELETE FROM login WHERE login = %s;" % _escape(login)
131
 
        if dry: return query
132
 
        self.db.query(query)
 
335
        if 'passhash' in kwargs:
 
336
            raise DBException("Supplied arguments include passhash (invalid).")
 
337
        if "password" in kwargs:
 
338
            kwargs = copy.copy(kwargs)
 
339
            kwargs['passhash'] = _passhash(kwargs['password'])
 
340
            del kwargs['password']
 
341
        return self.update({"login": login}, kwargs, "login",
 
342
            self.login_fields, self.login_primary, ["login", "studentid"],
 
343
            dry=dry)
133
344
 
134
345
    def get_user(self, login, dry=False):
135
346
        """Given a login, returns a dictionary of the user's DB fields,
137
348
 
138
349
        Raises a DBException if the login is not found in the DB.
139
350
        """
140
 
        query = ("SELECT login, nick, fullname, rolenm, studentid FROM login "
141
 
            "WHERE login = %s;" % _escape(login))
142
 
        if dry: return query
143
 
        result = self.db.query(query)
144
 
        # Expecting exactly one
145
 
        if result.ntuples() != 1:
146
 
            # It should not be possible for ntuples to be greater than 1
147
 
            assert (result.ntuples() < 1)
148
 
            raise DBException("get_user: No user with that login name")
149
 
        # Return as a dictionary
150
 
        return result.dictresult()[0]
 
351
        return self.get_single({"login": login}, "login",
 
352
            self.login_getfields, self.login_primary,
 
353
            error_notfound="get_user: No user with that login name", dry=dry)
 
354
 
 
355
    def get_users(self, dry=False):
 
356
        """Returns a list of all users. The list elements are a dictionary of
 
357
        the user's DB fields, excluding the passhash field.
 
358
        """
 
359
        return self.get_all("login", self.login_getfields, dry=dry)
151
360
 
152
361
    def user_authenticate(self, login, password, dry=False):
153
362
        """Performs a password authentication on a user. Returns True if
154
 
        "password" is the correct password for the given login, False
155
 
        otherwise. "password" is cleartext.
 
363
        "passhash" is the correct passhash for the given login, False
 
364
        otherwise.
156
365
        Also returns False if the login does not exist (so if you want to
157
366
        differentiate these cases, use get_user and catch an exception).
158
367
        """
170
379
        this. (The behaviour of doing so is undefined).
171
380
        """
172
381
        self.db.close()
 
382
        self.open = False