root/tags/dabo-0.1.1/dSecurityManager.py

Revision 295, 4.6 kB (checked in by paul, 5 years ago)

Rename properties dApp and dForm to Application and Form, respectively.
Move the definition of Form out of dObject and into dPemMixin, as
dObject was too low a location (outside of UI).

  • Property svn:eol-style set to native
Line 
1 import dabo.common, time
2 from dLocalize import _
3
4 class dSecurityManager(dabo.common.dObject):
5    
6     def __init__(self, *args, **kwargs):
7         self.beforeInit()
8         dSecurityManager.doDefault(*args, **kwargs)
9         self.initProperties()
10         self.afterInit()
11        
12    
13     def beforeInit(self):
14         pass
15        
16     def afterInit(self):
17         pass
18        
19     def initProperties(self):
20         pass
21        
22        
23     def login(self):
24         # Ask the ui to display the login form to the user, and then
25         # validate the results. Return True if validation succeeds.
26        
27         ret = False     
28         for attempt in range(self.LoginAttemptsAllowed):
29             if attempt > 0:
30                 message = _("Login incorrect, please try again. (%s/%s)") % (
31                                     attempt+1, self.LoginAttemptsAllowed)
32             else:
33                 message = _("Please enter your login information.")
34             user, password = self.Application.uiApp.getLoginInfo(message)
35
36             if user is None:
37                 # login form canceled.
38                 break
39                
40             if self.validateLogin(user, password):
41                 self.__userName = user
42                 self.UserCaption = self.getUserCaptionFromUserName(user)
43                 self.__userGroups = self.getUserGroupsFromUserName(user)
44                 ret = True
45                 break
46             else:
47                 self.__userName = None
48                 self.UserCaption = ''
49                 self.__userGroups = ()
50             time.sleep(self.LoginPause)
51        
52         if ret:
53             self.afterLoginSuccess()
54         else:
55             self.afterLoginFailure()
56         return ret
57        
58    
59     def afterLoginFailure(self):
60         """ Subclass hook called after an unsuccessful login attempt.
61         """
62         pass
63    
64    
65     def afterLoginSuccess(self):
66         """ Subclass hook called after a successful login.
67         """
68         pass
69        
70        
71     def getUserCaptionFromUserName(self, userName):
72         """ Return a descriptive name of the user from the short userName.
73         
74         This is a subclass hook: you should override this method with your own
75         code that converts the short userName into something more descriptive,
76         such as 'pmcnett' -> 'Paul McNett'. The default behavior just echoes
77         back the userName.
78         """
79         return userName
80        
81        
82     def getUserGroupsFromUserName(self, userName):
83         """ Return the tuple of groups that userName belongs to.
84         
85         This is a subclass hook: you must override this method with your own
86         code that returns a tuple filled with the groups the user belongs to.
87         The identifiers used for the groups must match the group identifiers
88         as coded in your business objects.
89         """
90         return ()
91    
92    
93     def validateLogin(self, user, password):
94         """ Return True if the passed user and password combination is valid.
95         
96         This is a subclass hook: you must override this method with your own
97         code that does whatever is required to verify the login info. This would
98         probably include looking up the information in a database.
99         """
100         return False
101        
102        
103     def _getLoginAttemptsAllowed(self):
104         try:
105             return self._loginAttemptsAllowed
106         except AttributeError:
107             return 3
108            
109     def _setLoginAttemptsAllowed(self, value):
110         self._loginAttemptsAllowed = int(value)
111        
112        
113     def _getLoginPause(self):
114         try:
115             return self._loginPause
116         except AttributeError:
117             return 0.25
118            
119     def _setLoginPause(self, value):
120         self._loginPause = float(value)
121        
122        
123     def _getRequireAppLogin(self):
124         try:
125             return self._requireAppLogin
126         except AttributeError:
127             return True
128            
129     def _setRequireAppLogin(self, value):
130         self._requireAppLogin = bool(value)
131        
132        
133     def _getUserName(self):
134         try:
135             return self.__userName
136         except AttributeError:
137             return None
138            
139    
140     def _getUserCaption(self):
141         try:
142             return self._userCaption
143         except AttributeError:
144             return ''
145            
146     def _setUserCaption(self, value):
147         if type(value) in (type(str()), type(unicode())):
148             self._userCaption = value
149         else:
150             raise TypeError, 'User caption must be string or unicode.'
151            
152    
153     def _getUserGroups(self):
154         try:
155             return self.__userGroups
156         except AttributeError:
157             return ()
158            
159     LoginAttemptsAllowed = property(_getLoginAttemptsAllowed, _setLoginAttemptsAllowed, None,
160                     _('Specifies the number of attempts the user has to login successfully.'))
161                    
162     LoginPause = property(_getLoginPause, _setLoginPause, None,
163                     _('Specifies the number of (fractional) seconds to wait between '
164                     'successive login attempts.'))
165                    
166     RequireAppLogin = property(_getRequireAppLogin, _setRequireAppLogin, None,
167                     _('Specifies whether the user is required to login to the application '
168                     'at startup. Note that this does not turn on/off login prompts globally, '
169                     'just at application startup.'))
170                        
171     UserCaption = property(_getUserCaption, _setUserCaption, None,
172                     _('The long descriptive name of the logged-on user.'))
173    
174     UserGroups = property(_getUserGroups, None, None,
175                     _('The tuple of groups that the user belongs to.'))
176    
177     UserName = property(_getUserName, None, None,
178                     _('The name of the logged-on user. Read-only.'))
179                    
180                    
Note: See TracBrowser for help on using the browser.