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

« back to all changes in this revision

Viewing changes to lib/common/user.py

  • Committer: stevenbird
  • Date: 2008-02-19 21:17:21 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:512
Renaming of problems to exercises (initial commit).
Fix up module naming (exercises sometimes called tutorials).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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",
 
35
    "role", "studentid", "acct_exp", "pass_exp"
 
36
)
 
37
# Fields not included: passhash, last_login
 
38
 
 
39
class UserException(Exception):
 
40
    pass
 
41
 
 
42
class User(object):
 
43
    """
 
44
    Stores the login details of a particular IVLE user.
 
45
    Its fields correspond to (most of) the fields of the "login" table of the
 
46
    IVLE db.
 
47
    All fields are always present, but some may be None.
 
48
    """
 
49
    __slots__ = user_fields_list
 
50
    def __init__(self, **kwargs):
 
51
        # XXX Will ignore unknown fields instead of erroring
 
52
        if "rolenm" in kwargs and "role" not in kwargs:
 
53
            kwargs['role'] = common.caps.Role(kwargs['rolenm'])
 
54
            del kwargs['rolenm']
 
55
        for r in user_fields_list:
 
56
            if r in kwargs:
 
57
                self.__setattr__(r, kwargs[r])
 
58
            elif r in user_fields_required:
 
59
                # Required argument, not specified
 
60
                raise TypeError("User: Required field %s missing" % repr(r))
 
61
            else:
 
62
                # Optional arguments
 
63
                if r == "nick":
 
64
                    try:
 
65
                        self.nick = kwargs['fullname']
 
66
                    except KeyError:
 
67
                        raise TypeError("User: Required field "
 
68
                            "'fullname' missing")
 
69
                else:
 
70
                    self.__setattr__(r, None)
 
71
    def __repr__(self):
 
72
        items = ["%s=%s" % (r, repr(self.__getattribute__(r)))
 
73
            for r in user_fields_list]
 
74
        return "User(" + ', '.join(items) + ")"
 
75
    def __iter__(self):
 
76
        """Iteration yielding field:value pairs.
 
77
        (Allows the "dict" function to work on Users)
 
78
        """
 
79
        for r in user_fields_list:
 
80
            yield (r, self.__getattribute__(r))
 
81
 
 
82
    def hasCap(self, capability):
 
83
        """Given a capability (which is a Role object), returns True if this
 
84
        User has that capability, False otherwise.
 
85
        """
 
86
        return self.role.hasCap(capability)
 
87
 
 
88
    def pass_expired(self):
 
89
        """Determines whether the pass_exp field indicates that
 
90
           login should be denied.
 
91
        """
 
92
        fieldval = self.pass_exp
 
93
        return fieldval is not None and time.localtime() > fieldval
 
94
    def acct_expired(self):
 
95
        """Determines whether the acct_exp field indicates that
 
96
           login should be denied.
 
97
        """
 
98
        fieldval = self.acct_exp
 
99
        return fieldval is not None and time.localtime() > fieldval