1
0
mirror of https://github.com/MinetestForFun/mysql_auth.git synced 2025-02-08 07:40:24 +01:00

Add automatic auth.txt import upon auth table creation

This commit is contained in:
Dorian Wouters 2016-08-23 16:58:36 +02:00
parent 4a322bb91d
commit e0f24649ad
No known key found for this signature in database
GPG Key ID: 6E9DA8063322434B
2 changed files with 56 additions and 1 deletions

40
auth_txt_import.lua Normal file
View File

@ -0,0 +1,40 @@
local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
local thismod = _G[modname]
function thismod.import_auth_txt()
local auth_file_path = minetest.get_worldpath() .. '/auth.txt'
local create_auth_stmt = thismod.create_auth_stmt
local create_auth_params = thismod.create_auth_params
local file, errmsg = io.open(auth_file_path, 'rb')
if not file then
minetest.log("info", modname .. ": " .. auth_file_path .. " could not be opened for reading" ..
"(" .. errmsg .. "); no auth entries imported")
return
end
for line in file:lines() do
if line ~= "" then
local fields = line:split(":", true)
local name, password, privilege_string, last_login = unpack(fields)
last_login = tonumber(last_login)
if not (name and password and privilege_string) then
minetest.log('warning', modname .. ": Invalid line in auth.txt, skipped: " .. dump(line))
end
minetest.log('info', modname .. " importing player '"..name.."'")
create_auth_params:set(1, name)
create_auth_params:set(2, password)
create_auth_params:set(3, privilege_string)
create_auth_params:set(4, last_login)
local success, msg = pcall(create_auth_stmt.exec, create_auth_stmt)
if not success then
error(modname .. ": import failed: " .. msg)
end
if create_auth_stmt:affected_rows() ~= 1 then
error(modname .. ": create_auth failed: affected row count is " ..
create_auth_stmt:affected_rows() .. ", expected 1")
end
end
end
io.close(file)
end

View File

@ -88,6 +88,9 @@ do
return nil return nil
end end
local parts = string_splitdots(name) local parts = string_splitdots(name)
if not parts then
return cfg[name]
end
local tbl = cfg[parts[1]] local tbl = cfg[parts[1]]
for n = 2, #parts do for n = 2, #parts do
if tbl == nil then if tbl == nil then
@ -100,7 +103,7 @@ do
end end
end end
local conn local conn, dbname
do do
-- MySQL API backend -- MySQL API backend
mysql.config(get('db.api')) mysql.config(get('db.api'))
@ -119,6 +122,7 @@ do
connopts.options = connopts.options or {} connopts.options = connopts.options or {}
connopts.options.MYSQL_OPT_RECONNECT = true connopts.options.MYSQL_OPT_RECONNECT = true
conn = mysql.connect(connopts) conn = mysql.connect(connopts)
dbname = connopts.db
thismod.conn = conn thismod.conn = conn
-- LuaPower's MySQL interface throws an error when the connection fails, no need to check if -- LuaPower's MySQL interface throws an error when the connection fails, no need to check if
@ -180,6 +184,7 @@ do
end end
end end
local auth_table_created
do -- Auth table existence check and setup do -- Auth table existence check and setup
conn:query("SHOW TABLES LIKE '" .. tables.auths.name .. "'") conn:query("SHOW TABLES LIKE '" .. tables.auths.name .. "'")
local res = conn:store_result() local res = conn:store_result()
@ -197,6 +202,9 @@ do
'PRIMARY KEY (' .. S.userid .. '),' .. 'PRIMARY KEY (' .. S.userid .. '),' ..
'UNIQUE (' .. S.username .. ')' .. 'UNIQUE (' .. S.username .. ')' ..
')') ')')
minetest.log('action', modname .. " created table '" .. dbname .. "." .. tables.auths.name ..
"'")
auth_table_created = true
end end
end end
@ -245,6 +253,13 @@ do
',' .. S.lastlogin .. ' FROM ' .. tables.auths.name ',' .. S.lastlogin .. ' FROM ' .. tables.auths.name
thismod.enumerate_auths_query = enumerate_auths_query thismod.enumerate_auths_query = enumerate_auths_query
if auth_table_created and get('import_auth_txt_on_table_create') ~= 'false' then
if not thismod.import_auth_txt then
dofile(modpath .. '/auth_txt_import.lua')
end
thismod.import_auth_txt()
end
thismod.auth_handler = { thismod.auth_handler = {
get_auth = function(name) get_auth = function(name)
assert(type(name) == 'string') assert(type(name) == 'string')