This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
McBessemer's profile picture

Add A Admin Account To Passcode Lock

Started by McBessemer, 14 July 2012 - 03:26 PM
McBessemer #1
Posted 14 July 2012 - 05:26 PM
While trying to add a admin account to my multiuser passcode door lock it does not look in the table Admins for the information while I ask the user to input there username and if the player is a admin then they get acess to the whole os.


local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-------------------------------------------
local function loadUsers()
  local file = fs.open("Users", "r")
  if file then
local t = {}
local line = file.readLine()
while line do -- while there's a line
local Username, pass = string.match(line, "(.+): (.+)")
if Username and pass then
t[Username] = pass
end
line = file.readLine()
end
file.close() -- close the file
return t -- return the table
end
return nil
end
-------------------------------------------
local function loadAdmins()
  local file = fs.open("Admins", "r")
  if file then
local tAdmins = {}
local line = file.readLine()
while line do -- while there's a line
local Admins, pass = string.match(line, "(.+): (.+)")
if Admins and pass then
tAdmins[Username] = pass
end
line = file.readLine()
end
file.close() -- close the file
return tAdmins -- return the table
  end
  return nil
end
-------------------------------------------
print("Loading...")
sleep(1)
print("Bessy Lock v1.2 Started")
print("Door Lock For Power Sub Station.")
print("Please enter username.")
write("Username: ")
local t = loadUsers()
Username = read()
if t[Username] ~= nil then
write("Password: ")
  pass = read("*") -- get password
  if t[Username] == pass then
print("Password Correct!")
rs.setOutput("right",true)
sleep(2)
rs.setOutput("right",false)
os.shutdown()
-----------------------------------------------------
local tAdmins = loadAdmins()
if tAdmins[Username] ~= nil then
write("Password: ")
  local pass = read("*") -- get password
if tAdmins[Username] == pass then
print("Password Correct!")
------Admin Stuff
  else
print("Password Incorrect!")
sleep(2)
os.shutdown()
end
------------------------------------------------------
else
print("Not Authorised Username!")
sleep(2)
os.shutdown()
end
else
print("Password Incorrect!")
sleep(2)
os.shutdown()
end
---------------------------------------------------------
else
print("Not Authorised Username!")
sleep(2)
os.shutdown()
end
os.pullEvent = oldPullEvent
shaakka #2
Posted 14 July 2012 - 05:59 PM

local Admins, pass = string.match(line, "(.+): (.+)")
if Admins and pass then
tAdmins[Username] = pass

should be
tAdmins[Admins] = pass
McBessemer #3
Posted 14 July 2012 - 09:48 PM

local Admins, pass = string.match(line, "(.+): (.+)")
if Admins and pass then
tAdmins[Username] = pass

should be
tAdmins[Admins] = pass

That still does not allow for the admins to login.
MysticT #4
Posted 14 July 2012 - 09:55 PM
Formating the code makes the error really easy to see:

local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-------------------------------------------
local function loadUsers()
    local file = fs.open("Users", "r")
    if file then
        local t = {}
        local line = file.readLine()
        while line do -- while there's a line
            local Username, pass = string.match(line, "(.+): (.+)")
            if Username and pass then
                t[Username] = pass
            end
            line = file.readLine()
        end
        file.close() -- close the file
        return t -- return the table
    end
    return nil
end
-------------------------------------------
local function loadAdmins()
    local file = fs.open("Admins", "r")
    if file then
        local tAdmins = {}
        local line = file.readLine()
        while line do -- while there's a line
            local Admins, pass = string.match(line, "(.+): (.+)")
            if Admins and pass then
                tAdmins[Username] = pass
            end
            line = file.readLine()
        end
        file.close() -- close the file
        return tAdmins -- return the table
    end
    return nil
end
-------------------------------------------
print("Loading...")
sleep(1)
print("Bessy Lock v1.2 Started")
print("Door Lock For Power Sub Station.")
print("Please enter username.")
write("Username: ")
local t = loadUsers()
Username = read()
if t[Username] ~= nil then
    write("Password: ")
    pass = read("*") -- get password
    if t[Username] == pass then
        print("Password Correct!")
        rs.setOutput("right",true)
        sleep(2)
        rs.setOutput("right",false)
        os.shutdown()
-----------------------------------------------------
        local tAdmins = loadAdmins()
        if tAdmins[Username] ~= nil then
            write("Password: ")
            local pass = read("*") -- get password
            if tAdmins[Username] == pass then
                print("Password Correct!")
                ------Admin Stuff
            else
                print("Password Incorrect!")
                sleep(2)
                os.shutdown()
            end
