1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2008 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# Module: User
# Author: Matt Giuca
# Date: 19/2/2008
# Provides a User class which stores the login details of a particular IVLE
# user. Objects of this class are expected to be stored in the request
# session.
import common.caps
# Similar to db.login_fields_list but contains a different set of fields.
# The User object does not contain all the fields.
user_fields_required = frozenset((
"login", "fullname", "role", "state", "unixid"
))
user_fields_list = (
"login", "state", "unixid", "email", "nick", "fullname",
"role", "studentid"
)
# Fields not included: passhash, acct_exp, pass_exp, last_login
class UserException(Exception):
pass
class User(object):
"""
Stores the login details of a particular IVLE user.
Its fields correspond to (most of) the fields of the "login" table of the
IVLE db.
All fields are always present, but some may be None.
"""
__slots__ = user_fields_list
def __init__(self, **kwargs):
# XXX Will ignore unknown fields instead of erroring
if "rolenm" in kwargs and "role" not in kwargs:
kwargs['role'] = common.caps.Role(kwargs['rolenm'])
del kwargs['rolenm']
for r in user_fields_list:
if r in kwargs:
self.__setattr__(r, kwargs[r])
elif r in user_fields_required:
# Required argument, not specified
raise TypeError("User: Required field %s missing" % repr(r))
else:
# Optional arguments
if r == "nick":
try:
self.nick = kwargs['fullname']
except KeyError:
raise TypeError("User: Required field "
"'fullname' missing")
else:
self.__setattr__(r, None)
def __repr__(self):
items = ["%s=%s" % (r, repr(self.__getattribute__(r)))
for r in user_fields_list]
return "User(" + ', '.join(items) + ")"
def hasCap(self, capability):
"""Given a capability (which is a Role object), returns True if this
User has that capability, False otherwise.
"""
return self.role.hasCap(capability)
|