I want to make a user permissions system to control adding and deleting users… I have the adding and deleting part down… but i need to know if i can add permissions to the same key/value pair as the username and password or if i have to make another table… I also need help checking the permissions… here is the code i have so far…
Spoiler
--[[ ICBM missile control panel by LuaCrawler
This goes with the door and launch controller for it to work.
for automatic load change the name of the file to startup and remove the extension.
]]
-- Variables
local delay = 5
local modem = peripheral.wrap("top")
local passwd = "passwd"
local server = 10
local rChan = tonumber(os.getComputerID())
local users = {
["Admin"] = "password",
}
local userPerm = {
["Admin"] = "admin"
}
local logo = {
}
local function changePass()
-- Startup Function
local function startup()
term.clear()
term.setCursorPos(1,1)
term.setCursorPos(18,8)
term.setTextColor(colors.lime)
print("MissileCraft 1.04")
term.setCursorPos(18,9)
print("'Speedy Skeleton'")
sleep(5)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
modem.open(tonumber(os.getComputerID()))
end
-- Actual program run
startup()
while true do
term.clear()
term.setCursorPos(1, 1)
textutils.slowPrint("Enter username:", 10)
local cmd = read()
-- Now check store password as variable
term.clear()
term.setCursorPos(1, 1)
textutils.slowPrint("Enter your password", 10)
local passwd = read()
for k,v in pairs(users) do
if cmd == k and passwd == v then
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.red)
print("ICBM launch system V. 1.0")
term.setTextColor(colors.white)
print("What would you like to do?")
write("> ")
local cmd2 = read()
if cmd2 == "exit" then
return
elseif cmd2 == "launch missile" or cmd2 == "Launch Missile" then
os.loadAPI("/LoadBar")
local bar = LoadBar.init(LoadBar.STANDARD, logo, 10, 30, 14, colors.red, nil, nil, nil )
local function doStuff()
bar:setMessage( "Loading..." )
for i = 0, 9 do
sleep(0.5)
bar:triggerUpdate("Initializing component("..(bar:getCurrentProgress()+1).."/10)")
end
bar:triggerUpdate("Done!")
end
local function doBar()
bar:run( true )
end
parallel.waitForAll( doBar, doStuff )
term.clear()
term.setBackgroundColor(colors.gray)
term.setCursorPos(1, 1)
for i = 1, 19 do
print(" ")
end
term.setTextColor(colors.yellow)
term.setCursorPos(1, 1)
textutils.slowPrint("Remote missile launch system")
textutils.slowPrint("Usage: launch <label>", 15)
term.setTextColor(colors.white)
lSilo = read()
if lSilo == "exit" or lSilo == "quit" then
break
else
modem.transmit(server, rChan, lSilo)
end
else
if cmd2 == "open" or cmd2 == "open door" then
term.setTextColor(colors.green)
print("Door opened")
modem.transmit(3,1, cmd2)
term.setTextColor(colors.white)
elseif cmd2 == "close" or cmd2 == "close door" then
modem.transmit(3,1, cmd2)
term.setTextColor(colors.green)
print("Door closed")
term.setTextColor(colors.white)
end
end
function save(table)
local file = fs.open("users.txt", "w")
file.write(textutils.serialize(users))
file.close()
end
if cmd2 == "add user" then
print("Enter your username for verification.")
uName = read()
for k,v in pairs(userPerm) do
if k == uName and v == "admin" or v == "moderator" then
print( "Enter your password "..k )
pass = read()
if uName == k and pass == v then
term.setTextColor(colors.gray)
textutils.slowPrint("Enter the username: ")
usr = read()
textutils.slowPrint("Enter the password: ")
pass = read()
users[usr] = pass
save(users)
print("What permissions should the user have:")
textutils.slowPrint("admin, moderator, or user")
perm = read()
userPerm[usr] = perm
term.setTextColor(colors.white)
print("User created!")
elseif k == not uName and v == not pass then
print("You don't have enough permissions!")
end
end
end
elseif cmd2 == "remove user" then
print("Enter username for verification")
uName2 = read()
for k,v in pairs(userPerm) do
if k == uName2 and v == "admin" or v == "moderator" then
print("Enter your password "..k )
pass2 = read()
if uName2 == k and pass2 == v then
textutils.slowPrint("Enter the username you want to delete: ")
delUsr = read()
users[delUsr] = nil
save(users)
userPerm[delUsr] = nil
print("User Removed!")
end
end
end
else
if cmd2 == "list users" then
for k,v in pairs(users) do
print(k.. " : " ..v)
end
end
end
if cmd == not k and pass == not v or cmd == k and pass == not v then
term.setTextColor(colors.red)
print("Incorrect password! Logging failed attempt!")
file = fs.open("/etc/log.txt", "w")
rFile = fs.open("/etc/log.txt", "a")
fExist = fs.exists("/etc/log.txt")
if fExist == false then
file:write("Failed login attempt on "..os.time())
elseif fExist == true then
rFile:write("Failed login attempt on "..os.time())
end
end
end
end
os.startTimer(delay)
while true do
local evt, p1, p2, p3, p4, p5 = os.pullEvent()
if evt == "timer" then
break
elseif evt == "modem_message" then
print("Message from:"..p2)
print(p4)
end
end
end
http://chopapp.com/#hzbgpo28
Nevermind… doesn't work for some reason