Advertisement
DOGGYWOOF

DOGGY OS DEV USER MANAGEMENT

Jun 28th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 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. if user ~= "root" then
  98. print(user)
  99. end
  100. end
  101. print("Press any key to continue...")
  102. read()
  103. end
  104.  
  105. -- Function for password recovery by admin
  106. function passwordRecovery()
  107. term.clear()
  108. term.setCursorPos(1, 1)
  109. print("Enter admin username:")
  110. local adminUsername = read()
  111.  
  112. -- Check if the entered user has admin privileges
  113. if not fs.exists("/disk/users/" .. adminUsername .. "/admin.txt") then
  114. print("Permission denied. Admin access required.")
  115. sleep(2)
  116. return
  117. end
  118.  
  119. -- Prompt for admin password
  120. print("Enter admin password:")
  121. local adminPassword = read("*")
  122.  
  123. -- Read stored admin password from the file
  124. local adminFile = fs.open("/disk/users/" .. adminUsername .. "/password.txt", "r")
  125. local storedAdminPassword = adminFile.readAll()
  126. adminFile.close()
  127.  
  128. -- Compare entered admin password with stored admin password
  129. if adminPassword == storedAdminPassword then
  130. print("Enter username for password reset:")
  131. local username = read()
  132.  
  133. -- Check if the username exists
  134. if not fs.exists("/disk/users/" .. username) then
  135. print("User not found. Try again.")
  136. sleep(2)
  137. return
  138. end
  139.  
  140. -- Confirm password reset
  141. print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
  142. local confirm = read()
  143. if confirm:lower() ~= "y" then
  144. print("Password reset canceled.")
  145. sleep(2)
  146. return
  147. end
  148.  
  149. -- Prompt for new password
  150. term.clear()
  151. term.setCursorPos(1, 1)
  152. print("Enter new password for " .. username .. ":")
  153. local newPassword = read("*")
  154.  
  155. -- Store the new password in the text file
  156. local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  157. file.write(newPassword)
  158. file.close()
  159.  
  160. print("Password reset successfully.")
  161. else
  162. print("Incorrect admin password. Password reset failed.")
  163. end
  164.  
  165. sleep(2)
  166. end
  167.  
  168. -- Function to exit and run /disk/os/gui
  169. function exitProgram()
  170. term.clear()
  171. term.setCursorPos(1, 1)
  172. print("Exiting program...")
  173. sleep(1)
  174. shell.run("/disk/os/gui")
  175. error("Exiting program.")
  176. end
  177.  
  178. -- Function for Security Card Login (insert or enter ID)
  179. function securityCardLogin(username)
  180. term.clear()
  181. term.setCursorPos(1, 1)
  182. print("1. Insert Security Card")
  183. print("2. Enter Security Card ID")
  184. print("3. Back to Main Menu")
  185.  
  186. local choice = read()
  187.  
  188. if choice == "1" then
  189. -- Insert Security Card
  190. grabAndSaveDiskID(username)
  191. elseif choice == "2" then
  192. -- Enter Security Card ID
  193. saveDiskID(username)
  194. elseif choice == "3" then
  195. return -- Back to main menu
  196. else
  197. print("Invalid choice. Try again.")
  198. sleep(2)
  199. end
  200. end
  201.  
  202. -- Function to save disk ID for a user
  203. function saveDiskID(username)
  204. term.clear()
  205. term.setCursorPos(1, 1)
  206. print("Enter Card ID:")
  207. local cardID = read()
  208.  
  209. -- Create directory if it doesn't exist
  210. local idDir = "/disk/users/" .. username .. "/ID"
  211. if not fs.exists(idDir) then
  212. fs.makeDir(idDir)
  213. end
  214.  
  215. -- Save disk ID as a file
  216. local filePath = idDir .. "/" .. cardID .. ".file"
  217. local file = fs.open(filePath, "w")
  218. file.writeLine("Disk ID: " .. cardID)
  219. file.close()
  220.  
  221. print("Disk ID saved successfully.")
  222. sleep(2)
  223. end
  224.  
  225. -- Function to grab disk ID and save it for a user
  226. function grabAndSaveDiskID(username)
  227. term.clear()
  228. term.setCursorPos(1, 1)
  229. print("Insert Security Card to grab ID...")
  230.  
  231. while true do
  232. local peripherals = peripheral.getNames()
  233.  
  234. for _, name in ipairs(peripherals) do
  235. if peripheral.getType(name) == "drive" then
  236. local diskID = disk.getID(name)
  237. if diskID then
  238. -- Create directory if it doesn't exist
  239. local idDir = "/disk/users/" .. username .. "/ID"
  240. if not fs.exists(idDir) then
  241. fs.makeDir(idDir)
  242. end
  243.  
  244. -- Save disk ID as a file
  245. local filePath = idDir .. "/" .. diskID .. ".file"
  246. local file = fs.open(filePath, "w")
  247. file.writeLine("Disk ID: " .. diskID)
  248. file.close()
  249.  
  250. print("Disk ID grabbed and saved successfully.")
  251. return -- Exit function after saving
  252. end
  253. end
  254. end
  255.  
  256. sleep(1) -- Check every second
  257. end
  258. end
  259.  
  260. -- Main program
  261. while true do
  262. term.clear()
  263. term.setCursorPos(1, 1)
  264. print("1. Create User")
  265. print("2. Delete User")
  266. print("3. View All Users")
  267. print("4. Password Recovery (Admin Only)")
  268. print("5. Security Card Login")
  269. print("6. Exit")
  270.  
  271. local choice = read()
  272.  
  273. if choice == "1" then
  274. createUser()
  275. elseif choice == "2" then
  276. deleteUser()
  277. elseif choice == "3" then
  278. viewAllUsers()
  279. elseif choice == "4" then
  280. passwordRecovery()
  281. elseif choice == "5" then
  282. print("Enter username:")
  283. local username = read()
  284. -- Validate username
  285. if fs.exists("/disk/users/" .. username) then
  286. print("Enter password:")
  287. local password = read("*")
  288. -- Verify password before proceeding to security card login
  289. local file = fs.open("/disk/users/" .. username .. "/password.txt", "r")
  290. local storedPassword = file.readAll()
  291. file.close()
  292. if password == storedPassword then
  293. securityCardLogin(username)
  294. else
  295. print("Incorrect password.")
  296. sleep(2)
  297. end
  298. else
  299. print("User not found.")
  300. sleep(2)
  301. end
  302. elseif choice == "6" then
  303. exitProgram()
  304. else
  305. print("Invalid choice. Try again.")
  306. sleep(2)
  307. end
  308. end
  309.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement