root/tags/dabo-0.3.1/dSecurityManager.py

Revision 541, 4.7 kB (checked in by paul, 4 years ago)

Removed the test directory in biz.

Commented out all doDefault() calls and replaced with super().method() calls:
I ran a test and discovered that the latter executes 1.5 orders of magnitude
faster, and while we are talking 1/1000's of a second versus 1/100's of a
second, if lots of objects are being created it could conceivably be noticeable.

I think that we should still show doDefault() in user documentation and demos,
but that we should try to use super(cls, self).method() in the dabo library
code. It is more explicit, doesn't really require that much more typing, and
more efficient at runtime.

The standard super() will fail if the method doesn't exist in the superclass, while
doDefault() has code to pass silently in that case. So doDefault() is definitely
the appropriate method to show to users.

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