Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[tlfOS Security API]]--
- function login()
- local verify = false
- local admin = false
- local Users = loadUsers()
- local user = ""
- local pass = ""
- local title = "Login"
- local login = {
- "Username:",
- "",
- "",
- "Password:",
- ""
- }
- local w,h = term.getSize()
- table.insert(login, title)
- local max = text.getLongestString(login) + 2
- table.remove(login, #login)
- ejectAll()
- for i = 1, #Users do
- if not fs.isDir("&SYS&tlf/users/"..Users[i].name) then
- fs.delete("&SYS&tlf/users/"..Users[i].name)
- end
- end
- if #Users == 0 then
- user = makeUser("true")
- verify = true
- admin = true
- else
- term.clear()
- screen.displayWindow(title, login, (w-max)/2, (h-#login-2)/2)
- user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, (h-#login-2)/2 + 2)
- pass = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, (h-#login-2)/2 + 5, "*")
- for i = 1, #Users do
- if user == Users[i].name then
- if pass == Users[i].password then
- verify = true
- if Users[i].admin == "true" then
- admin = true
- end
- end
- end
- end
- end
- if verify then
- screen.clear(true, true, user)
- return user, admin
- else
- text.printCentered("Incorrect username or password", (h-#login-2)/2 + 7)
- sleep(1.5)
- os.shutdown()
- end
- end
- function makeUser(admin)
- if admin ~= "true" then
- admin = "false"
- end
- local user = ""
- local pass = ""
- local title = "Create User"
- local login = {
- "Enter a username and password",
- "Username:",
- "",
- "",
- "Password:",
- ""
- }
- local w,h = term.getSize()
- table.insert(login, title)
- local max = text.getLongestString(login) + 2
- table.remove(login, #login)
- user = ""
- pass = ""
- screen.displayWindow(title, login, (w-max)/2, ((h-#login)-2)/2)
- user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 3)
- pass = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 6, "*")
- if not fs.exists("&SYS&tlf/users") then
- fs.makeDir("&SYS&tlf/users")
- end
- if fs.exists("&SYS&tlf/users/"..user) and fs.isDir("&SYS&tlf/users/"..user) then
- print("User already exists")
- sleep(1)
- elseif not fs.isDir("&SYS&tlf/users/"..user) then
- fs.delete("&SYS&tlf/users/"..user)
- else
- fs.makeDir("&SYS&tlf/users/"..user)
- temp = fs.open("&SYS&tlf/users/"..user.."/account", "w")
- temp.write("password:"..pass.."\nadmin:"..admin)
- temp.close()
- end
- return user
- end
- function changePass()
- local user = ""
- local passOld = ""
- local passNew = ""
- local title = "Change Password"
- local Users = loadUsers()
- local verify = false
- local admin = "false"
- local login = {
- "Username:",
- "",
- "",
- "Current Password:",
- "",
- "",
- "New Password:",
- ""
- }
- local w,h = term.getSize()
- table.insert(login, title)
- local max = text.getLongestString(login) + 2
- table.remove(login, #login)
- screen.displayWindow(title, login, (w-max)/2, ((h-#login)-2)/2)
- user = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 2)
- passOld = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 5, "*")
- passNew = text.readWithBoundaries((w-max)/2 + 1, (w-max)/2 + max - 1, ((h-#login)-2)/2 + 8, "*")
- for i = 1, #Users do
- if user == Users[i].name then
- if passOld == Users[i].password then
- verify = true
- if Users[i].admin == "true" then
- admin = "true"
- end
- end
- end
- end
- if not verify then
- print("User does not exist or password is incorrect")
- sleep(1)
- else
- fs.delete("&SYS&tlf/users/"..user.."/account")
- temp = fs.open("&SYS&tlf/users/"..user.."/account", "w")
- temp.write("password:"..passNew.."\nadmin:"..admin)
- temp.close()
- end
- end
- function loadUsers()
- local tUsers = {};
- local file = nil
- if not fs.exists("&SYS&tlf/users") or not fs.isDir("&SYS&tlf/users") then
- fs.makeDir("&SYS&tlf/users")
- end
- local tFiles = fs.list("&SYS&tlf/users")
- for i=1, #tFiles do
- file = fs.open("&SYS&tlf/users/"..tFiles[i].."/account", "r")
- if file ~= nil then
- line1 = file.readLine()
- if string.find(line1, "password:(.+)") == nil then
- file.close()
- fs.delete("&SYS&tlf/users/"..tFiles[i].."/account")
- else
- for string in string.gmatch(line1, "password:(.+)") do
- string1 = string
- end
- for string in string.gmatch(file.readLine(), "admin:(.+)") do
- string2 = string
- end
- tUsers[i] = {name = tFiles[i], password = string1, admin = string2}
- file.close()
- end
- end
- end
- return tUsers
- end
- function ejectAll()
- disk.eject("top")
- disk.eject("bottom")
- disk.eject("left")
- disk.eject("right")
- disk.eject("front")
- disk.eject("back")
- end
Advertisement
Add Comment
Please, Sign In to add comment