Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jsonpickle, os, hashlib
- class AccountInfo:
- def __init__(self, password, secret_code=''):
- self.password = password
- self.secret_code = secret_code
- self.admin = False
- class Request:
- def __init__(self, username, admin_request=False, delete_request=False):
- self.username = username
- self.admin_request = admin_request
- self.delete_request = delete_request
- def load_dictionary(path):
- if os.path.isfile(path) and os.stat(path).st_size != 0:
- with open(path) as file:
- contents = file.read()
- return jsonpickle.decode(contents)
- return {}
- def save_dictionary(dictionary, path):
- with open(path, 'w+') as file:
- string = jsonpickle.encode(dictionary, file)
- file.write(string)
- def main():
- current_account = ''
- while 1 == 1:
- print("Menu: ")
- print("1. Register")
- print("2. Login")
- print("3. Show accounts")
- print("4. Logout")
- print("5. Forgot your password")
- print("6. Admin request")
- print("7. View requests")
- print("8. View status")
- print("9. View Admins")
- print("10. Delete account")
- print("11. Exit")
- choice = input()
- if choice == "1":
- register()
- elif choice == "2":
- current_account = login()
- elif choice == "3":
- show_accounts(current_account)
- elif choice == "4":
- current_account = logout(current_account)
- elif choice == "5":
- forgot_password()
- elif choice == "6":
- admin_request(current_account)
- elif choice == "7":
- view_requests(current_account)
- elif choice == "8":
- view_status(current_account)
- elif choice == "9":
- view_admins()
- elif choice == "10":
- delete_account(current_account)
- elif choice == "11":
- exit()
- def register():
- print("Enter your desired username: ")
- username = input()
- if username in database:
- print("Account already exists.")
- exit()
- print("Enter your desired password: ")
- password = input()
- print("Enter your secret code (4 digits long): ")
- secret_code = input()
- if len(secret_code) != 4:
- print("Your secret code must be 4 digits long.")
- return
- if not secret_code.isdigit():
- print("Your code must be a 4 digit password.")
- return
- database[username] = AccountInfo(hashlib.sha512(password.encode('utf-8')).hexdigest(), secret_code)
- if username == "shredder8910" or username == "asher" or username == "cookie":
- database[username].admin = True
- save_dictionary(database, database_path)
- def login():
- print("Enter your username: ")
- username = input()
- print("Enter your password: ")
- password = input()
- encrypted = hashlib.sha512(password.encode('utf-8')).hexdigest()
- if username not in database:
- print("Your account does not exist.")
- return ''
- if not database[username].password:
- print("Incorrect password.")
- return ''
- else:
- print("Logged in as " + username)
- return username
- def show_accounts(current_account):
- if current_account in database:
- if database[current_account].admin:
- print("Accounts: ")
- for item in database.items():
- print("Username: " + item[0] + " Password: " + item[1].password)
- else:
- print("You don't have admin privileges.")
- else:
- print("Your account does not exist or you are not logged in.")
- def logout(current_account):
- if current_account != '':
- print("You're now logged out.")
- else:
- print("You're not logged in.")
- return ''
- def forgot_password():
- print("Enter your username: ")
- username = input()
- print("Enter your secret code you used when you created your account: ")
- secret_code = input()
- if username in database:
- if database[username].secretCode == secret_code:
- print("Verification success.")
- print("Enter your new password: ")
- new_password = input()
- if database[username].password == new_password:
- print("You must select a new password.")
- else:
- database[username].password = new_password
- else:
- print("Account doesn't exist")
- def admin_request(current_account):
- if current_account != '':
- if current_account in database:
- if database[current_account].admin:
- print("You're already an admin.")
- else:
- requests[current_account] = Request(current_account, True)
- print("Admin request sent.")
- save_dictionary(requests, requests_path)
- else:
- print("You're not logged in.")
- def view_status(current_account):
- if current_account != '':
- if current_account in database:
- print("User: " + current_account + " | Admin: " + str(database[current_account].admin))
- else:
- print("Your account does not exist.")
- else:
- print("You're not logged in.")
- def view_admins():
- for username, account_info in database.items():
- if account_info.admin:
- print("User: " + username + " | Admin: " + str(account_info.admin))
- def view_requests(current_account):
- resolved_requests = []
- if current_account in database:
- if database[current_account].admin:
- for username, request in requests.items():
- if request.admin_request:
- print("Request from " + username + " to be admin.")
- print("Allow to be admin? (yes or no): ")
- answer = input()
- if answer.lower() == "yes":
- database[username].admin = True
- print("Admin access granted.")
- else:
- print("Admin access denied.")
- if request.delete_request:
- print("Request from " + username + " for their account to be deleted.")
- print("Allow account to be deleted? (yes or no): ")
- answer = input()
- if answer.lower() == "yes":
- database.pop(username, None)
- print("Account deleted.")
- else:
- print("Delete request denied.")
- resolved_requests.append(username)
- for resolved in resolved_requests:
- requests.pop(resolved, None)
- save_dictionary(requests, requests_path)
- save_dictionary(database, database_path)
- def delete_account(current_account):
- if current_account != '':
- if current_account in database:
- print("What is your secret code?")
- secret_code = input()
- if database[current_account].secret_code != secret_code:
- print("Incorrect secret code.")
- return
- print("Secret code valid.")
- print("Type DELETE to send a request for your account to be deleted.")
- response = input()
- if response == "DELETE":
- request = requests.get(current_account, Request(current_account))
- requests[current_account] = request
- requests[current_account].delete_request = True
- print("Request sent.")
- save_dictionary(requests, requests_path)
- else:
- print("Request cancelled.")
- else:
- print("Your account does not exist.")
- else:
- print("You're not logged in.")
- jsonpickle.set_encoder_options('simplejson', indent=4)
- database_path = 'database.json'
- requests_path = 'requests.json'
- database = load_dictionary(database_path)
- requests = load_dictionary(requests_path)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement