Advertisement
Guest User

Untitled

a guest
May 5th, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1. --------------------
  2. --     Config     --
  3. --------------------
  4. local host = "localhost"
  5. local username = "secretuser"
  6. local password = "secretpass"
  7. local database = "ulyssesmod_forums"
  8. local port = 3306
  9. local prefix = "smf2_" -- table prefix
  10.  
  11. local grants =
  12. {
  13.    [9] = "superadmin",
  14.    [10] = "donator",
  15.    [2] = "superadmin",
  16.    [11] = "donatoradmin",
  17.    [13] = "admin",
  18. }
  19.  
  20. local persistent = false -- Use a persistent MySQL connection?
  21.  
  22. --[[
  23. Table structure:
  24. <prefix>themes
  25. ID_MEMBER, ID_THEME, variable, value (1, 1, steamid, STEAM_****)
  26.  
  27. <prefix>members
  28. ID_MEMBER, memberName, ID_GROUP (1, megiddo, 9)
  29. 9 = developer
  30. 10 = donator
  31. 2 = global mod
  32. ]]--
  33.  
  34. require( "mysql" )
  35.  
  36. module( "forumusers", package.seeall )
  37.  
  38. local db
  39.  
  40. function DoQuery( query, type )
  41.    local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )
  42.  
  43.    if not isok and err == "" then isok = true end -- False positive
  44.  
  45.    if not isok then
  46.       error( tostring( err ), 2 )
  47.       return nil
  48.    end
  49.  
  50.    if result then
  51.       -- print( query ) -- For debug
  52.       -- PrintTable( result )
  53.    end
  54.  
  55.    return result
  56. end
  57.  
  58. function Connect()
  59.    if db then return db end -- Still connected
  60.  
  61.    db, err = mysql.connect( host, username, password, database, port )
  62.    if db == 0 then
  63.       db = nil
  64.       error( tostring( err ), 1 )
  65.       return
  66.    end
  67.  
  68.    return db
  69. end
  70.  
  71. function Disconnect( force )
  72.    if not db then return end -- Already disconnected
  73.    if persistent and not force then return end -- Don't disconnect, persistent
  74.  
  75.    local succ, err = mysql.disconnect( db )
  76.    if not succ then
  77.       error( tostring( err ), 2 )
  78.    end
  79.    
  80.    db = nil
  81. end
  82. hook.Add( "ShutDown", "ForumUsersClose", function() Disconnect( true ) end ) -- Force closed on shutdown.
  83.  
  84. function Escape( str )
  85.    if not db then
  86.       Msg( "Not connected to DB.\n" )
  87.       return
  88.    end
  89.    
  90.    if not str then return end
  91.  
  92.    local esc, err = mysql.escape( db, str )
  93.    if not esc then
  94.       error( tostring( err ), 2 )
  95.       return nil
  96.    end
  97.  
  98.    -- print( "esc=" .. esc ) -- For debug
  99.    return esc
  100. end
  101.  
  102. -- Check a user
  103. function CheckUser( ply )
  104.    Connect()
  105.  
  106.    ULib.ucl.authed[ ply ] = nil
  107.    ULib.ucl.awaitingauth[ ply ] = nil
  108.  
  109.    local results = DoQuery( "SELECT ID_MEMBER FROM " .. prefix .. "themes WHERE value=\"" .. ply:SteamID() .. "\"" ) -- Get memberids with this steamid
  110.    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?
  111.       local uid = results[ 1 ][ 1 ] -- Member ID
  112.  
  113.       local results = DoQuery( "SELECT memberName, ID_GROUP FROM " .. prefix .. "members WHERE ID_MEMBER=" .. uid ) -- Get name and group
  114.       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
  115.          local name = results[ 1 ][ 1 ]
  116.          local group = grants[ tonumber( results[ 1 ][ 2 ] ) ]
  117.          
  118.          ULib.ucl.addUser( name, "steamid", ply:SteamID(), { group } )
  119.       end
  120.    end
  121.    
  122.    ULib.ucl.probe( ply ) -- If they weren't added above add them now
  123.  
  124.    Disconnect()
  125. end
  126. hook.Add( "PlayerInitialSpawn", "ForumUsersInitSpawn", CheckUser )
  127. hook.Remove( "PlayerInitialSpawn", "ucl_initialspawn" ) -- We'll handle calling ucl.probe ourself
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement