Advertisement
DOGGYWOOF

Untitled

Jun 28th, 2024 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.14 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. while true do
  181. term.clear()
  182. term.setCursorPos(1, 1)
  183. print("Security Card Manager")
  184. print("1. Add Card")
  185. print("2. Remove Cards")
  186. print("3. Show All Security Cards")
  187. print("4. Disable Security Card Login")
  188. print("5. Back to Main Menu")
  189.  
  190. local choice = read()
  191.  
  192. if choice == "1" then
  193. addCardMenu(username)
  194. elseif choice == "2" then
  195. removeCardsMenu(username)
  196. elseif choice == "3" then
  197. showAllSecurityCards(username)
  198. elseif choice == "4" then
  199. disableSecurityCardLogin(username)
  200. elseif choice == "5" then
  201. return -- Back to main menu
  202. else
  203. print("Invalid choice. Try again.")
  204. sleep(2)
  205. end
  206. end
  207. end
  208.  
  209. -- Function for Add Card submenu
  210. function addCardMenu(username)
  211. while true do
  212. term.clear()
  213. term.setCursorPos(1, 1)
  214. print("Add Card")
  215. print("1. Insert Security Card")
  216. print("2. Enter Security Card ID")
  217. print("3. Back to Security Card Manager")
  218.  
  219. local choice = read()
  220.  
  221. if choice == "1" then
  222. grabAndSaveDiskID(username)
  223. elseif choice == "2" then
  224. saveDiskID(username)
  225. elseif choice == "3" then
  226. return -- Back to Security Card Manager
  227. else
  228. print("Invalid choice. Try again.")
  229. sleep(2)
  230. end
  231. end
  232. end
  233.  
  234. -- Function for Remove Cards submenu
  235. function removeCardsMenu(username)
  236. while true do
  237. term.clear()
  238. term.setCursorPos(1, 1)
  239. print("Remove Cards")
  240. print("1. Insert Security Card to delete")
  241. print("2. Enter Security Card ID to delete")
  242. print("3. Back to Security Card Manager")
  243.  
  244. local choice = read()
  245.  
  246. if choice == "1" then
  247. grabAndDeleteDiskID(username)
  248. elseif choice == "2" then
  249. deleteSpecificCard(username)
  250. elseif choice == "3" then
  251. return -- Back to Security Card Manager
  252. else
  253. print("Invalid choice. Try again.")
  254. sleep(2)
  255. end
  256. end
  257. end
  258.  
  259. -- Function to delete a specific security card by ID
  260. function deleteSpecificCard(username)
  261. term.clear()
  262. term.setCursorPos(1, 1)
  263. print("Enter Card ID to delete:")
  264. local cardID = read()
  265.  
  266. local filePath = "/disk/users/" .. username .. "/ID/" .. cardID .. ".file"
  267.  
  268. if fs.exists(filePath) then
  269. fs.delete(filePath)
  270. print("Security Card deleted successfully.")
  271. else
  272. print("Security Card not found.")
  273. end
  274.  
  275. sleep(2)
  276. end
  277.  
  278. -- Function to grab disk ID and delete it for a user
  279. function grabAndDeleteDiskID(username)
  280. term.clear()
  281. term.setCursorPos(1, 1)
  282. print("Insert Security Card to delete ID...")
  283.  
  284. while true do
  285. local peripherals = peripheral.getNames()
  286.  
  287. for _, name in ipairs(peripherals) do
  288. if peripheral.getType(name) == "drive" then
  289. local diskID = disk.getID(name)
  290. if diskID then
  291. local filePath = "/disk/users/" .. username .. "/ID/" .. diskID .. ".file"
  292.  
  293. if fs.exists(filePath) then
  294. fs.delete(filePath)
  295. print("Security Card ID deleted successfully.")
  296. else
  297. print("Security Card ID not found.")
  298. end
  299.  
  300. return -- Exit function after deleting ID
  301. end
  302. end
  303. end
  304.  
  305. sleep(1) -- Check every second
  306. end
  307. end
  308.  
  309. -- Function to save disk ID for a user
  310. function saveDiskID(username)
  311. term.clear()
  312. term.setCursorPos(1, 1)
  313. print("Enter Card ID:")
  314. local cardID = read()
  315.  
  316. -- Create directory if it doesn't exist
  317. local idDir = "/disk/users/" .. username .. "/ID"
  318. if not fs.exists(idDir) then
  319. fs.makeDir(idDir)
  320. end
  321.  
  322. -- Save disk ID as a file
  323. local filePath = idDir .. "/" .. cardID .. ".file"
  324. local file = fs.open(filePath, "w")
  325. file.writeLine("Disk ID: " .. cardID)
  326. file.close()
  327.  
  328. print("Disk ID saved successfully.")
  329. sleep(2)
  330. end
  331.  
  332. -- Function to grab disk ID and save it for a user
  333. function grabAndSaveDiskID(username)
  334. term.clear()
  335. term.setCursorPos(1, 1)
  336. print("Insert Security Card to grab ID...")
  337.  
  338. while true do
  339. local peripherals = peripheral.getNames()
  340.  
  341. for _, name in ipairs(peripherals) do
  342. if peripheral.getType(name) == "drive" then
  343. local diskID = disk.getID(name)
  344. if diskID then
  345. -- Create directory if it doesn't exist
  346. local idDir = "/disk/users/" .. username .. "/ID"
  347. if not fs.exists(idDir) then
  348. fs.makeDir(idDir)
  349. end
  350.  
  351. -- Save disk ID as a file
  352. local filePath = idDir .. "/" .. diskID .. ".file"
  353. local file = fs.open(filePath, "w")
  354. file.writeLine("Disk ID: " .. diskID)
  355. file.close()
  356.  
  357. print("Disk ID grabbed and saved successfully.")
  358. return -- Exit function after saving
  359. end
  360. end
  361. end
  362.  
  363. sleep(1) -- Check every second
  364. end
  365. end
  366.  
  367. -- Function to disable Security Card Login (delete all IDs)
  368. function disableSecurityCardLogin(username)
  369. term.clear()
  370. term.setCursorPos(1, 1)
  371. print("Are you sure you want to disable Security Card Login? (y/n)")
  372. local confirm = read()
  373. if confirm:lower() == "y" then
  374. local idDir = "/disk/users/" .. username .. "/ID"
  375. if fs.exists(idDir) then
  376. fs.delete(idDir)
  377. print("Security Card Login disabled successfully.")
  378. else
  379. print("No security cards found to delete.")
  380. end
  381. else
  382. print("Operation canceled.")
  383. end
  384.  
  385. sleep(2)
  386. end
  387.  
  388. -- Function to show all security cards for a user
  389. function showAllSecurityCards(username)
  390. term.clear()
  391. term.setCursorPos(1, 1)
  392. print("Security Cards for User: " .. username)
  393.  
  394. local idDir = "/disk/users/" .. username .. "/ID"
  395. if fs.exists(idDir) then
  396. local files = fs.list(idDir)
  397. for _, file in ipairs(files) do
  398. if fs.isDir(idDir .. "/" .. file) == false then
  399. print(file)
  400. end
  401. end
  402. else
  403. print("No security cards found.")
  404. end
  405.  
  406. print("Press any key to continue...")
  407. read()
  408. end
  409.  
  410. -- Main program
  411. while true do
  412. term.clear()
  413. term.setCursorPos(1, 1)
  414. print("1. Create User")
  415. print("2. Delete User")
  416. print("3. View All Users")
  417. print("4. Password Recovery (Admin Only)")
  418. print("5. Security Card Login")
  419. print("6. Exit")
  420.  
  421. local choice = read()
  422.  
  423. if choice == "1" then
  424. createUser()
  425. elseif choice == "2" then
  426. deleteUser()
  427. elseif choice == "3" then
  428. viewAllUsers()
  429. elseif choice == "4" then
  430. passwordRecovery()
  431. elseif choice == "5" then
  432. print("Enter username:")
  433. local username = read()
  434. -- Validate username
  435. if fs.exists("/disk/users/" .. username) then
  436. print("Enter password:")
  437. local password = read("*")
  438. -- Verify password before proceeding to security card login
  439. local file = fs.open("/disk/users/" .. username .. "/password.txt", "r")
  440. local storedPassword = file.readAll()
  441. file.close()
  442. if password == storedPassword then
  443. securityCardLogin(username)
  444. else
  445. print("Incorrect password.")
  446. sleep(2)
  447. end
  448. else
  449. print("User not found.")
  450. sleep(2)
  451. end
  452. elseif choice == "6" then
  453. exitProgram()
  454. else
  455. print("Invalid choice. Try again.")
  456. sleep(2)
  457. end
  458. end
  459.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement