From: Matthijs Kooijman Date: Fri, 2 Jul 2010 14:52:25 +0000 (+0200) Subject: phpbb: Restructure to make PhpbbAuth and PhpbbGroupsBackend more equal. X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=ec774f80e9cbc1efdc32e2bbc7d7c50978e5fe70;p=matthijs%2Fprojects%2Fwipi.git phpbb: Restructure to make PhpbbAuth and PhpbbGroupsBackend more equal. Previously, you would instantiate the PhpbbAuth class and the groups backend constructor would be a member of that. Now, there is a setup function that returns an instantiated PhpbbAuth object and a PhpbbGroupsBackend constructor. --- diff --git a/conf/auth/phpbb.py b/conf/auth/phpbb.py index 5185eb3..0558d53 100644 --- a/conf/auth/phpbb.py +++ b/conf/auth/phpbb.py @@ -15,6 +15,41 @@ from MoinMoin import log logging = log.getLogger(__name__) +def connect(dbhost=None, dbport=None, dbname=None, dbuser=None, dbpass=None, **kwargs): + # This code was shamelessly stolen from + # django.db.backends.mysql.base.cursor + kwargs = { + 'charset': 'utf8', + 'use_unicode': False, + } + if dbuser: + kwargs['user'] = dbuser + if dbname: + kwargs['db'] = dbname + if dbpass: + kwargs['passwd'] = dbpass + if dbhost.startswith('/'): + kwargs['unix_socket'] = dbhost + elif dbhost: + kwargs['host'] = dbhost + if dbport: + kwargs['port'] = int(dbport) + + # End stolen code + + try: + conn = MySQLdb.connect (**kwargs) + except: + import sys + import traceback + info = sys.exc_info() + logging.error("phpbb: authentication failed due to exception connecting to DB, traceback follows...") + logging.error(''.join(traceback.format_exception(*info))) + return False + + return conn + + class PhpbbGroupsBackend(LazyGroupsBackend): class PhpbbGroup(LazyGroup): """ @@ -22,11 +57,11 @@ class PhpbbGroupsBackend(LazyGroupsBackend): """ pass - def __init__(self, request, auth): + def __init__(self, request, **kwargs): super(LazyGroupsBackend, self).__init__(request) - self.auth = auth self.request = request + self.dbconfig = kwargs def __iter__(self): """ @@ -35,7 +70,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): return self.list_query("SELECT group_name \ FROM `%sgroups` \ WHERE group_single_user = 0" - % self.auth.phpbb_prefix) + % self.dbconfig['phpbb_prefix']) def __contains__(self, group_name): """ @@ -45,7 +80,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): SELECT * \ FROM `%sgroups` \ WHERE group_single_user = 0 \ - AND group_name=%%s)" % self.auth.phpbb_prefix, + AND group_name=%%s)" % self.dbconfig['phpbb_prefix'], group_name) def __getitem__(self, group_name): @@ -64,7 +99,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): WHERE u.user_id = ug.user_id AND ug.group_id = g.group_id \ AND ug.user_pending = 0 AND g.group_single_user = 0 \ AND g.group_name = %%s" - % (self.auth.phpbb_prefix, self.auth.phpbb_prefix, self.auth.phpbb_prefix), + % (self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix']), group_name) def _group_has_member(self, group_name, member): @@ -78,7 +113,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): WHERE u.user_id = ug.user_id AND ug.group_id = g.group_id \ AND ug.user_pending = 0 AND g.group_single_user = 0 \ AND g.group_name = %%s AND u.username = %%s)" - % (self.auth.phpbb_prefix, self.auth.phpbb_prefix, self.auth.phpbb_prefix), + % (self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix']), (group_name, member)) def groups_with_member(self, member): @@ -91,7 +126,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): WHERE u.user_id = ug.user_id AND ug.group_id = g.group_id \ AND ug.user_pending = 0 AND g.group_single_user = 0 \ AND u.username = %%s" - % (self.auth.phpbb_prefix, self.auth.phpbb_prefix, self.auth.phpbb_prefix), + % (self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix'], self.dbconfig['phpbb_prefix']), member) def single_query(self, *args): @@ -102,7 +137,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): conn = None cursor = None try: - conn = self.auth.connect(self.request) + conn = connect(**self.dbconfig) cursor = conn.cursor() cursor.execute(*args) @@ -121,7 +156,7 @@ class PhpbbGroupsBackend(LazyGroupsBackend): conn = None cursor = None try: - conn = self.auth.connect(self.request) + conn = connect(**self.dbconfig) cursor = conn.cursor() cursor.execute(*args) @@ -137,7 +172,7 @@ class PhpbbAuth(BaseAuth): logout_possible = True login_inputs = ['username', 'password'] - def __init__(self, name='phpbb', dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, phpbb_prefix='', hint=None): + def __init__(self, name='phpbb', hint=None, **kwargs): """ Authenticate using credentials from a phpbb database @@ -145,24 +180,15 @@ class PhpbbAuth(BaseAuth): The hint parameter is a snippet of HTML that is displayed below the login form. """ - self.dbhost = dbhost - self.dbuser = dbuser - self.dbpass = dbpass - self.dbname = dbname - self.dbport = dbport - self.phpbb_prefix = phpbb_prefix + self.dbconfig = kwargs self.name = name self.hint = hint - # Create a "constructor" to create a phpbb_groups instance, while - # passing ourselves to it. - self.groups_backend = lambda config, request: PhpbbGroupsBackend(request, self) - def check_login(self, request, username, password): """ Checks the given username password combination. Returns the corresponding emailaddress, or False if authentication failed. """ - conn = self.connect(request) + conn = connect(**self.dbconfig) if not conn: return False @@ -173,7 +199,7 @@ class PhpbbAuth(BaseAuth): # through the phpbb_prefix variable, but that should be a trusted # value anyway. cursor = conn.cursor () - cursor.execute ("SELECT user_password,user_email FROM `%susers` WHERE username=%%s" % self.phpbb_prefix, username) + cursor.execute ("SELECT user_password,user_email FROM `%susers` WHERE username=%%s" % self.dbconfig['phpbb_prefix'], username) # No data? No login. if (cursor.rowcount == 0): @@ -189,40 +215,6 @@ class PhpbbAuth(BaseAuth): else: return False - def connect(self, request): - # This code was shamelessly stolen from - # django.db.backends.mysql.base.cursor - kwargs = { - 'charset': 'utf8', - 'use_unicode': False, - } - if self.dbuser: - kwargs['user'] = self.dbuser - if self.dbname: - kwargs['db'] = self.dbname - if self.dbpass: - kwargs['passwd'] = self.dbpass - if self.dbhost.startswith('/'): - kwargs['unix_socket'] = self.dbhost - elif self.dbhost: - kwargs['host'] = self.dbhost - if self.dbport: - kwargs['port'] = int(self.dbport) - - # End stolen code - - try: - conn = MySQLdb.connect (**kwargs) - except: - import sys - import traceback - info = sys.exc_info() - logging.error("phpbb_login: authentication failed due to exception connecting to DB, traceback follows...") - logging.error(''.join(traceback.format_exception(*info))) - return False - - return conn - def login(self, request, user_obj, **kw): """ Handle a login. Called by moinmoin. @@ -269,4 +261,41 @@ class PhpbbAuth(BaseAuth): """ Return a snippet of HTML that is displayed with the login form. """ return self.hint +def setup(**kwargs): + """ + Setup the phpbb backend. Takes the following keyword arguments: + dbhost -- The database server host + dbport -- The database server portname + dbname -- The database name + dbuser -- The username to log in + dbpass -- The password to log in + phpbb_prefix -- The table name prefix used for this phpbb installation + name -- The name to use for the auth backend + hint -- A hint to show in the login interface (HTML string) + + This function can be called multiple times to create backends for + different phpbb installations + + Returns a tuple (auth, groups) containing an (instantiated) auth backend + and a groups backend (constructor). These can be put directly into the + "auth" (as part of the list) and "groups" (directly) config directives. + + e.g., + + class FarmConfig: + (phpbb_auth, phpbb_groups) = phpbb.setup(...) + auth = [phpbb_auth] + groups = phpbb_groups + + (actual configuration parameters to setup() are omitted in this example) + """ + + # Create a "constructor" to create a phpbb_groups instance, while + # passing ourselves to it. + groups = lambda config, request: PhpbbGroupsBackend(request, **kwargs) + # Create an instantiated auth backend. + auth = PhpbbAuth(**kwargs) + + return (auth, groups) + # vim: set sw=4 expandtab sts=4:vim diff --git a/conf/farmconfig.py b/conf/farmconfig.py index 5d3e8a3..c17bb47 100644 --- a/conf/farmconfig.py +++ b/conf/farmconfig.py @@ -165,19 +165,19 @@ class FarmConfig(DefaultConfig): require_comment = True # Authentication - from auth.phpbb import PhpbbAuth # This comes from plugin - from dbsettings import phpbb_dbhost, phpbb_dbuser, phpbb_dbpass, phpbb_dbname, phpbb_prefix - phpbb = PhpbbAuth( + import auth.phpbb + import dbsettings + (phpbb_auth, phpbb_groups) = auth.phpbb.setup( name = 'phpbb', - dbhost = phpbb_dbhost, - dbuser = phpbb_dbuser, - dbpass = phpbb_dbpass, - dbname = phpbb_dbname, - phpbb_prefix = phpbb_prefix, + dbhost = dbsettings.phpbb_dbhost, + dbuser = dbsettings.phpbb_dbuser, + dbpass = dbsettings.phpbb_dbpass, + dbname = dbsettings.phpbb_dbname, + phpbb_prefix = dbsettings.phpbb_prefix, hint = "Hier kunnen bestuursleden van Evolution Events inloggen om wijzigingen te maken." ) - auth = [phpbb] - groups = phpbb.groups_backend + auth = [phpbb_auth] + groups = phpbb_groups user_autocreate = True