Home

  Documentation

  Download

  News

  Examples

auth.py Source Code

C:/Work/Lemon/Backups/Lemon-0.2.10/Lemon-0.2.10/../../doc/examples/code/auth.py
#!/usr/bin/env python

"""Auth Example. Login using 
Username:   test
Password:   123"""

import cgitb; cgitb.enable()
import sys,os
sys.path.append('../')
sys.path.append('../../')

import lemon
import lemon.db
import lemon.auth
import lemon.session

# Setup the databse
#conn = lemon.db.connect(type='mysql', database='test')
conn = lemon.db.connect(type='gadfly', database='Lemon', dir='../../../doc/examples/data/gadfly')
#conn = lemon.db.connect(type='odbc', database='Test')
        
# Set up the session
session = lemon.session.start(storage='db', database=conn, app='test')
# Create auth object from the session information
auth    = lemon.auth.start(session, storage='db', database=conn, autoRedirect=False, idle=10)
# NOTE for use with the webserver included with Lemon we set autoRedirect=False.
#      becuase there is a problem preventing the redirect from working. 
#      This should normally be True.

# WARNING No HTTP content-type headers should be printed before the auth.isValid() method if the autoLogin
# feature is enabled as this will print its own header. 

if auth.isValid() > 0:                                  # See if the User is logged on else present login form.
    print lemon.getHTTPHeader('text/html') 
    if lemon.cgi.has_key('logout'):
        auth.logout()
        print '<html><h1>Logged Out Now</h1><p><a href="auth.py">Login again</a>.</p></html>'
    else:
        username = auth.getUser()['username']           # Return a user object containing user's details.
                                                        # and access the data via attributes or dict style.
        accessStatus = auth.getStatus()                 # Retrieve the entry for the appName column.
        
        print """<html>
                    <h1>Welcome - You Logged In</h1>
                        <p> Visiting this the auth.py script again will result in you seeing this page until 
                            you logout.</p>
                        <p> <b>Some Variables:</b><br>
                        
                            Username:       %s<br>
                            Access Level:   %s<br>
                        </p>
                        <p>
                            <a href="%s">Visit auth.py again</a>. <a href="%s?logout=True">Logout</a>.
                        </p>
                </html>"""%(username, accessStatus, os.environ['SCRIPT_NAME'], os.environ['SCRIPT_NAME'])

© 2002-2003 James Gardner