Ulysses

Ulysses Stuff => Suggestions => Topic started by: ben build on February 15, 2008, 05:12:59 PM

Title: Mysql
Post by: ben build on February 15, 2008, 05:12:59 PM
This is quite a big request but would it be possible to have a mysql option which makes it use mysql for users, groups and bans instead of txt

I run multiple servers and i don't want to have to write my own admins system to accommodate this, I know there is the uban system but it doesn't seem to integrate very well.
Title: Re: Mysql
Post by: spbogie on February 18, 2008, 08:21:00 AM
There is no need to write an entirely new admin mod just to get your users through SQL. You can just write a module for ULib and call ULib.ucl.addUser and ULib.ucl.addGroup. Full documentation of the ULib developer library can be found at http://www.ulyssesmod.net/docs (http://www.ulyssesmod.net/docs).
One small note on making it a module however, you should only put a small init script in lua/ULib/modules/ as it will be run on both client and server. Put all the functionality and configuration in (a) separate file(s) located elsewhere.

I do not personally have the time right now to create this myself (finals and such), and I know Megiddo and JamminR have been quite busy lately as well, but we encourage you, or anyone else, to attempt. We wrote ULib for everyone, not just us, to use. If you take up this project, we would greatly appreciate it if you would release it here. If you can't/don't want to try this, and no one else picks it up, you will have to wait until we have time.
Title: Re: Mysql
Post by: Megiddo on February 18, 2008, 12:08:21 PM
We're pulling the users for all our servers from these forums with just a few lines of code. :)
Title: Re: Mysql
Post by: JamminR on February 18, 2008, 03:18:22 PM
I personally don't know SQL, but as spbogie said, don't have time to learn either.
Megiddo, mind sharing those few lines of code for pulling from our db, minus PW and servernames/etc. Or would that open us to risk?
I've always been curious what it looks like. Might help Ben on his way.

Ben,
As for UBan, Other than it using different commands, why would it be more difficult than what you wish now?
Title: Re: Mysql
Post by: Megiddo on February 18, 2008, 05:37:24 PM
Obviously this only works for SMF forums, but maybe it will help you. The grants is based off the group IDs assigned by the forums. This is more than my previously stated few lines of code because of the MySQL wrapper, which was grabbed from UBan (and not really entirely necessary). The only function of substance is CheckUser().

Code: [Select]
--------------------
--     Config     --
--------------------
local host = "localhost"
local username = "secretuser"
local password = "secretpass"
local database = "ulyssesmod_forums"
local port = 3306
local prefix = "smf2_" -- table prefix

local grants =
{
[9] = "superadmin",
[10] = "donator",
[2] = "superadmin",
[11] = "donatoradmin",
[13] = "admin",
}

local persistent = false -- Use a persistent MySQL connection?

--[[
Table structure:
<prefix>themes
ID_MEMBER, ID_THEME, variable, value (1, 1, steamid, STEAM_****)

<prefix>members
ID_MEMBER, memberName, ID_GROUP (1, megiddo, 9)
9 = developer
10 = donator
2 = global mod
]]--

require( "mysql" )

module( "forumusers", package.seeall )

local db

function DoQuery( query, type )
local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )

if not isok and err == "" then isok = true end -- False positive

if not isok then
error( tostring( err ), 2 )
return nil
end

if result then
-- print( query ) -- For debug
-- PrintTable( result )
end

return result
end

function Connect()
if db then return db end -- Still connected

db, err = mysql.connect( host, username, password, database, port )
if db == 0 then
db = nil
error( tostring( err ), 1 )
return
end

return db
end

function Disconnect( force )
if not db then return end -- Already disconnected
if persistent and not force then return end -- Don't disconnect, persistent

local succ, err = mysql.disconnect( db )
if not succ then
error( tostring( err ), 2 )
end

db = nil
end
hook.Add( "ShutDown", "ForumUsersClose", function() Disconnect( true ) end ) -- Force closed on shutdown.

function Escape( str )
if not db then
Msg( "Not connected to DB.\n" )
return
end

if not str then return end

local esc, err = mysql.escape( db, str )
if not esc then
error( tostring( err ), 2 )
return nil
end

-- print( "esc=" .. esc ) -- For debug
return esc
end

-- Check a user
function CheckUser( ply )
Connect()

ULib.ucl.authed[ ply ] = nil
ULib.ucl.awaitingauth[ ply ] = nil

local results = DoQuery( "SELECT ID_MEMBER FROM " .. prefix .. "themes WHERE value=\"" .. ply:SteamID() .. "\"" ) -- Get memberids with this steamid
if results[ 1 ] then -- Get into the row. I realize we're not handling cases where multiple users have the same steamid but meh. Should never happen, right?
local uid = results[ 1 ][ 1 ] -- Member ID

local results = DoQuery( "SELECT memberName, ID_GROUP FROM " .. prefix .. "members WHERE ID_MEMBER=" .. uid ) -- Get name and group
if results[ 1 ] and grants[ tonumber( results[ 1 ][ 2 ] ) ] then -- Should always exist but might as always make sure. Also make sure they have a permission
local name = results[ 1 ][ 1 ]
local group = grants[ tonumber( results[ 1 ][ 2 ] ) ]

