~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to enthought/permissions/default/user_manager.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#------------------------------------------------------------------------------
2
 
# Copyright (c) 2008, Riverbank Computing Limited
3
 
# All rights reserved.
4
 
#
5
 
# This software is provided without warranty under the terms of the BSD
6
 
# license included in enthought/LICENSE.txt and may be redistributed only
7
 
# under the conditions described in the aforementioned license.  The license
8
 
# is also available online at http://www.enthought.com/licenses/BSD.txt
9
 
# Thanks for using Enthought open source!
10
 
#
11
 
# Author: Riverbank Computing Limited
12
 
# Description: <Enthought permissions package component>
13
 
#------------------------------------------------------------------------------
14
 
 
15
 
 
16
 
# Enthought library imports.
17
 
from enthought.pyface.action.api import Action
18
 
from enthought.traits.api import Bool, Event, HasTraits, implements, \
19
 
        Instance, List, Unicode
20
 
 
21
 
# Local imports.
22
 
from enthought.permissions.i_user import IUser
23
 
from enthought.permissions.i_user_manager import IUserManager
24
 
from enthought.permissions.package_globals import get_permissions_manager
25
 
from enthought.permissions.permission import ManageUsersPermission
26
 
from i_user_database import IUserDatabase
27
 
 
28
 
 
29
 
class UserManager(HasTraits):
30
 
    """The default user manager implementation."""
31
 
 
32
 
    implements(IUserManager)
33
 
 
34
 
    #### 'IUserManager' interface #############################################
35
 
 
36
 
    management_actions = List(Instance(Action))
37
 
 
38
 
    user = Instance(IUser)
39
 
 
40
 
    user_actions = List(Instance(Action))
41
 
 
42
 
    user_authenticated = Event(IUser)
43
 
 
44
 
    #### 'UserManager' interface ##############################################
45
 
 
46
 
    # The user database.
47
 
    user_db = Instance(IUserDatabase)
48
 
 
49
 
    ###########################################################################
50
 
    # 'IUserManager' interface.
51
 
    ###########################################################################
52
 
 
53
 
    def bootstrapping(self):
54
 
        """Return True if we are bootstrapping, ie. no users have been defined.
55
 
        """
56
 
 
57
 
        return self.user_db.bootstrapping()
58
 
 
59
 
    def authenticate_user(self):
60
 
        """Authenticate the user."""
61
 
 
62
 
        if self.user_db.authenticate_user(self.user):
63
 
            self.user.authenticated = True
64
 
 
65
 
            # Tell the policy manager before everybody else.
66
 
            get_permissions_manager().policy_manager.load_policy(self.user)
67
 
 
68
 
            self.user_authenticated = self.user
69
 
 
70
 
    def unauthenticate_user(self):
71
 
        """Unauthenticate the user."""
72
 
 
73
 
        if self.user.authenticated and self.user_db.unauthenticate_user(self.user):
74
 
            self.user.authenticated = False
75
 
 
76
 
            # Tell the policy manager before everybody else.
77
 
            get_permissions_manager().policy_manager.load_policy(None)
78
 
 
79
 
            self.user_authenticated = None
80
 
 
81
 
    def matching_user(self, name):
82
 
        """Select a user."""
83
 
 
84
 
        return self.user_db.matching_user(name)
85
 
 
86
 
    ###########################################################################
87
 
    # Trait handlers.
88
 
    ###########################################################################
89
 
 
90
 
    def _management_actions_default(self):
91
 
        """Return the list of management actions."""
92
 
 
93
 
        from enthought.permissions.secure_proxy import SecureProxy
94
 
 
95
 
        user_db = self.user_db
96
 
        actions = []
97
 
        perm = ManageUsersPermission()
98
 
 
99
 
        if user_db.can_add_user:
100
 
            act = Action(name="&Add a User...", on_perform=user_db.add_user)
101
 
            actions.append(SecureProxy(act, permissions=[perm], show=False))
102
 
 
103
 
        if user_db.can_modify_user:
104
 
            act = Action(name="&Modify a User...",
105
 
                    on_perform=user_db.modify_user)
106
 
            actions.append(SecureProxy(act, permissions=[perm], show=False))
107
 
 
108
 
        if user_db.can_delete_user:
109
 
            act = Action(name="&Delete a User...",
110
 
                    on_perform=user_db.delete_user)
111
 
            actions.append(SecureProxy(act, permissions=[perm], show=False))
112
 
 
113
 
        return actions
114
 
 
115
 
    def _user_actions_default(self):
116
 
        """Return the list of user actions."""
117
 
 
118
 
        actions = []
119
 
 
120
 
        if self.user_db.can_change_password:
121
 
            actions.append(_ChangePasswordAction())
122
 
 
123
 
        return actions
124
 
 
125
 
    def _user_default(self):
126
 
        """Return the default current user."""
127
 
 
128
 
        return self.user_db.user_factory()
129
 
 
130
 
    def _user_db_default(self):
131
 
        """Return the default user database."""
132
 
 
133
 
        # Defer to an external user database if there is one.
134
 
        try:
135
 
            from enthought.permissions.external.user_database import UserDatabase
136
 
        except ImportError:
137
 
            from enthought.permissions.default.user_database import UserDatabase
138
 
 
139
 
        return UserDatabase()
140
 
 
141
 
 
142
 
class _ChangePasswordAction(Action):
143
 
    """An action that allows the current user to change their password.  It
144
 
    isn't exported through actions/api.py because it is specific to this user
145
 
    manager implementation."""
146
 
 
147
 
    #### 'Action' interface ###################################################
148
 
 
149
 
    enabled = Bool(False)
150
 
 
151
 
    name = Unicode("&Change Password...")
152
 
 
153
 
    ###########################################################################
154
 
    # 'object' interface.
155
 
    ###########################################################################
156
 
 
157
 
    def __init__(self, **traits):
158
 
        """Initialise the object."""
159
 
 
160
 
        super(_ChangePasswordAction, self).__init__(**traits)
161
 
 
162
 
        get_permissions_manager().user_manager.on_trait_event(self._refresh_enabled, 'user_authenticated')
163
 
 
164
 
    ###########################################################################
165
 
    # 'Action' interface.
166
 
    ###########################################################################
167
 
 
168
 
    def perform(self, event):
169
 
        """Perform the action."""
170
 
 
171
 
        um = get_permissions_manager().user_manager
172
 
        um.user_db.change_password(um.user)
173
 
 
174
 
    ###########################################################################
175
 
    # Private interface.
176
 
    ###########################################################################
177
 
 
178
 
    def _refresh_enabled(self, user):
179
 
        """Invoked whenever the current user's authorisation state changes."""
180
 
 
181
 
        self.enabled = user is not None