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

« back to all changes in this revision

Viewing changes to lib/common/user.py

  • Committer: dcoles
  • Date: 2008-07-18 02:40:58 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:912
Makeuser: Makeuser will now chown all files in a users home directory to the
unixid provided by the database. This means remakeallusers.py can be used to
update everyones unixids after a database change. The makeuser script will also 
no longer generate a random unixid when one is not given on the command line.

Database: Change to database so that a sequence is used by default. The allowed  
range is 1000-29999 (let's start at 5000, because 1000+ is used by adduser.  
Reference: http://www.debian.org/doc/debian-policy/ch-opersys.html)
See SF Bug '[ 2012190 ] Student UID generation'
Migration script will reallocate all users a new unixid from the sequence.

Usermgt-Server: Now no longer generates a random unixid. Rely on the database 
to come up with a new sequence number.

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 db
 
27
import time
 
28
import common.caps
 
29
 
 
30
# Similar to db.login_fields_list but contains a different set of fields.
 
31
# The User object does not contain all the fields.
 
32
user_fields_required = frozenset((
 
33
    "login", "fullname", "role", "state", "unixid"
 
34
))
 
35
user_fields_list = (
 
36
    "login", "state", "unixid", "email", "nick", "fullname",
 
37
    "role", "studentid", "acct_exp", "pass_exp", "last_login",
 
38
    "svn_pass", "local_password",
 
39
)
 
40
timestamp_fields = frozenset((
 
41
    "acct_exp", "pass_exp", "last_login",
 
42
))
 
43
# Fields not included: passhash, last_login
 
44
 
 
45
class UserException(Exception):
 
46
    pass
 
47
 
 
48
class User(object):
 
49
    """
 
50
    Stores the login details of a particular IVLE user.
 
51
    Its fields correspond to (most of) the fields of the "login" table of the
 
52
    IVLE db.
 
53
    All fields are always present, but some may be None.
 
54
    """
 
55
    __slots__ = user_fields_list
 
56
    def __init__(self, **kwargs):
 
57
        # XXX Will ignore unknown fields instead of erroring
 
58
        if "rolenm" in kwargs and "role" not in kwargs:
 
59
            kwargs['role'] = common.caps.Role(kwargs['rolenm'])
 
60
        if "passhash" in kwargs and "local_password" not in kwargs:
 
61
            kwargs['local_password'] = kwargs['passhash'] is not None
 
62
        for r in user_fields_list:
 
63
            if r in kwargs:
 
64
                self.__setattr__(r, kwargs[r])
 
65
            elif r in user_fields_required:
 
66
                # Required argument, not specified
 
67
                raise TypeError("User: Required field %s missing" % repr(r))
 
68
            else:
 
69
                # Optional arguments
 
70
                if r == "nick":
 
71
                    try:
 
72
                        self.nick = kwargs['fullname']
 
73
                    except KeyError:
 
74
                        raise TypeError("User: Required field "
 
75
                            "'fullname' missing")
 
76
                else:
 
77
                    self.__setattr__(r, None)
 
78
        for field in timestamp_fields:
 
79
            # Convert all timestamp fields from strings to Timestamp objects
 
80
            if hasattr(self, field):
 
81
                val = self.__getattribute__(field)
 
82
                if val is not None:
 
83
                    val = time.strptime(val, db.TIMESTAMP_FORMAT)
 
84
                    self.__setattr__(field, val)
 
85
 
 
86
    def __repr__(self):
 
87
        items = ["%s=%s" % (r, repr(self.__getattribute__(r)))
 
88
            for r in user_fields_list]
 
89
        return "User(" + ', '.join(items) + ")"
 
90
    def __iter__(self):
 
91
        """Iteration yielding field:value pairs.
 
92
        (Allows the "dict" function to work on Users)
 
93
        """
 
94
        for r in user_fields_list:
 
95
            yield (r, self.__getattribute__(r))
 
96
 
 
97
    def hasCap(self, capability):
 
98
        """Given a capability (which is a Role object), returns True if this
 
99
        User has that capability, False otherwise.
 
100
        """
 
101
        return self.role.hasCap(capability)
 
102
 
 
103
    def pass_expired(self):
 
104
        """Determines whether the pass_exp field indicates that
 
105
           login should be denied.
 
106
        """
 
107
        fieldval = self.pass_exp
 
108
        return fieldval is not None and time.localtime() > fieldval
 
109
    def acct_expired(self):
 
110
        """Determines whether the acct_exp field indicates that
 
111
           login should be denied.
 
112
        """
 
113
        fieldval = self.acct_exp
 
114
        return fieldval is not None and time.localtime() > fieldval