| 1 |
from cherrypy import cpg |
|---|
| 2 |
|
|---|
| 3 |
class Login: |
|---|
| 4 |
def check(cls, fn): |
|---|
| 5 |
def _check(self, *args, **kwargs): |
|---|
| 6 |
if cpg.request.sessionMap.has_key('userid'): |
|---|
| 7 |
|
|---|
| 8 |
return fn(self, *args, **kwargs) |
|---|
| 9 |
else: |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
try: |
|---|
| 14 |
submit = kwargs['login'] |
|---|
| 15 |
email = kwargs['loginEmail'] |
|---|
| 16 |
password = kwargs['loginPassword'] |
|---|
| 17 |
except KeyError: |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
return self.loginPage(cpg.request.path) |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
userid = self.getUserId(email, password) |
|---|
| 24 |
if userid is None: |
|---|
| 25 |
|
|---|
| 26 |
return self.loginPage(cpg.request.path, 'Invalid email address or password.') |
|---|
| 27 |
|
|---|
| 28 |
cpg.request.sessionMap['userid'] = userid |
|---|
| 29 |
return fn(self, *args, **kwargs) |
|---|
| 30 |
return _check |
|---|
| 31 |
check = classmethod(check) |
|---|
| 32 |
|
|---|
| 33 |
def getUserId(self, email, password): |
|---|
| 34 |
'''Simple function to look up a user id from email and password. |
|---|
| 35 |
Naturally, this would be stored in a database rather than |
|---|
| 36 |
hardcoded, and the password would be stored in a hashed format |
|---|
| 37 |
rather than in cleartext. |
|---|
| 38 |
|
|---|
| 39 |
Returns the userid on success, or None on failure. |
|---|
| 40 |
''' |
|---|
| 41 |
|
|---|
| 42 |
accounts = {('tim@lesher.ws', 'foo'): 'tim'} |
|---|
| 43 |
|
|---|
| 44 |
return accounts.get((email,password), None) |
|---|
| 45 |
|
|---|
| 46 |
def loginPage(self, targetPage, message=None): |
|---|
| 47 |
'''Return a login "pagelet" that replaces the regular content if |
|---|
| 48 |
the user is not logged in.''' |
|---|
| 49 |
result = [] |
|---|
| 50 |
result.append('<h1>Sitename Login</h1>') |
|---|
| 51 |
if message is not None: |
|---|
| 52 |
result.append('<p>%s</p>' % message) |
|---|
| 53 |
result.append('<form action=%s method=post>' % targetPage) |
|---|
| 54 |
result.append('<p>Email Address: <input type=text name="loginEmail"></p>') |
|---|
| 55 |
result.append('<p>Password: <input type=password name="loginPassword"></p>') |
|---|
| 56 |
result.append('<p><input type="submit" name="login" value="Log In"></p>') |
|---|
| 57 |
result.append('</form>') |
|---|
| 58 |
return '\n'.join(result) |
|---|
| 59 |
|
|---|
| 60 |
def logOut(self): |
|---|
| 61 |
'''Log Out.''' |
|---|
| 62 |
del cpg.request.sessionMap['userid'] |
|---|
| 63 |
return 'You are no more logged in' + self.index() |
|---|
| 64 |
logOut.exposed = True |
|---|
| 65 |
|
|---|
| 66 |
class Page(Login): |
|---|
| 67 |
def index(self): |
|---|
| 68 |
return '''<h1>SiteName</h1> |
|---|
| 69 |
<h2>Home Page</h2> |
|---|
| 70 |
<p><a href="public">Public Page</a></p> |
|---|
| 71 |
<p><a href="private">Private Page</a> <i>(registered users only)</i></p> |
|---|
| 72 |
''' |
|---|
| 73 |
index.exposed = True |
|---|
| 74 |
|
|---|
| 75 |
def public(self): |
|---|
| 76 |
return '''<h1>SiteName</h1> |
|---|
| 77 |
<h2>Public Page</h2> |
|---|
| 78 |
<p><a href="/">Go back home</a></p>''' |
|---|
| 79 |
public.exposed = True |
|---|
| 80 |
|
|---|
| 81 |
def private(self, *args, **kwargs): |
|---|
| 82 |
return '''<h1>SiteName</h1> |
|---|
| 83 |
<h2>Private Page</h2> |
|---|
| 84 |
<p><a href="logOut">Log Out</a></p> |
|---|
| 85 |
<p><a href="/">Go back home</a></p>''' |
|---|
| 86 |
private = Login.check(private) |
|---|
| 87 |
private.exposed = True |
|---|
| 88 |
|
|---|
| 89 |
if __name__ == "__main__": |
|---|
| 90 |
cpg.root = Page() |
|---|
| 91 |
cpg.server.start(configFile="server.conf") |
|---|