1
#------------------------------------------------------------------------------
2
# Copyright (c) 2008, Riverbank Computing Limited
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!
11
# Author: Riverbank Computing Limited
12
# Description: <Enthought permissions package component>
13
#------------------------------------------------------------------------------
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
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
29
class UserManager(HasTraits):
30
"""The default user manager implementation."""
32
implements(IUserManager)
34
#### 'IUserManager' interface #############################################
36
management_actions = List(Instance(Action))
38
user = Instance(IUser)
40
user_actions = List(Instance(Action))
42
user_authenticated = Event(IUser)
44
#### 'UserManager' interface ##############################################
47
user_db = Instance(IUserDatabase)
49
###########################################################################
50
# 'IUserManager' interface.
51
###########################################################################
53
def bootstrapping(self):
54
"""Return True if we are bootstrapping, ie. no users have been defined.
57
return self.user_db.bootstrapping()
59
def authenticate_user(self):
60
"""Authenticate the user."""
62
if self.user_db.authenticate_user(self.user):
63
self.user.authenticated = True
65
# Tell the policy manager before everybody else.
66
get_permissions_manager().policy_manager.load_policy(self.user)
68
self.user_authenticated = self.user
70
def unauthenticate_user(self):
71
"""Unauthenticate the user."""
73
if self.user.authenticated and self.user_db.unauthenticate_user(self.user):
74
self.user.authenticated = False
76
# Tell the policy manager before everybody else.
77
get_permissions_manager().policy_manager.load_policy(None)
79
self.user_authenticated = None
81
def matching_user(self, name):
84
return self.user_db.matching_user(name)
86
###########################################################################
88
###########################################################################
90
def _management_actions_default(self):
91
"""Return the list of management actions."""
93
from enthought.permissions.secure_proxy import SecureProxy
95
user_db = self.user_db
97
perm = ManageUsersPermission()
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))
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))
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))
115
def _user_actions_default(self):
116
"""Return the list of user actions."""
120
if self.user_db.can_change_password:
121
actions.append(_ChangePasswordAction())
125
def _user_default(self):
126
"""Return the default current user."""
128
return self.user_db.user_factory()
130
def _user_db_default(self):
131
"""Return the default user database."""
133
# Defer to an external user database if there is one.
135
from enthought.permissions.external.user_database import UserDatabase
137
from enthought.permissions.default.user_database import UserDatabase
139
return UserDatabase()
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."""
147
#### 'Action' interface ###################################################
149
enabled = Bool(False)
151
name = Unicode("&Change Password...")
153
###########################################################################
154
# 'object' interface.
155
###########################################################################
157
def __init__(self, **traits):
158
"""Initialise the object."""
160
super(_ChangePasswordAction, self).__init__(**traits)
162
get_permissions_manager().user_manager.on_trait_event(self._refresh_enabled, 'user_authenticated')
164
###########################################################################
165
# 'Action' interface.
166
###########################################################################
168
def perform(self, event):
169
"""Perform the action."""
171
um = get_permissions_manager().user_manager
172
um.user_db.change_password(um.user)
174
###########################################################################
176
###########################################################################
178
def _refresh_enabled(self, user):
179
"""Invoked whenever the current user's authorisation state changes."""
181
self.enabled = user is not None