1
0
mirror of https://github.com/MinetestForFun/mysql_auth.git synced 2025-01-08 00:50:25 +01:00

Add singleplayer & admin handling; try to fix prepared stmt bug

This commit is contained in:
Dorian Wouters 2016-08-18 11:37:25 +02:00
parent 8e68cebdaf
commit 52d07a9382
No known key found for this signature in database
GPG Key ID: 6E9DA8063322434B

View File

@ -4,7 +4,9 @@ local modpath = minetest.get_modpath(modname)
local thismod = {} local thismod = {}
_G[modname] = thismod _G[modname] = thismod
if not minetest.setting_get(modname .. '.enable_singleplayer') and minetest.is_singleplayer() then local singleplayer = minetest.is_singleplayer() -- Caching is OK since you can't open a game to
-- multiplayer unless you restart it.
if not minetest.setting_get(modname .. '.enable_singleplayer') and singleplayer then
core.log('action', modname .. ": Not adding auth handler because of singleplayer game") core.log('action', modname .. ": Not adding auth handler because of singleplayer game")
return return
end end
@ -195,26 +197,37 @@ do
local S = tables.auths.schema local S = tables.auths.schema
local get_auth_stmt = conn:prepare('SELECT ' .. S.password .. ',' .. S.privs .. ',' .. local get_auth_stmt = conn:prepare('SELECT ' .. S.password .. ',' .. S.privs .. ',' ..
S.lastlogin .. ' FROM ' .. tables.auths.name .. ' WHERE ' .. S.username .. '=?') S.lastlogin .. ' FROM ' .. tables.auths.name .. ' WHERE ' .. S.username .. '=?')
thismod.get_auth_stmt = get_auth_stmt
local get_auth_params = get_auth_stmt:bind_params({S.username_type}) local get_auth_params = get_auth_stmt:bind_params({S.username_type})
thismod.get_auth_params = get_auth_params
local get_auth_results = get_auth_stmt:bind_result({S.password_type, S.privs_type, local get_auth_results = get_auth_stmt:bind_result({S.password_type, S.privs_type,
S.lastlogin_type}) S.lastlogin_type})
thismod.get_auth_results = get_auth_results
local create_auth_stmt = conn:prepare('INSERT INTO ' .. tables.auths.name .. '(' .. S.username .. local create_auth_stmt = conn:prepare('INSERT INTO ' .. tables.auths.name .. '(' .. S.username ..
',' .. S.password .. ',' .. S.privs .. ') VALUES (?,?,?)') ',' .. S.password .. ',' .. S.privs .. ') VALUES (?,?,?)')
thismod.create_auth_stmt = create_auth_stmt
local create_auth_params = create_auth_stmt:bind_params({S.username_type, S.password_type, local create_auth_params = create_auth_stmt:bind_params({S.username_type, S.password_type,
S.privs_type}) S.privs_type})
thismod.create_auth_params = create_auth_params
local set_password_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' .. S.password .. local set_password_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' .. S.password ..
'=? WHERE ' .. S.username .. '=?') '=? WHERE ' .. S.username .. '=?')
thismod.set_password_stmt = set_password_stmt
local set_password_params = set_password_stmt:bind_params({S.password_type, S.username_type}) local set_password_params = set_password_stmt:bind_params({S.password_type, S.username_type})
thismod.set_password_params = set_password_params
local set_privileges_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' .. S.privs .. local set_privileges_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' .. S.privs ..
'=? WHERE ' .. S.username .. '=?') '=? WHERE ' .. S.username .. '=?')
thismod.set_privileges_stmt = set_privileges_stmt
local set_privileges_params = set_privileges_stmt:bind_params({S.privs_type, S.username_type}) local set_privileges_params = set_privileges_stmt:bind_params({S.privs_type, S.username_type})
thismod.set_privileges_params = set_privileges_params
local record_login_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' .. local record_login_stmt = conn:prepare('UPDATE ' .. tables.auths.name .. ' SET ' ..
S.lastlogin .. '=? WHERE ' .. S.username .. '=?') S.lastlogin .. '=? WHERE ' .. S.username .. '=?')
thismod.record_login_stmt = record_login_stmt
local record_login_params = record_login_stmt:bind_params({S.lastlogin_type, S.username_type}) local record_login_params = record_login_stmt:bind_params({S.lastlogin_type, S.username_type})
thismod.record_login_params = record_login_params
thismod.auth_handler = { thismod.auth_handler = {
get_auth = function(name) get_auth = function(name)
@ -225,16 +238,30 @@ do
minetest.log('error', modname .. ': get_auth failed: ' .. msg) minetest.log('error', modname .. ': get_auth failed: ' .. msg)
return nil return nil
end end
get_auth_stmt:store_result()
if not get_auth_stmt:fetch() then if not get_auth_stmt:fetch() then
minetest.log('error', modname .. ': get_auth failed: get_auth_stmt:fetch() returned false') minetest.log('error', modname .. ': get_auth failed: get_auth_stmt:fetch() returned false')
return nil return nil
end end
while get_auth_stmt:fetch() do end
local password, privs_str, lastlogin = get_auth_results:get(1), get_auth_results:get(2), local password, privs_str, lastlogin = get_auth_results:get(1), get_auth_results:get(2),
get_auth_results:get(3) get_auth_results:get(3)
get_auth_stmt:free_result() local admin = (name == minetest.setting_get("name"))
local privs
if singleplayer or admin then
privs = {}
-- If admin, grant all privs, if singleplayer, grant all privs w/ give_to_singleplayer
for priv, def in pairs(core.registered_privileges) do
if (singleplayer and def.give_to_singleplayer) or admin then
privs[priv] = true
end
end
else
privs = minetest.string_to_privs(privs_str)
end
return { return {
password = password, password = password,
privileges = minetest.string_to_privs(privs_str), privileges = privs,
last_login = lastlogin last_login = lastlogin
} }
end, end,
@ -250,7 +277,7 @@ do
minetest.log('error', modname .. ': create_auth failed: ' .. msg) minetest.log('error', modname .. ': create_auth failed: ' .. msg)
return false return false
end end
return true return (create_auth_stmt:affected_rows() == 1)
end, end,
set_password = function(name, password) set_password = function(name, password)
assert(type(name) == 'string') assert(type(name) == 'string')
@ -267,7 +294,7 @@ do
return false return false
end end
end end
return true return (set_password_stmt:affected_rows() == 1)
end, end,
set_privileges = function(name, privileges) set_privileges = function(name, privileges)
assert(type(name) == 'string') assert(type(name) == 'string')
@ -279,8 +306,8 @@ do
minetest.log('error', modname .. ': set_privileges failed: ' .. msg) minetest.log('error', modname .. ': set_privileges failed: ' .. msg)
return false return false
end end
minetest.notify_authentication_modified(name) --minetest.notify_authentication_modified(name)
return true return (set_privileges_stmt:affected_rows() == 1)
end, end,
reload = function() reload = function()
return true return true
@ -294,7 +321,7 @@ do
minetest.log('error', modname .. ': record_login failed: ' .. msg) minetest.log('error', modname .. ': record_login failed: ' .. msg)
return false return false
end end
return true return (record_login_stmt:affected_rows() == 1)
end end
} }
end end
@ -304,6 +331,7 @@ minetest.log('action', modname .. ": Registered auth handler")
minetest.register_on_shutdown(function() minetest.register_on_shutdown(function()
if thismod.conn then if thismod.conn then
thismod.get_auth_stmt:free_result()
thismod.conn:close() thismod.conn:close()
end end
end) end)