Software /
code /
prosody-modules
Changeset
371:c416db434e5b
Do not run in transaction.
Code cleanup.
Changed logging to module logging.
Properly count SQL result rows.
author | Tomasz Sterna <tomek@xiaoka.com> |
---|---|
date | Tue, 26 Apr 2011 19:28:08 +0200 |
parents | 370:16da8cd69715 |
children | 372:000f1d1c6ca5 |
files | mod_auth_sql/mod_auth_sql.lua |
diffstat | 1 files changed, 24 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/mod_auth_sql/mod_auth_sql.lua Fri Apr 22 01:49:53 2011 +0000 +++ b/mod_auth_sql/mod_auth_sql.lua Tue Apr 26 19:28:08 2011 +0200 @@ -8,7 +8,6 @@ local DBI; local connection; -local host,user,store = module.host; local params = module:get_option("sql"); local resolve_relative_path = require "core.configmanager".resolve_relative_path; @@ -36,7 +35,7 @@ return nil, err; end module:log("debug", "Successfully connected to database"); - dbh:autocommit(false); -- don't commit automatically + dbh:autocommit(true); -- don't run in transaction connection = dbh; return connection; end @@ -60,7 +59,7 @@ if params.driver == "PostgreSQL" then sql = sql:gsub("`", "\""); end - if not test_connection() then connect() end + if not test_connection() then connect(); end -- do prepared statement stuff local stmt, err = connection:prepare(sql); if not stmt and not test_connection() then error("connection failed"); end @@ -75,20 +74,24 @@ function new_default_provider(host) local provider = { name = "sql" }; - log("debug", "initializing default authentication provider for host '%s'", host); + module:log("debug", "initializing default authentication provider for host '%s'", host); function provider.test_password(username, password) - log("debug", "test password '%s' for user %s at host %s", password, username, module.host); + module:log("debug", "test_password '%s' for user %s at host %s", password, username, host); local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `password`=? AND `realm`=?", - username, password, module.host); + username, password, host); if stmt ~= nil then - if #stmt:rows(true) > 0 then + local count = 0; + for row in stmt:rows(true) do + count = count + 1; + end + if count > 0 then return true; end else - log("error", "QUERY ERROR: %s %s", err, debug.traceback()); + module:log("error", "QUERY ERROR: %s %s", err, debug.traceback()); return nil, err; end @@ -96,10 +99,10 @@ end function provider.get_password(username) - log("debug", "get_password for username '%s' at host '%s'", username, module.host); + module:log("debug", "get_password for username '%s' at host '%s'", username, host); local stmt, err = getsql("SELECT `password` FROM `authreg` WHERE `username`=? AND `realm`=?", - username, module.host); + username, host); local password = nil; if stmt ~= nil then @@ -107,7 +110,7 @@ password = row.password; end else - log("error", "QUERY ERROR: %s %s", err, debug.traceback()); + module:log("error", "QUERY ERROR: %s %s", err, debug.traceback()); return nil; end @@ -119,17 +122,21 @@ end function provider.user_exists(username) - log("debug", "test user %s existence at host %s", username, module.host); + module:log("debug", "test user %s existence at host %s", username, host); local stmt, err = getsql("SELECT `username` FROM `authreg` WHERE `username`=? AND `realm`=?", - username, module.host); + username, host); if stmt ~= nil then - if #stmt:rows(true) > 0 then + local count = 0; + for row in stmt:rows(true) do + count = count + 1; + end + if count > 0 then return true; end else - log("error", "QUERY ERROR: %s %s", err, debug.traceback()); + module:log("error", "QUERY ERROR: %s %s", err, debug.traceback()); return nil, err; end @@ -141,12 +148,12 @@ end function provider.get_sasl_handler() - local realm = module:get_option("sasl_realm") or module.host; + local realm = module:get_option("sasl_realm") or host; local getpass_authentication_profile = { plain = function(sasl, username, realm) local prepped_username = nodeprep(username); if not prepped_username then - log("debug", "NODEprep failed on username: %s", username); + module:log("debug", "NODEprep failed on username: %s", username); return "", nil; end local password = usermanager.get_password(prepped_username, realm);