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

« back to all changes in this revision

Viewing changes to ivle/database.py

  • Committer: me at id
  • Date: 2009-01-15 01:06:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:branches%2Fstorm:1144
ivle.auth.* now uses Storm, and not ivle.(db|user). This changes the interface
    somewhat (lots of things take a 'store' argument), so fix callers.

ivle.db: Remove user_authenticate; it's now a method of ivle.database.User.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2009 The University of Melbourne
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
# Author: Matt Giuca, Will Grant
 
19
 
 
20
"""
 
21
Database Classes and Utilities for Storm ORM
 
22
 
 
23
This module provides all of the classes which map to database tables.
 
24
It also provides miscellaneous utility functions for database interaction.
 
25
"""
 
26
 
 
27
import md5
 
28
 
 
29
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
 
30
                         Reference
 
31
 
 
32
import ivle.conf
 
33
import ivle.caps
 
34
 
 
35
def get_conn_string():
 
36
    """
 
37
    Returns the Storm connection string, generated from the conf file.
 
38
    """
 
39
    return "postgres://%s:%s@%s:%d/%s" % (ivle.conf.db_user,
 
40
        ivle.conf.db_password, ivle.conf.db_host, ivle.conf.db_port,
 
41
        ivle.conf.db_dbname)
 
42
 
 
43
def get_store():
 
44
    """
 
45
    Open a database connection and transaction. Return a storm.store.Store
 
46
    instance connected to the configured IVLE database.
 
47
    """
 
48
    return Store(create_database(get_conn_string()))
 
49
 
 
50
class User(object):
 
51
    """
 
52
    Represents an IVLE user.
 
53
    """
 
54
    __storm_table__ = "login"
 
55
 
 
56
    id = Int(primary=True, name="loginid")
 
57
    login = Unicode()
 
58
    passhash = Unicode()
 
59
    state = Unicode()
 
60
    rolenm = Unicode()
 
61
    unixid = Int()
 
62
    nick = Unicode()
 
63
    pass_exp = DateTime()
 
64
    acct_exp = DateTime()
 
65
    last_login = DateTime()
 
66
    svn_pass = Unicode()
 
67
    email = Unicode()
 
68
    fullname = Unicode()
 
69
    studentid = Unicode()
 
70
    settings = Unicode()
 
71
 
 
72
    def _get_role(self):
 
73
        if self.rolenm is None:
 
74
            return None
 
75
        return ivle.caps.Role(self.rolenm)
 
76
    def _set_role(self, value):
 
77
        if not isinstance(value, ivle.caps.Role):
 
78
            raise TypeError("role must be an ivle.caps.Role")
 
79
        self.rolenm = unicode(value)
 
80
    role = property(_get_role, _set_role)
 
81
 
 
82
    def __init__(self, **kwargs):
 
83
        """
 
84
        Create a new User object. Supply any columns as a keyword argument.
 
85
        """
 
86
        for k,v in kwargs.items():
 
87
            if k.startswith('_') or not hasattr(self, k):
 
88
                raise TypeError("User got an unexpected keyword argument '%s'"
 
89
                    % k)
 
90
            setattr(self, k, v)
 
91
 
 
92
    def __repr__(self):
 
93
        return "<%s '%s'>" % (type(self).__name__, self.login)
 
94
 
 
95
    def authenticate(self, password):
 
96
        """Validate a given password against this user.
 
97
 
 
98
        Returns True if the given password matches the password hash for this
 
99
        User, False if it doesn't match, and None if there is no hash for the
 
100
        user.
 
101
        """
 
102
        if self.passhash is None:
 
103
            return None
 
104
        return self.hash_password(password) == self.passhash
 
105
 
 
106
    def hasCap(self, capability):
 
107
        """Given a capability (which is a Role object), returns True if this
 
108
        User has that capability, False otherwise.
 
109
        """
 
110
        return self.role.hasCap(capability)
 
111
 
 
112
    # XXX Should be @property
 
113
    def pass_expired(self):
 
114
        """Determines whether the pass_exp field indicates that
 
115
           login should be denied.
 
116
        """
 
117
        fieldval = self.pass_exp
 
118
        return fieldval is not None and time.localtime() > fieldval
 
119
    # XXX Should be @property
 
120
    def acct_expired(self):
 
121
        """Determines whether the acct_exp field indicates that
 
122
           login should be denied.
 
123
        """
 
124
        fieldval = self.acct_exp
 
125
        return fieldval is not None and time.localtime() > fieldval
 
126
 
 
127
    @staticmethod
 
128
    def hash_password(password):
 
129
        return md5.md5(password).hexdigest()
 
130
 
 
131
    @classmethod
 
132
    def get_by_login(cls, store, login):
 
133
        """
 
134
        Get the User from the db associated with a given store and
 
135
        login.
 
136
        """
 
137
        return store.find(cls, cls.login == unicode(login)).one()