------------------------------------------------------
        else
            print("Not Authorised Username!")
            sleep(2)
            os.shutdown()
        end
    else
        print("Password Incorrect!")
        sleep(2)
        os.shutdown()
    end
---------------------------------------------------------
else
    print("Not Authorised Username!")
    sleep(2)
    os.shutdown()
end
os.pullEvent = oldPullEvent
You check for admin users only if the first username and password is correct.
McBessemer #5
Posted 14 July 2012 - 11:08 PM
Okay so now I have:

local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
side = "right"
-------------------------------------------
local function loadUsers()
    local file = fs.open("Users", "r")
    if file then
	    local t = {}
	    local line = file.readLine()
	    while line do -- while there's a line
		    local Username, pass = string.match(line, "(.+): (.+)")
		    if Username and pass then
			    t[Username] = pass
		    end
		    line = file.readLine()
	    end
	    file.close() -- close the file
	    return t -- return the table
    end
    return nil
end
-------------------------------------------
local function loadAdmins()
    local file = fs.open("Admins", "r")
    if file then
	    local tAdmins = {}
	    local line = file.readLine()
	    while line do -- while there's a line
		    local Admins, pass = string.match(line, "(.+): (.+)")
		    if Admins and pass then
			    tAdmins[Username] = pass
		    end
		    line = file.readLine()
	    end
	    file.close() -- close the file
	    return tAdmins -- return the table
    end
    return nil
end
-------------------------------------------
print("Loading...")
sleep(1)
print("Bessy Lock v1.2 Started")
print("Door Lock For Power Sub Station.")
print("Please Enter Username.")
write("Username: ")
Username = read()
-----------------------------------------------------
local t = loadUsers()
if t[Username] ~= nil then
print("Username Accepeted Please Enter Password")
write("Password: ")
pass = read("*") -- get password
if t[Username] == pass then
print("Welcome ".. Username)
rs.setOutput(side,true)
sleep(4)
rs.setOutput(side,false)
os.reboot()
-----------------------------------------------------
local tAdmins = loadAdmins()
if tAdmins[Username] ~= nil then
print("Username Accepeted Please Enter Password")
write("Password: ")
local pass = read("*") -- get password
if tAdmins[Username] == pass then
term.clear()
term.setCursorPos(1,1)
print("Password & ".. Username" Correct!")
print("Computer unlocking...")
print("Please type 'reboot' or 'shutdown' before leaving")
os.pullEvent(terminate)
end
end
-----------------------------------------------------
else
term.clear()
term.setCursorPos(1,1)
print("Password Incorrect!")
sleep(2)
os.reboot()
end
else
term.clear()
term.setCursorPos(1,1)
print("Username Incorrect!!")
sleep(2)
os.reboot()
end
os.pullEvent = oldPullEvent
But it still does not pull admins.
MysticT #6
Posted 14 July 2012 - 11:24 PM
Errr… What did you change? The problem is the same, you check for admins only if the first username and password is correct.
McBessemer #7
Posted 14 July 2012 - 11:52 PM
I moved the ends……..
But what I dont understand is If I can do

if input == pass then
rs.setOutput(side,true)
sleep(4)
rs.setOutput(side,false)
os.reboot()
end
if input == pass2 then
rs.setOutput(side,true)
sleep(4)
rs.setOutput(side,false)
os.reboot()
end
and that goes down the list till it finds the correct pass entered why wouldnt it work like it is now.
MysticT #8
Posted 14 July 2012 - 11:54 PM
You should use elseif:

if tUser[name] ~= nil then
  -- the user exists, get and check the password
elseif tAdmins[name] ~= nil then
  -- the user exists and it's an admin, get and check the password
else
  -- there's no user with that name
end
McBessemer #9
Posted 15 July 2012 - 12:43 AM
Attempt to index ? (a nil value) error after entering a password for a Admin and also It accepts any username now…..

local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
side = "right"
-------------------------------------------
local function loadUsers()
	local file = fs.open("Users", "r")
	if file then
		local tUsers = {}
		local line = file.readLine()
		while line do -- while there's a line
			local name, pass = string.match(line, "(.+): (.+)")
			if name and pass then
				tUsers[name] = pass
			end
			line = file.readLine()
		end
		file.close() -- close the file
		return tUsers -- return the table
	end
	return nil
