Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # User Registration And Login Example
- # Mike Kerry - June 2021
- # Note: In this simple example, actual passwords are stored in the user details file
- # In a real-world situation, actual passwords would not be stored, only password hashes
- # Example of user_details text file:-
- # Mike,abc123
- # Fred,fredpass
- userdict = {} # This dictionary will hold User IDs and Passwords read from the user_details file and from user input
- while True:
- try:
- fin = open("user_details.txt")
- except:
- break # user_detais.txt does not exist. Start a new user dictionary
- for uline in fin: # Read 1 line of user-details
- uline = uline.strip() # Remove End-of-Line characters
- usplit = uline.split(",") # split line on comma
- userid = usplit[0] # Get the UserID
- userpswd = usplit[1] # Get the User Password
- if userid in userdict: # Check this User ID does not exist already
- print("Duplicate user on file:", userid)
- else:
- userdict[userid] = userpswd # Create a dictionary entry for this user
- fin.close()
- break
- while True: # Main Menu Loop
- while True: # Loop looking for valid menu option
- print("\nMenu")
- print("1: Register new user")
- print("2. Log in existing user")
- print("8. Show all users and passwords") # (Should be an Administrator option only)
- print("9: Quit")
- option = input("Choose option: ")
- if option in ["1", "2", "8", "9"]:
- break
- print("Invalid input")
- if option == "1": # Register new user
- while True: # Keep looping until a valid new User ID is entered
- userid = input("Enter user ID: ")
- if len(userid) > 12:
- print("User ID must not be longer than 12 characters")
- continue
- if userid not in userdict: # Check User ID not already on the system
- break
- print("Sorry, that user ID already exists\n")
- # Loop around asking for a User ID
- while True: # Keep looping until a valid password is chosen
- userpswd = input("Choose a password: ")
- if len(userpswd) > 5 and "," not in userpswd:
- break
- print("Passwords must be 6 characters or more and may not include a comma")
- userdict[userid] = userpswd # Add new user+password to the dictionary
- print("Congratulations. User", userid, "is now registered")
- print("You may now use option 2 to Log In to the system")
- input("Press Enter to continue")
- elif option == "2": # Log-in an existing user
- userid = input("Enter user ID: ")
- if userid in userdict: # Check that the User ID exists in our dictionary
- userpswd = input("Enter password: ") # Request password
- if userpswd == userdict[userid]: # Check password matches the one held on file
- print("You have successfully logged in as", userid)
- #
- # HERE THE LOGGED IN USER CAN DO OTHER PROCESSES
- #
- input("Press Enter to continue")
- continue
- print("Invalid user ID or password")
- elif option == "8": # Print a list of User IDs and Passwords
- # Obviously, in practice, this option would only be accessible by Admin
- sortkeys = sorted(userdict.keys())
- for userid in sortkeys:
- print(userid.ljust(16), userdict[userid])
- elif option == "9": # Quit. Write updated dictionary to text file
- fout = open("user_details.txt", "w")
- for userid in userdict:
- # Write out UserID and Password separated by a comma, and end with New-Line
- fout.write(userid + "," + userdict[userid] + "\n")
- fout.close()
- break # Break out of main menu loop, so that the program terminates
- print("Goodbye")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement