Advertisement
DOGGYWOOF

delete after

Jan 21st, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  2. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  3.  
  4. local USERS_FOLDER = "/disk/users/"
  5. local ERROR_FOLDER = "/disk/error/"
  6. local BSOD_PROGRAM = "BSOD.lua"
  7. local DISABLED_FILE = "disabled.txt"
  8.  
  9. -- Function to check admin credentials
  10. local function checkAdminCredentials()
  11. term.clear()
  12. term.setCursorPos(1, 1)
  13. print("Enter admin username:")
  14. local adminUsername = read()
  15.  
  16. -- Check if the entered user has admin privileges
  17. if not fs.exists(USERS_FOLDER .. adminUsername .. "/admin.txt") then
  18. print("Permission denied. Admin access required.")
  19. sleep(2)
  20. return false
  21. end
  22.  
  23. -- Prompt for admin password
  24. print("Enter admin password:")
  25. local adminPassword = read("*")
  26.  
  27. -- Read stored admin password from the file
  28. local adminFile = fs.open(USERS_FOLDER .. adminUsername .. "/password.txt", "r")
  29. local storedAdminPassword = adminFile.readAll()
  30. adminFile.close()
  31.  
  32. -- Compare entered admin password with stored admin password
  33. if adminPassword == storedAdminPassword then
  34. return true
  35. else
  36. print("Incorrect admin password.")
  37. sleep(2)
  38. return false
  39. end
  40. end
  41.  
  42. -- Function to create a new user
  43. function createUser()
  44. if not checkAdminCredentials() then
  45. return
  46. end
  47.  
  48. term.clear()
  49. term.setCursorPos(1, 1)
  50. print("Enter new username:")
  51. local username = read()
  52.  
  53. -- Check if the username already exists
  54. if fs.exists(USERS_FOLDER .. username) then
  55. print("Username already exists. Try again.")
  56. sleep(2)
  57. return
  58. end
  59.  
  60. -- Create a directory for the new user
  61. fs.makeDir(USERS_FOLDER .. username)
  62.  
  63. -- Prompt for password
  64. local password
  65. repeat
  66. term.clear()
  67. term.setCursorPos(1, 1)
  68. print("Enter password:")
  69. password = read("*")
  70.  
  71. print("Confirm password:")
  72. local confirmPassword = read("*")
  73.  
  74. if password ~= confirmPassword then
  75. print("Passwords do not match. Try again.")
  76. sleep(2)
  77. end
  78. until password == confirmPassword
  79.  
  80. -- Store the password in a text file
  81. local file = fs.open(USERS_FOLDER .. username .. "/password.txt", "w")
  82. file.write(password)
  83. file.close()
  84.  
  85. print("User created successfully.")
  86. sleep(2)
  87. end
  88.  
  89. -- Function to delete a user
  90. function deleteUser()
  91. if not checkAdminCredentials() then
  92. return
  93. end
  94.  
  95. term.clear()
  96. term.setCursorPos(1, 1)
  97. print("Enter username to delete:")
  98. local usernameToDelete = read()
  99.  
  100. local users = fs.list(USERS_FOLDER)
  101.  
  102. if users and table.concat(users, ","):find(usernameToDelete) then
  103. -- User exists, check for block.txt
  104. if fs.exists(USERS_FOLDER .. usernameToDelete .. "/block.txt") then
  105. -- Display content of block.txt as an error message
  106. local file = fs.open(USERS_FOLDER .. usernameToDelete .. "/block.txt", "r")
  107. local errorMessage = file.readAll()
  108. file.close()
  109. print(errorMessage)
  110. sleep(2)
  111. return
  112. end
  113.  
  114. -- Prompt for password to confirm deletion
  115. print("Enter password to confirm deletion:")
  116. local inputPassword = read("*")
  117.  
  118. -- Read stored password from the file
  119. local file = fs.open(USERS_FOLDER .. usernameToDelete .. "/password.txt", "r")
  120. local storedPassword = file.readAll()
  121. file.close()
  122.  
  123. -- Compare entered password with stored password
  124. if inputPassword == storedPassword then
  125. -- Delete the user directory
  126. fs.delete(USERS_FOLDER .. usernameToDelete)
  127. print("User '" .. usernameToDelete .. "' deleted successfully.")
  128. else
  129. print("Incorrect password. Deletion failed.")
  130. end
  131. else
  132. print("User '" .. usernameToDelete .. "' not found.")
  133. end
  134.  
  135. sleep(2)
  136. end
  137.  
  138. -- Function to view all users
  139. function viewAllUsers()
  140. if not checkAdminCredentials() then
  141. return
  142. end
  143.  
  144. term.clear()
  145. term.setCursorPos(1, 1)
  146. print("All Users:")
  147. local users = fs.list(USERS_FOLDER)
  148.  
  149. if users then
  150. for _, user in ipairs(users) do
  151. print(user)
  152. end
  153. end
  154.  
  155. print("Press any key to continue...")
  156. read()
  157. end
  158.  
  159. -- Function for password recovery by admin
  160. function passwordRecovery()
  161. if not checkAdminCredentials() then
  162. return
  163. end
  164.  
  165. term.clear()
  166. term.setCursorPos(1, 1)
  167. print("Enter username for password reset:")
  168. local username = read()
  169.  
  170. -- Check if the username exists
  171. if not fs.exists(USERS_FOLDER .. username) then
  172. print("User not found. Try again.")
  173. sleep(2)
  174. return
  175. end
  176.  
  177. -- Confirm password reset
  178. print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
  179. local confirm = read()
  180. if confirm:lower() ~= "y" then
  181. print("Password reset canceled.")
  182. sleep(2)
  183. return
  184. end
  185.  
  186. -- Prompt for new password
  187. term.clear()
  188. term.setCursorPos(1, 1)
  189. print("Enter new password for " .. username .. ":")
  190. local newPassword = read("*")
  191.  
  192. -- Store the new password in the text file
  193. local file = fs.open(USERS_FOLDER .. username .. "/password.txt", "w")
  194. file.write(newPassword)
  195. file.close()
  196.  
  197. print("Password reset successfully.")
  198. sleep(2)
  199. end
  200.  
  201. -- Function to activate user account
  202. local function activateUserAccount()
  203. if not checkAdminCredentials() then
  204. return
  205. end
  206.  
  207. term.clear()
  208. term.setCursorPos(1, 1)
  209. print("Enter username to activate:")
  210. local usernameToActivate = read()
  211.  
  212. local users = fs.list(USERS_FOLDER)
  213.  
  214. if users and table.concat(users, ","):find(usernameToActivate) then
  215. -- User exists, check if disabled.txt exists
  216. local userDirectory = fs.exists(USERS_FOLDER .. usernameToActivate) and USERS_FOLDER
  217. local disabledFilePath = userDirectory .. usernameToActivate .. "/" .. DISABLED_FILE
  218.  
  219. if fs.exists(disabledFilePath) then
  220. -- Disabled file found, delete it
  221. fs.delete(disabledFilePath)
  222. print("User '" .. usernameToActivate .. "' account is now active and can log in.")
  223. else
  224. print("User '" .. usernameToActivate .. "' account is not disabled.")
  225. end
  226. else
  227. print("User '" .. usernameToActivate .. "' not found.")
  228. end
  229.  
  230. sleep(2)
  231. end
  232.  
  233. -- Function to exit and run /disk/os/gui
  234. function exitProgram()
  235. if not checkAdminCredentials() then
  236. return
  237. end
  238.  
  239. term.clear()
  240. term.setCursorPos(1, 1)
  241. print("Exiting program...")
  242. sleep(1)
  243. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  244. error("Exiting program.")
  245. end
  246.  
  247. -- New main program with added options
  248. while true do
  249. term.clear()
  250. term.setCursorPos(1, 1)
  251. print("1. Create User")
  252. print("2. Delete User")
  253. print("3. View All Users")
  254. print("4. Password Recovery (Admin Only)")
  255. print("5. Activate User Account")
  256. print("6. Exit")
  257.  
  258. local choice = read()
  259.  
  260. if choice == "1" then
  261. createUser()
  262. elseif choice == "2" then
  263. deleteUser()
  264. elseif choice == "3" then
  265. viewAllUsers()
  266. elseif choice == "4" then
  267. passwordRecovery()
  268. elseif choice == "5" then
  269. activateUserAccount()
  270. elseif choice == "6" then
  271. exitProgram()
  272. else
  273. print("Invalid choice. Try again.")
  274. sleep(2)
  275. end
  276. end
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement