Many applications are surprisingly easy to create in Zope. This article will teach you how to create a simple user database using Zope objects for data storage.
Our final step in creating our user database is to create a system that will challenge the user's username and password when he or she tries to access a restricted page, permitting him or her to view the page only if their username and password match the stored data. This is easily done by checking the user's cookies. Create a Script ( Python ) object named verify with the following code:
if context.REQUEST.has_key ( 'username' ) and context.REQUEST.has_key ( 'password' ): if not hasattr ( container.users, context.REQUEST.username ): return False userObject = getattr ( container.users,context.REQUEST.username ) if context.REQUEST.password != userObject.password: return False userData = { 'email' : userObject.email } return userData return False
First, the script checks to see if the two cookies containing the user's username and password are set. If they are, the script moves on to check if the supplied uesrname exists. If it does, the script checks the passwords against each other. If they match, a dictionary is returned containing the user's email address.
We'll now create a page to test out our system. Create a Zope Page Template object named private_html:
<html> <head> <title>Private Area</title> </head> <body tal:define='verifyStatus container/verify'> <span tal:condition='not:verifyStatus'> Uh oh! You don't have permission to access this area! </span> <span tal:condition='verifyStatus'> Welcome to the ultra-secret club.<br /><br /> Your e-mail address is:<br /> <span style='font-weight: bold; 'tal:content='verifyStatus/email'></span> </span> </body> </html>
The page calls the verify object and stores the data returned in a variable called verifyStatus. If the user supplied the correct information, verifyStatus will be a dictionary containing his or her email address. The email address is printed if the user has access. If not, the user is presented with an error message.
Conclusion
Our system is now complete. If you wish, you can add a field to the registration form asking for other details. This can be accomplished in the same way we added a field and property for the user's email address. Don't forget to add the property to the dictionary in verify.
As you probably noticed, user management systems are suprisingly easy to create in Zope, as are many other types of applications. A user database could also be implemented using a relational database, such as MySQL. You are not restricted to using flat files for storage.