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

1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
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
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
27
import md5
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
28
import datetime
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
29
1080.1.4 by matt.giuca
ivle.database: Added User class.
30
from storm.locals import create_database, Store, Int, Unicode, DateTime, \
31
                         Reference
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
32
33
import ivle.conf
1080.1.4 by matt.giuca
ivle.database: Added User class.
34
import ivle.caps
1080.1.2 by matt.giuca
New module: ivle.database. Classes and utilities for Storm ORM.
35
36
def get_conn_string():
37
    """
38
    Returns the Storm connection string, generated from the conf file.
39
    """
40
    return "postgres://%s:%s@%s:%d/%s" % (ivle.conf.db_user,
41
        ivle.conf.db_password, ivle.conf.db_host, ivle.conf.db_port,
42
        ivle.conf.db_dbname)
43
44
def get_store():
45
    """
46
    Open a database connection and transaction. Return a storm.store.Store
47
    instance connected to the configured IVLE database.
48
    """
49
    return Store(create_database(get_conn_string()))
1080.1.4 by matt.giuca
ivle.database: Added User class.
50
51
class User(object):
52
    """
53
    Represents an IVLE user.
54
    """
55
    __storm_table__ = "login"
56
57
    id = Int(primary=True, name="loginid")
58
    login = Unicode()
59
    passhash = Unicode()
60
    state = Unicode()
61
    rolenm = Unicode()
62
    unixid = Int()
63
    nick = Unicode()
64
    pass_exp = DateTime()
65
    acct_exp = DateTime()
66
    last_login = DateTime()
67
    svn_pass = Unicode()
68
    email = Unicode()
69
    fullname = Unicode()
70
    studentid = Unicode()
71
    settings = Unicode()
72
73
    def _get_role(self):
74
        if self.rolenm is None:
75
            return None
76
        return ivle.caps.Role(self.rolenm)
77
    def _set_role(self, value):
78
        if not isinstance(value, ivle.caps.Role):
79
            raise TypeError("role must be an ivle.caps.Role")
80
        self.rolenm = unicode(value)
81
    role = property(_get_role, _set_role)
82
83
    def __init__(self, **kwargs):
84
        """
85
        Create a new User object. Supply any columns as a keyword argument.
86
        """
87
        for k,v in kwargs.items():
88
            if k.startswith('_') or not hasattr(self, k):
89
                raise TypeError("User got an unexpected keyword argument '%s'"
90
                    % k)
91
            setattr(self, k, v)
92
93
    def __repr__(self):
94
        return "<%s '%s'>" % (type(self).__name__, self.login)
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
95
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
96
    def authenticate(self, password):
97
        """Validate a given password against this user.
98
99
        Returns True if the given password matches the password hash for this
100
        User, False if it doesn't match, and None if there is no hash for the
101
        user.
102
        """
103
        if self.passhash is None:
104
            return None
105
        return self.hash_password(password) == self.passhash
106
1080.1.7 by matt.giuca
The new ivle.database.User class is now used in Request and usrmgt, which
107
    def hasCap(self, capability):
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
108
        """Given a capability (which is a Role object), returns True if this
109
        User has that capability, False otherwise.
110
        """
111
        return self.role.hasCap(capability)
112
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
113
    @property
114
    def password_expired(self):
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
115
        fieldval = self.pass_exp
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
116
        return fieldval is not None and datetime.datetime.now() > fieldval
117
118
    @property
119
    def account_expired(self):
1080.1.5 by matt.giuca
ivle.database.User: Add the missing methods from ivle.user.User.
120
        fieldval = self.acct_exp
1080.1.15 by me at id
Give ivle.database.User {password,account}_expired attributes, and get
121
        return fieldval is not None and datetime.datetime.now() > fieldval
1080.1.6 by matt.giuca
ivle.database.User: Added get_by_login method.
122
1080.1.13 by me at id
ivle.database.User: Add an authenticate() method, and a hash_password()
123
    @staticmethod
124
    def hash_password(password):
125
        return md5.md5(password).hexdigest()
126
1080.1.6 by matt.giuca
ivle.database.User: Added get_by_login method.
127
    @classmethod
128
    def get_by_login(cls, store, login):
129
        """
130
        Get the User from the db associated with a given store and
131
        login.
132
        """
1080.1.7 by matt.giuca
The new ivle.database.User class is now used in Request and usrmgt, which
133
        return store.find(cls, cls.login == unicode(login)).one()