ULX

Author Topic: Mysql  (Read 13309 times)

0 Members and 1 Guest are viewing this topic.

Offline ben build

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Mysql
« 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.

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Mysql
« Reply #1 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.
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.
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Mysql
« Reply #2 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. :)
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Mysql
« Reply #3 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?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Mysql
« Reply #4 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
« Last Edit: February 18, 2008, 05:39:56 PM by Megiddo »
Experiencing God's grace one day at a time.

Offline high6

  • Newbie
  • *
  • Posts: 15
  • Karma: -2
Re: Mysql
« Reply #5 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*

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Mysql
« Reply #6 on: February 18, 2008, 10:52:35 PM »
*wishes for ulx callbacks*
Callbacks for what?
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline high6

  • Newbie
  • *
  • Posts: 15
  • Karma: -2
Re: Mysql
« Reply #7 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.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Mysql
« Reply #8 on: February 18, 2008, 11:32:53 PM »
Why do you not just add them yourself?

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Mysql
« Reply #9 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.
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline high6

  • Newbie
  • *
  • Posts: 15
  • Karma: -2
Re: Mysql
« Reply #10 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.

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Mysql
« Reply #11 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.
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline high6

  • Newbie
  • *
  • Posts: 15
  • Karma: -2
Re: Mysql
« Reply #12 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.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Mysql
« Reply #13 on: April 08, 2010, 05:55:11 PM »
*wishes for ulx callbacks*

Incidentally, after two years, this request got filled! See here.
Experiencing God's grace one day at a time.

Offline Frrz

  • Jr. Member
  • **
  • Posts: 65
  • Karma: 0
    • Youtube ftw
Re: Mysql
« Reply #14 on: April 09, 2010, 01:07:44 PM »
You bumped a two year old thread?