[Zope-PTK] Some methods for the PortalMembership kit
Morten W. Petersen
morten@esol.no
21 Dec 2000 15:23:54 +0100
Hi guys,
just thought I'd share some functions I've made for the PortalMembership kit.
These methods lets us use a tree structure like so:
/a/a/a/a/Members
/a/c/d/a/Members
and authenticate from the root, /a, and redirected to their respective home
folders.
Any thoughts or comments?
def get_all_login_managers(self):
"""Gets all login managers from the sub-tree."""
splitter = lambda x: x[-1]
return map(splitter, self.ZopeFind(self,
obj_metatypes=['Login Manager'],
search_sub=1))
def get_all_user_objects(self):
"""Gets all user object from the sub-tree."""
users = []
for match in self.ZopeFind(self,
obj_metatypes=['Login Manager'],
search_sub=1):
login_manager_name, login_manager = match[:]
users.extend(login_manager.getMembers())
return users
def global_get_user(self, username):
"""Traverses the entire sub-tree and returns the first user
with the given username."""
# This method assumes that there is in fact only one user
# with the given username.
filter_function = eval("lambda x: x.getUserName() == '%s'" % username)
try:
return filter(filter_function, get_all_user_objects(self))[0]
except IndexError:
return None
def global_authenticate_user(self, username, password, REQUEST):
"""Returns a user object if we can match a username & password."""
user = global_get_user(self, username)
if not user:
return user
login_manager = user.getParentNode()
if user and login_manager.UserSource.authenticateUser(
user, password,
REQUEST):
return user
else:
return None
def authenticate_and_return_url(self, username, password, REQUEST):
"""Returns a URL if we can match username and password."""
user = global_authenticate_user(self, username, password, REQUEST)
if user:
return user.getHomeUrl()
else:
'login_form?authentication_failed:int=1'
-Morten