ULib.ucl.addUser( name, "steamid", ply:SteamID(), { group } )
end
end

ULib.ucl.probe( ply ) -- If they weren't added above add them now

Disconnect()
end
hook.Add( "PlayerInitialSpawn", "ForumUsersInitSpawn", CheckUser )
hook.Remove( "PlayerInitialSpawn", "ucl_initialspawn" ) -- We'll handle calling ucl.probe ourself
Title: Re: Mysql
Post by: high6 on February 18, 2008, 10:03:20 PM
I am actually working on making ulx mostly sql for my friends server. I will make it so it is easy to integrate with new version.

*wishes for ulx callbacks*
Title: Re: Mysql
Post by: spbogie on February 18, 2008, 10:52:35 PM
*wishes for ulx callbacks*
Callbacks for what?
Title: Re: Mysql
Post by: high6 on February 18, 2008, 10:58:19 PM
Callbacks for what?

like

Code: [Select]
ulx.AddBanFunction(func_here)

function func_here()
//called when a person is banned.
end

so other mods can be integrated into ulx really easy and hook its bannings.
Title: Re: Mysql
Post by: MrPresident on February 18, 2008, 11:32:53 PM
Why do you not just add them yourself?
Title: Re: Mysql
Post by: spbogie on February 19, 2008, 07:57:25 AM
For your purpose you would want to overried the functionality anyways. If you want everything to be over on SQL then it is probably best to override the underlying ULib functions instead, directing all bans done through ulib to your SQL server, and leaving ULX unmodified.
Title: Re: Mysql
Post by: high6 on February 19, 2008, 10:19:27 AM
ok well I will try to get this done soon as possible. Problem is though that mysql broke for the latest gmod. So either I have to wait or attempt to rebuild it myself... Never was good with gmod dlls.
Title: Re: Mysql
Post by: spbogie on February 19, 2008, 11:06:15 AM
dlls will no longer work from the addons directory, other than that, the sql module should work fine. You just need to move it out of addons and put it in the main /lua/includes/modules.
Title: Re: Mysql
Post by: high6 on February 19, 2008, 11:14:47 AM
ok well I am trying to figure out whats wrong with it, hopefully i get it working soon. :D.
Title: Re: Mysql
Post by: Megiddo on April 08, 2010, 05:55:11 PM
*wishes for ulx callbacks*

Incidentally, after two years, this request got filled! See here (http://ulyssesmod.net/docs/files/lua/ULib/shared/defines-lua.html#ULibCommandCalled).
Title: Re: Mysql
Post by: Frrz on April 09, 2010, 01:07:44 PM
You bumped a two year old thread?
Title: Re: Mysql
Post by: JamminR on April 09, 2010, 02:43:53 PM
/me considers a "why reply" ban... then realizes this isn't facepunch.
Title: Re: Mysql
Post by: Megiddo on April 09, 2010, 03:55:04 PM
You bumped a two year old thread?

It's a perk of being an admin, I can bump threads with impunity. :)
Title: Re: Mysql
Post by: Pantho on April 09, 2010, 05:55:19 PM
Obviously this only works for SMF forums, but maybe it will help you. The grants is based off the group IDs assigned by the forums. This is more than my previously stated few lines of code because of the MySQL wrapper, which was grabbed from UBan (and not really entirely necessary). The only function of substance is CheckUser().

<JamminR-Edited out re-paste of code block>

Hmm I used a SMF forum already, and already set usergroups for admins in SMF.

Does this code still work? would be kinda intresting to use :)
Title: Re: Mysql
Post by: JamminR on April 09, 2010, 06:35:51 PM
Pantho, both gmod and SMF have changed since we used the code.
Theoretically, yes it should still work, most likely needing some tweaking.
Requires sql plugins for gmod, and an SMF plugin for custom fields (which store a SMF user's steamid)
Title: Re: Mysql
Post by: Pantho on April 09, 2010, 07:45:01 PM
Pantho, both gmod and SMF have changed since we used the code.
Theoretically, yes it should still work, most likely needing some tweaking.
Requires sql plugins for gmod, and an SMF plugin for custom fields (which store a SMF user's steamid)

I've already got custom tables in my SMF Database I used for my CSS versions.

I can edit the file enough to correct for changes SQL databases etc, plus I used SMF 2 RCx (I forget which). I completely suck at LUA but I will check into it tomorrow :)
Title: Re: Mysql
Post by: JamminR on April 09, 2010, 09:23:23 PM
I completely suck at LUA but I will check into it tomorrow :)

I don't know SQL, but Lua is one of the easiest scripting languages I've ever tinkered with.
I forget that the code also uses ULib queries, which have been, not just changed, overhauled.
If/As you find/get errors, post the code (minus your login info(and server info if you don't want to even share that) and the errors you get in Developers corner. Between all the geeks around here, we should be able to help.
(Geek ~= bad)