Advertisement
Guest User

Untitled

a guest
May 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. local ADMIN_GROUP = "Admin"
  2. local MAX_PASSWORD_CHARS = 30
  3. local g_console = getElementsByType"console"[1]
  4. local g_inWizard,g_wizardCR,g_pattern,g_inputIgnore
  5.  
  6. function aStartWizard ()
  7.     --Check if they have any admins
  8.     for i,object in ipairs(aclGroupListObjects ( aclGetGroup(ADMIN_GROUP) )) do
  9.         if string.find(object,"user.") == 1 then
  10.             return
  11.         end
  12.     end
  13.     outputServerLog ( "-> It appears your server lacks any Administrators." )
  14.     outputServerLog ( "-> An Administrator is required in order to manage a server.  Please use the command 'adminwizard' to setup an admin." )
  15. end
  16.  
  17. addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), aStartWizard);
  18.  
  19. local function input(message)
  20.     if message == "adminwizard" then return end
  21.     if not g_pattern then return end
  22.     -- outputServerLog ( " "..tostring(message) )
  23.     if g_pattern(message) then
  24.         g_wizardCR(false)
  25.         return
  26.     end
  27.     g_wizardCR(message)
  28.     -- outputServerLog ( "|"..tostring( coroutine.resume(g_wizardCR,message) ) )
  29. end
  30.  
  31.  
  32. local wizard = {}
  33. wizard[1] = function()
  34.         outputServerLog ( "-> Please enter the default USERNAME for the account.  This may be any alphanumerical name." )
  35.         g_pattern = function(msg) return string.find(msg,"%W") end
  36.         local message = coroutine.yield()
  37.         g_pattern = nil
  38.         -- outputServerLog ( "message: "..tostring(message) )
  39.         if not message then
  40.             outputServerLog ( "-> The username was invalid." )
  41.             g_wizardCR = coroutine.wrap(wizard[1])
  42.             g_wizardCR()
  43.             return
  44.         end
  45.         -- outputServerLog ( "-> Here "..tostring(wizard[2]) )
  46.         wizard.username = message
  47.         g_wizardCR = coroutine.wrap(wizard[2])
  48.         g_wizardCR()
  49.     end
  50. wizard[2] = function()
  51.         outputServerLog ( "-> Please enter the default PASSWORD for the account.  This may be up to "..MAX_PASSWORD_CHARS.." characters in length." )
  52.         g_pattern = function(msg) return #msg > MAX_PASSWORD_CHARS end
  53.         -- addEventHandler ( "onConsole", g_console, input )
  54.         local message = coroutine.yield()
  55.         g_pattern = nil
  56.         -- removeEventHandler ( "onConsole", g_console, input )
  57.         if not message then
  58.             outputServerLog ( "-> The password was invalid." )
  59.             g_wizardCR = coroutine.wrap(wizard[2])
  60.             g_wizardCR()
  61.             return
  62.         end
  63.         wizard.password = message
  64.         g_wizardCR = coroutine.wrap(wizard[3])
  65.         g_wizardCR()
  66.     end
  67. wizard[3] = function()
  68.         outputServerLog ( "-> You have chosen to create an admin account with username '"..wizard.username.."' and password '"..wizard.password.."'.  Is this correct? (y/n)" )
  69.         g_pattern = function(msg)
  70.             if msg == "y" or msg == "yes" then
  71.                 return false
  72.             end
  73.             return true
  74.         end
  75.         local message = coroutine.yield()
  76.         g_pattern = nil
  77.         -- outputServerLog ( " -> debug " .. tostring(message) )
  78.         if message then
  79.             if addAccount ( wizard.username, wizard.password ) then
  80.                 outputServerLog ( "-> The admin account has been added successfully.  Please use the 'login' command ingame to log in.  Syntax: login <username> <password>" )
  81.                 g_wizardCR = coroutine.wrap(wizard[4])
  82.                 g_wizardCR()
  83.                 return
  84.                 -- now add the acl for it.
  85.                 -- local adminAclGroup = aclGetGroup(ADMIN_GROUP)
  86.                 -- aclGroupAddObject(adminAclGroup, "user." .. wizard.username)
  87.                 -- aclSave()
  88.                 -- aclReload()
  89.                 -- outputServerLog ( "-> Your account is directly added to the admin acl group." )
  90.                 -- outputServerLog ( "-> You can use this wizard again anytime by typing 'adminwizard' in the server console." )
  91.             else
  92.                 outputServerLog ( "-> The account could not be created.  It may exist already.  Please type 'adminwizard' to run the wizard again." )
  93.             end
  94.         else
  95.             outputServerLog ( "-> Please type 'adminwizard' to run the wizard again." )
  96.         end
  97.         -- Remove also the event handler for onConsole. Why, if we put adminwizard again, then we get an error. We don't want that.
  98.         removeEventHandler ( "onConsole", g_console, input )
  99.         g_wizardCR,g_inWizard = nil,nil
  100.     end
  101. wizard[4] = function()
  102.     outputServerLog ( "-> Do you want to make this account an admin account? (y for yes i will, n for no i don't)" )
  103.     g_pattern = function(msg)
  104.         if msg == "y" or msg == "yes" then
  105.             return false
  106.         end
  107.         return true
  108.     end
  109.     local message = coroutine.yield()
  110.     g_pattern = nil
  111.     if message then
  112.         local adminAclGroup = aclGetGroup(ADMIN_GROUP)
  113.         aclGroupAddObject(adminAclGroup, "user." .. wizard.username)
  114.         aclSave()
  115.         aclReload()
  116.         outputServerLog ( "-> The account is now an admin account!" )
  117.     end
  118.     outputServerLog ( "-> Please type 'adminwizard' to run the wizard again." )
  119.     removeEventHandler ( "onConsole", g_console, input )
  120.     g_wizardCR, g_inWizard = nil, nil
  121. end
  122.  
  123.  
  124. addCommandHandler ( "adminwizard",
  125.     function(source)
  126.         if source ~= g_console then return end
  127.         if g_inWizard then
  128.             outputServerLog ( "-> You are already in an admin wizard." )
  129.             return
  130.         end
  131.         addEventHandler ( "onConsole", g_console, input )
  132.         g_wizardCR = coroutine.wrap(wizard[1])
  133.         g_wizardCR()
  134.     end
  135. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement