Advertisement
DOGGYWOOF

manage users test

Jan 20th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. -- Function to create a new user
  2. function createUser()
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. print("Enter new username:")
  6. local username = read()
  7.  
  8. -- Check if the username already exists
  9. if fs.exists("/disk/users/" .. username) then
  10. print("Username already exists. Try again.")
  11. sleep(2)
  12. return
  13. end
  14.  
  15. -- Create a directory for the new user
  16. fs.makeDir("/disk/users/" .. username)
  17.  
  18. -- Prompt for password
  19. local password
  20. repeat
  21. term.clear()
  22. term.setCursorPos(1, 1)
  23. print("Enter password:")
  24. password = read("*")
  25.  
  26. print("Confirm password:")
  27. local confirmPassword = read("*")
  28.  
  29. if password ~= confirmPassword then
  30. print("Passwords do not match. Try again.")
  31. sleep(2)
  32. end
  33. until password == confirmPassword
  34.  
  35. -- Store the password in a text file
  36. local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  37. file.write(password)
  38. file.close()
  39.  
  40. print("User created successfully.")
  41. sleep(2)
  42. end
  43.  
  44. -- Function to delete a user
  45. function deleteUser()
  46. term.clear()
  47. term.setCursorPos(1, 1)
  48. print("Enter username to delete:")
  49. local username = read()
  50.  
  51. -- Check if the username exists
  52. if not fs.exists("/disk/users/" .. username) then
  53. print("User not found. Try again.")
  54. sleep(2)
  55. return
  56. end
  57.  
  58. -- Check for the presence of block.txt
  59. if fs.exists("/disk/users/" .. username .. "/block.txt") then
  60. -- Display content of block.txt as an error message
  61. local file = fs.open("/disk/users/" .. username .. "/block.txt", "r")
  62. local errorMessage = file.readAll()
  63. file.close()
  64. print(errorMessage)
  65. sleep(2)
  66. return
  67. end
  68.  
  69. -- Prompt for password to confirm deletion
  70. print("Enter password to confirm deletion:")
  71. local inputPassword = read("*")
  72.  
  73. -- Read stored password from the file
  74. local file = fs.open("/disk/users/" .. username .. "/password.txt", "r")
  75. local storedPassword = file.readAll()
  76. file.close()
  77.  
  78. -- Compare entered password with stored password
  79. if inputPassword == storedPassword then
  80. -- Delete the user directory
  81. fs.delete("/disk/users/" .. username)
  82. print("User deleted successfully.")
  83. else
  84. print("Incorrect password. Deletion failed.")
  85. end
  86.  
  87. sleep(2)
  88. end
  89.  
  90. -- Function to view all users
  91. function viewAllUsers()
  92. term.clear()
  93. term.setCursorPos(1, 1)
  94. print("All Users:")
  95. local users = fs.list("/disk/users")
  96. for _, user in ipairs(users) do
  97. print(user)
  98. end
  99. print("Press any key to continue...")
  100. read()
  101. end
  102.  
  103. -- Function for password recovery by admin
  104. function passwordRecovery()
  105. term.clear()
  106. term.setCursorPos(1, 1)
  107. print("Enter admin username:")
  108. local adminUsername = read()
  109.  
  110. -- Check if the entered user has admin privileges
  111. if not fs.exists("/disk/users/" .. adminUsername .. "/admin.txt") then
  112. print("Permission denied. Admin access required.")
  113. sleep(2)
  114. return
  115. end
  116.  
  117. -- Prompt for admin password
  118. print("Enter admin password:")
  119. local adminPassword = read("*")
  120.  
  121. -- Read stored admin password from the file
  122. local adminFile = fs.open("/disk/users/" .. adminUsername .. "/password.txt", "r")
  123. local storedAdminPassword = adminFile.readAll()
  124. adminFile.close()
  125.  
  126. -- Compare entered admin password with stored admin password
  127. if adminPassword == storedAdminPassword then
  128. print("Enter username for password reset:")
  129. local username = read()
  130.  
  131. -- Check if the username exists
  132. if not fs.exists("/disk/users/" .. username) then
  133. print("User not found. Try again.")
  134. sleep(2)
  135. return
  136. end
  137.  
  138. -- Confirm password reset
  139. print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
  140. local confirm = read()
  141. if confirm:lower() ~= "y" then
  142. print("Password reset canceled.")
  143. sleep(2)
  144. return
  145. end
  146.  
  147. -- Prompt for new password
  148. term.clear()
  149. term.setCursorPos(1, 1)
  150. print("Enter new password for " .. username .. ":")
  151. local newPassword = read("*")
  152.  
  153. -- Store the new password in the text file
  154. local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  155. file.write(newPassword)
  156. file.close()
  157.  
  158. print("Password reset successfully.")
  159. else
  160. print("Incorrect admin password. Password reset failed.")
  161. end
  162.  
  163. sleep(2)
  164. end
  165.  
  166. -- Function to exit and run /disk/os/gui
  167. function exitProgram()
  168. term.clear()
  169. term.setCursorPos(1, 1)
  170. print("Exiting program...")
  171. sleep(1)
  172. shell.run("/disk/os/gui")
  173. error("Exiting program.")
  174. end
  175.  
  176. -- Main program
  177. while true do
  178. term.clear()
  179. term.setCursorPos(1, 1)
  180. print("1. Create User")
  181. print("2. Delete User")
  182. print("3. View All Users")
  183. print("4. Password Recovery (Admin Only)")
  184. print("5. Exit")
  185.  
  186. local choice = read()
  187.  
  188. if choice == "1" then
  189. createUser()
  190. elseif choice == "2" then
  191. deleteUser()
  192. elseif choice == "3" then
  193. viewAllUsers()
  194. elseif choice == "4" then
  195. passwordRecovery()
  196. elseif choice == "5" then
  197. exitProgram()
  198. else
  199. print("Invalid choice. Try again.")
  200. sleep(2)
  201. end
  202. end
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement