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

499 by mattgiuca
Added module: lib/common/user.
1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2008 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
# Module: User
19
# Author: Matt Giuca
20
# Date:   19/2/2008
21
22
# Provides a User class which stores the login details of a particular IVLE
23
# user. Objects of this class are expected to be stored in the request
24
# session.
25
26
import common.caps
27
28
# Similar to db.login_fields_list but contains a different set of fields.
29
# The User object does not contain all the fields.
30
user_fields_required = frozenset((
31
    "login", "fullname", "role", "state", "unixid"
32
))
33
user_fields_list = (
34
    "login", "state", "unixid", "email", "nick", "fullname",
575 by mattgiuca
common/usr, common/db: Added field to User, "local_password",
35
    "role", "studentid", "acct_exp", "pass_exp", "svn_pass",
36
    "local_password"
499 by mattgiuca
Added module: lib/common/user.
37
)
503 by mattgiuca
user.py: Added acct_expired and pass_expired methods.
38
# Fields not included: passhash, last_login
499 by mattgiuca
Added module: lib/common/user.
39
40
class UserException(Exception):
41
    pass
42
43
class User(object):
44
    """
45
    Stores the login details of a particular IVLE user.
46
    Its fields correspond to (most of) the fields of the "login" table of the
47
    IVLE db.
48
    All fields are always present, but some may be None.
49
    """
50
    __slots__ = user_fields_list
51
    def __init__(self, **kwargs):
52
        # XXX Will ignore unknown fields instead of erroring
53
        if "rolenm" in kwargs and "role" not in kwargs:
54
            kwargs['role'] = common.caps.Role(kwargs['rolenm'])
575 by mattgiuca
common/usr, common/db: Added field to User, "local_password",
55
        if "passhash" in kwargs and "local_password" not in kwargs:
56
            kwargs['local_password'] = kwargs['passhash'] is not None
499 by mattgiuca
Added module: lib/common/user.
57
        for r in user_fields_list:
58
            if r in kwargs:
59
                self.__setattr__(r, kwargs[r])
60
            elif r in user_fields_required:
61
                # Required argument, not specified
62
                raise TypeError("User: Required field %s missing" % repr(r))
63
            else:
64
                # Optional arguments
65
                if r == "nick":
66
                    try:
67
                        self.nick = kwargs['fullname']
68
                    except KeyError:
69
                        raise TypeError("User: Required field "
70
                            "'fullname' missing")
71
                else:
72
                    self.__setattr__(r, None)
73
    def __repr__(self):
74
        items = ["%s=%s" % (r, repr(self.__getattribute__(r)))
75
            for r in user_fields_list]
76
        return "User(" + ', '.join(items) + ")"
504 by mattgiuca
Warning: Broken build, but rather unavoidable or this commit will spiral out
77
    def __iter__(self):
78
        """Iteration yielding field:value pairs.
79
        (Allows the "dict" function to work on Users)
80
        """
81
        for r in user_fields_list:
82
            yield (r, self.__getattribute__(r))
503 by mattgiuca
user.py: Added acct_expired and pass_expired methods.
83
499 by mattgiuca
Added module: lib/common/user.
84
    def hasCap(self, capability):
85
        """Given a capability (which is a Role object), returns True if this
86
        User has that capability, False otherwise.
87
        """
88
        return self.role.hasCap(capability)
503 by mattgiuca
user.py: Added acct_expired and pass_expired methods.
89
90
    def pass_expired(self):
91
        """Determines whether the pass_exp field indicates that
92
           login should be denied.
93
        """
94
        fieldval = self.pass_exp
95
        return fieldval is not None and time.localtime() > fieldval
96
    def acct_expired(self):
97
        """Determines whether the acct_exp field indicates that
98
           login should be denied.
99
        """
100
        fieldval = self.acct_exp
101
        return fieldval is not None and time.localtime() > fieldval