end
-------------------------------------------
local function loadAdmins()
	local file = fs.open("Admins", "r")
	if file then
		local tAdmins = {}
		local line = file.readLine()
		while line do -- while there's a line
			local name, pass = string.match(line, "(.+): (.+)")
			if name and pass then
				tAdmins[name] = pass
			end
			line = file.readLine()
		end
		file.close() -- close the file
		return tAdmins -- return the table
	end
	return nil
end
-------------------------------------------
print("Loading...")
sleep(1)
print("Bessy Lock v1.2 Started")
print("Door Lock For Power Sub Station.")
print("Please Enter Username.")
write("Username: ")
name = read()
-----------------------------------------------------
local tUsers = loadUsers()
print("Username Accepeted Please Enter Password")
write("Password: ")
local pass = read("*") -- get password
if tUsers[name] == pass then
print("Welcome ".. name)
rs.setOutput(side,true)
sleep(4)
rs.setOutput(side,false)
os.reboot()
local tAdmins = loadAdmins()
elseif tAdmins[name] ~= nil then
print("Username Accepeted Please Enter Password")
write("Password: ")
local pass = read("*") -- get password
if tAdmins[name] == pass then
term.clear()
term.setCursorPos(1,1)
print("Password & ".. name" Correct!")
print("Computer unlocking...")
print("Please type 'reboot' or 'shutdown' before leaving")
os.pullEvent(terminate)
else
term.clear()
term.setCursorPos(1,1)
 print(.. name " Incorrect!!")
sleep(2)
os.reboot()
end
else
term.clear()
term.setCursorPos(1,1)
print("Password Incorrect!")
sleep(2)
os.reboot()
end
os.pullEvent = oldPullEvent
MysticT #10
Posted 15 July 2012 - 12:58 AM
The admin table is not loaded, because you load it after checking the username and pasword.
Also, you removed the if that checks for username, that's why it allows any username.
McBessemer #11
Posted 15 July 2012 - 01:41 AM
:P/>/> Sorry but this is just stumping me, it just does not want to work, now it just stops after a Admin name is entered or invalid.

local oldPullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
side = "right"
-------------------------------------------
local function loadUsers()
    local file = fs.open("Users", "r")
    if file then
	    local tUsers = {}
	    local line = file.readLine()
	    while line do -- while there's a line
		    local name, pass = string.match(line, "(.+): (.+)")
		    if name and pass then
			    tUsers[name] = pass
		    end
		    line = file.readLine()
	    end
	    file.close() -- close the file
	    return tUsers -- return the table
    end
    return nil
end
-------------------------------------------
local function loadAdmins()
    local file = fs.open("Admins", "r")
    if file then
	    local tAdmins = {}
	    local line = file.readLine()
	    while line do -- while there's a line
		    local name, pass = string.match(line, "(.+): (.+)")
		    if name and pass then
			    tAdmins[name] = pass
		    end
		    line = file.readLine()
	    end
	    file.close() -- close the file
	    return tAdmins -- return the table
    end
    return nil
end
-------------------------------------------
print("Loading...")
sleep(1)
print("Bessy Lock v1.2 Started")
print("Door Lock For Power Sub Station.")
print("Please Enter Username.")
write("Username: ")
name = read()
-----------------------------------------------------
local tAdmins = loadAdmins()
local tUsers = loadUsers()
if tUsers[name] ~= nil then
print("Username Accepeted Please Enter Password")
write("Password: ")
pass = read("*") -- get password
if tUsers[name] == pass then
print("Welcome ".. name)
rs.setOutput(side,true)
sleep(4)
rs.setOutput(side,false)
os.reboot()
elseif tAdmins[name] ~= nil then
print("Username Accepeted Please Enter Password")
write("Password: ")
pass = read("*") -- get password
if tAdmins[name] == pass then
term.clear()
term.setCursorPos(1,1)
print("Password & ".. name" Correct!")
print("Computer unlocking...")
print("Please type 'reboot' or 'shutdown' before leaving")
os.pullEvent(terminate)
end
end
end
os.pullEvent = oldPullEvent
MysticT #12
Posted 15 July 2012 - 01:49 AM
Ok, this is what the if should look like:

write("Username: ")
local usr = read()
if tUsers[usr] ~= nil then
    write("Password: ")
    local pass = read("*")
    if tUsers[usr] == pass then
        -- User accepted, open door
    else
        -- Wrong password
    end
elseif tAdmins[usr] ~= nil then
    write("Password: ")
    pass = read("*")
    if tAdmins[usr] == pass then
        -- Admin accepted, give access to the computer
    else
        -- Wrong password
    end
else
    -- Wrong username
end