CherryPy Project Download

MultiAuth: pydoprovider.py

Line 
1 """
2 Author: Adam Svanberg <asvanberg@gmail.com>
3
4 Date: 2005-07-06
5
6 Decription:
7     Simple database provider class for use with the cherryauth module.
8
9 Requirements:
10     PyDO2 + Supported DBMS of choice.
11
12 Usage Example:
13
14     Using this sample Sqlite database with filename 'sample.db'
15
16     CREATE TABLE user (
17     name TEXT PRIMARY KEY,
18     password TEXT
19     );
20     CREATE TABLE role (
21     user TEXT,
22     role TEXT,
23     PRIMARY KEY (user,role)
24     );
25     INSERT INTO user VALUES('the user','the password');
26     INSERT INTO role VALUES('the user','admin');
27     INSERT INTO role VALUES('the user','guest');
28
29     a provider could be instatiated as
30
31     p = PyDOProvider('sqlite',('sample.db',), enable_roles=True, usercol='name')
32
33     note that the usercol parameter is specified, since it differs from
34     the default value.
35 """
36 from pydo import *
37
38 class PyDOProvider:
39     """
40     Provides authentication against any database supported by PyDO2.
41
42     A valid PyDO driver name must be specified along with driver
43     specific arguments (see PyDO2 manual). Optional keyword arguments
44     may be provided to specify table and column names for the database
45     containing user and role data. The use of roles is optional, and
46     must be enabled using the 'rolemode' parameter.
47
48     Accepted keyword arguments along with their default values:
49     
50     usertable <user>
51     usercol <username>
52     passcol <password>
53     enable_roles <False> (Remaining parameters are not parsed if False)
54     roletable <role>
55     rolecol <role>
56     roleusercol <user>
57         
58     """
59
60
61     def __init__(self, driver, driver_args, **kw):
62
63         # Initialize db connection.
64         self.alias = 'pydo'
65         initAlias(self.alias, driver, driver_args, pool=True)
66
67         # Setup table classes.
68         self.usertable = kw.get('usertable', 'user')
69         self.usercol = kw.get('usercol', 'user')
70         self.passcol = kw.get('passcol', 'password')
71
72         userdict = {'table':self.usertable,
73                     'fields':(Unique(self.usercol), self.passcol),
74                     'connectionAlias':self.alias,
75                     'refetch':True}
76         PyDOProvider.User = type('User', (PyDO,), userdict)
77
78         self.enable_roles = kw.get('enable_roles', False)
79         if self.enable_roles:
80             self.roletable = kw.get('roletable', 'role')
81             self.rolecol = kw.get('rolecol', 'role')
82             self.roleusercol = kw.get('roleusercol', 'user')
83
84             roledict = {'table':self.roletable,
85                         'fields':(self.rolecol, self.roleusercol),
86                         'connectionAlias':self.alias,
87                         'refetch':True}
88             PyDOProvider.Role = type('Role', (PyDO,), roledict)
89
90
91     def get_roles(self,username):
92         """Return list of roles associated with username"""
93         if self.enable_roles:
94             return [ user[self.rolecol] for user in PyDOProvider.Role.getSome(**{self.roleusercol:username}) ]
95         else:
96             return []
97
98     def authenticate(self, username, password):
99         """Return authentication status and associated roles"""
100         user = PyDOProvider.User.getUnique(**{self.usercol:username})
101         return user[self.passcol] == password, self.get_roles(username)

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages