Advertisement
Guest User

Untitled

a guest
May 12th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. --------------------
  2. --     Config     --
  3. --------------------
  4. local host = "192.168.1.160"
  5. local username = "lcaservers"
  6. local password = "3p1cl0lz"
  7. local database = "gamesv"
  8. local port = 3306
  9. --local prefix = "admin_" -- table prefix ##DEPRECIATED##
  10.  
  11. local grants =
  12. {
  13.    [0] = "user",
  14.    [1] = "respected",
  15.    [2] = "admin",
  16.    [3] = "superadmin",
  17.    --[4] = "user",
  18. }
  19.  
  20. local persistent = true -- Use a persistent MySQL connection?
  21.  
  22. --[[
  23. Table structure:
  24.  
  25. gamesv.users
  26.  id, name,  steamid,  group,    password
  27. ( 1  halon  STEAM_**   0     util.crc(passwd) )
  28.  
  29. ## DEPRECIATED ##
  30. <prefix>themes
  31. ID_MEMBER, ID_THEME, variable, value (1, 1, steamid, STEAM_****)
  32.  
  33. <prefix>members
  34. ID_MEMBER, memberName, ID_GROUP (1, halon, 1)
  35.    [0] = "undefined",
  36.    [1] = "superadmin",
  37.    [2] = "admin",
  38.    [3] = "respected",
  39.    [4] = "user",
  40. ## DEPRECIATED ##
  41.  
  42. ]]--
  43.  
  44. require( "mysql" )
  45.  
  46. module( "dbusers", package.seeall )
  47.  
  48. local db
  49.  
  50. --[[
  51.     Execute the query
  52. ]]--
  53. function DoQuery( query, type )
  54.    local result, isok, err = mysql.query( db, query, type or mysql.QUERY_NUMERIC )
  55.  
  56.    if not isok and err == "" then isok = true end -- False positive
  57.  
  58.    if not isok then
  59.       error( tostring( err ), 2 )
  60.       return nil
  61.    end
  62.  
  63.    if result then
  64.     --       DEBUGGING        --
  65.     -- print( query ) -- For debug
  66.     -- PrintTable( result )
  67.    end
  68.  
  69.    return result
  70. end
  71.  
  72. --[[
  73.     Connects to the database
  74. ]]--
  75. function Connect()
  76.    if db then return db end -- Still connected
  77.  
  78.    db, err = mysql.connect( host, username, password, database, port )
  79.    if db == 0 then
  80.       db = nil
  81.       error( tostring( err ), 1 )
  82.       return
  83.    end
  84.  
  85.    return db
  86. end
  87.  
  88. --[[
  89.     Disconnects from the database
  90. ]]--
  91. function Disconnect( force )
  92.    if not db then return end -- Already disconnected
  93.    if persistent and not force then return end -- Don't disconnect, persistent
  94.  
  95.    local succ, err = mysql.disconnect( db )
  96.    if not succ then
  97.       error( tostring( err ), 2 )
  98.    end
  99.    
  100.    db = nil
  101. end
  102. hook.Add( "ShutDown", "DbUsersClose", function() Disconnect( true ) end ) -- Force closed on shutdown.
  103.  
  104. --[[
  105.     Unused function
  106. ]]--
  107. function Escape( str )
  108.    if not db then
  109.       Msg( "Not connected to DB.\n" )
  110.       return
  111.    end
  112.    
  113.    if not str then return end
  114.  
  115.    local esc, err = mysql.escape( db, str )
  116.    if not esc then
  117.       error( tostring( err ), 2 )
  118.       return nil
  119.    end
  120.    --             DEBUGGING          --
  121.    --print( "esc=" .. esc ) -- For debug
  122.    return esc
  123. end
  124.  
  125.  
  126. --[[
  127.     Total rewrite of the old function
  128. ]]--
  129. function CheckUser( ply )
  130.     Connect()
  131.     ULib.ucl.authed[ ply ] = nil
  132.     --ULib.ucl.awaitingauth[ ply ] = true
  133.    
  134.     local results = DoQuery("SELECT * FROM users WHERE steamid='" .. ply:SteamID() .. "'")
  135.     if results[ 1 ] then
  136.         local name = results[1][2]
  137.         local group = grants[ tonumber(results[1][4]) ]
  138.         ULib.ucl.addUser(ply:SteamID(),{},{},group)
  139.         print(name .. " was added to " .. group)
  140.     end
  141.    
  142.     ULib.ucl.probe( ply ) -- If they weren't added above add them now
  143.     Disconnect()
  144. end
  145.  
  146. hook.Add( "PlayerInitialSpawn", "DbUsersInitSpawn", CheckUser )
  147. hook.Remove( "PlayerInitialSpawn", "ucl_initialspawn" ) -- We'll handle calling ucl.probe ourself
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement