Author: ianb
Date: 2004-04-21 16:52:45 -0600 (Wed, 21 Apr 2004)
New Revision: 90
Added:
LoginKit/usermanager.py
Removed:
LoginKit/main.py
Modified:
LoginKit/__init__.py
LoginKit/userindex.py
Log:
Refactoring...
Modified: LoginKit/__init__.py
===================================================================
--- LoginKit/__init__.py 2004-04-21 04:35:25 UTC (rev 89)
+++ LoginKit/__init__.py 2004-04-21 22:52:45 UTC (rev 90)
@@ -1,4 +1,4 @@
-from main import UserManager, SimpleUser
+from usermanager import UserManager, SimpleUser
from usercomponent import UserComponent
def InstallInWebKit(appServer):
Deleted: LoginKit/main.py
===================================================================
--- LoginKit/main.py 2004-04-21 04:35:25 UTC (rev 89)
+++ LoginKit/main.py 2004-04-21 22:52:45 UTC (rev 90)
@@ -1,200 +0,0 @@
-import os, re, sha, md5, threading
-
-defaultUsernameRE = re.compile(r'^[a-zA-Z][a-zA-Z0-9_@...]*$')
-
-class UserExistsError(Exception):
- """
- Raised when there's an attempt to create a new user given a
- username that is already in use.
- """
- pass
-
-class UserManager:
-
- """
- The UserManager creates and manages users in a system. As such it
- also handles the identification phase, which usually implies
- password verification.
- """
-
- def __init__(self, usernameValidator=None,
- passwordValidator=None,
- usersConfidential=0):
- if usernameValidator:
- self.usernameValid = usernameValidator
- if passwordValidator:
- self.passwordValid = passwordValidator
- self._usersConfidential = usersConfidential
-
- def passwordCorrect(self, username, password):
- if not self.userExists(username):
- return 0
- user = self.userForUsername(username)
- return user.passwordCorrect(password)
-
- def userExists(self, username):
- userID = self.userIDForUsername(username)
- return userID is not None
-
- def usernameInvalid(self, username):
- """
- This checks if a username is acceptable, not if it exists.
- Returns the error message if it is invalid; else None.
- """
- if not username:
- return 'Username must not be empty'
- if not defaultUsernameRE.search(username):
- return 'Username must start with a letter, and contain only letters, numbers, _, ., -, and @'
- return None
-
- def passwordInvalid(self, password):
- """
- This checks if a password is acceptable, not if it is correct.
- E.g., a policy where each password much have a non-letter
- character would be placed here. Returns the error message if
- invalid; else None
- """
- if not password:
- return 'Your password must not be empty.'
- return None
-
- def usersConfidential(self):
- """
- If true, then usernames should not be revealed; specifically
- this is for bad logins. If users are confidential, then the
- message would typically be 'username or password is incorrect',
- if users are not confidential than we can tell the user if
- the username they provided exists.
- """
- return self._usersConfidential
-
- def userForUsername(self, username):
- userID = self.userIDForUsername(username)
- if userID is None:
- return None
- return self.userForUserID(userID)
-
- def userForUserID(self, userID):
- return None
-
- def userIDForUsername(self, username):
- """
- External usernames and internal IDs are a separate concept;
- but in many systems they are identical (as in this default
- implementation).
- """
- return username
-
- def usernameForUserID(self, userID):
- return userID
-
- def userChanged(self, user):
- """
- This is called by the user objects whenever they are changed
- in a way that the UserManager should be aware of, e.g., if
- the UserManager is handling persistence.
- """
- pass
-
- def allUserIDs(self):
- """
- Returns a list of all user IDs.
- """
- raise NotImplementedError
-
- def allUsers(self):
- """
- Returns a list of all user objects.
- """
- return [self.userForUserID(userID)
- for userID in self.allUserIDs()]
-
-############################################################
-## Hash Functions
-############################################################
-
-def shaHash(s, salt):
- return sha.new(s + salt).hexdigest()
-
-def md5Hash(s, salt):
- return sha.new(s + salt).hexdigest()
-
-def noHash(s, salt):
- return s
-
-
-class SimpleUser:
-
- """
- SimpleUser is a very simple implementation of a user, and an
- appropriate superclass for more complex implementations.
- """
-
- _hasher = shaHash
-
- def __init__(self, manager):
- self._manager = manager
- self._username = None
- self._userID = None
- self._password = None
- self._email = None
- self._name = None
-
- def __repr__(self):
- return '<%s %r at %s>' % (self.__class__.__name__,
- self._username,
- hex(id(self)))
-
- def setUsername(self, username):
- oldUsername = self.username()
- self._username = username
- self._manager.changeUsername(self, oldUsername)
- self.changed()
-
- def username(self):
- return self._username
-
- def email(self):
- return self._email
-
- def setEmail(self, value):
- self._email = value
- self.changed()
-
- def name(self):
- return self._name
-
- def setName(self, value):
- self._name = value
- self.changed()
-
- def setUserID(self, userID):
- assert self._userID is None
- self._userID = userID
- self.changed()
-
- def userID(self):
- return self._userID
-
- def hashPassword(self, password):
- return self._hasher.im_func(password, str(self._userID))
-
- def setPassword(self, password):
- self._password = self.hashPassword(password)
- self.changed()
-
- def passwordCorrect(self, password):
- return self.hashPassword(password) == self._password
-
- def __getstate__(self):
- # We don't want to pickle the manager, just this object...
- d = self.__dict__.copy()
- if d.has_key('_manager'):
- del d['_manager']
- return d
-
- def setUserManager(self, userManager):
- self._userManager = userManager
-
- def changed(self):
- self._manager.userChanged(self)
Modified: LoginKit/userindex.py
===================================================================
--- LoginKit/userindex.py 2004-04-21 04:35:25 UTC (rev 89)
+++ LoginKit/userindex.py 2004-04-21 22:52:45 UTC (rev 90)
@@ -25,8 +25,6 @@
% self.htmlEncode(str(getattr(user, getter)())))
write('</tr>\n')
write('</table>')
-
-
class UserIndexComponent(Component):
Copied: LoginKit/usermanager.py (from rev 89, LoginKit/main.py)
|