Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes --> code tidy + refactor #104

Merged
merged 8 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions customer_adoption_form_dog.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ def adopt_dog_form():
garden_fenced = get_input("Is your garden fully fenced? (yes/no): " + Style.RESET_ALL).strip().lower()
garden_access = get_input("How is the garden accessed? " + Style.RESET_ALL).strip()

elif garden == "communal":
garden_size = "N/A"
garden_fenced = "N/A"
garden_access = "N/A"

elif garden == "no":
elif garden == "communal" or garden == "no":
garden_size = "N/A"
garden_fenced = "N/A"
garden_access = "N/A"
Expand Down
109 changes: 54 additions & 55 deletions register.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,64 @@ def register():
clear_screen()

while True:
# Prompt user to enter a username
username = input("\nEnter a username: ").strip()
username = get_username()
if username is None:
continue

if username == "":
clear_screen()
print(Fore.RED + "\nUsername cannot be empty. Please try again." + Style.RESET_ALL)
password = get_password("Enter a password: ")
if password is None:
continue

# Check if username already exists in the database
if users_collection.find_one({'username': username}):
print(Fore.RED + "\nUsername already exists. Please choose another one." + Style.RESET_ALL)
time.sleep(2)
clear_screen()

confirm_password = get_password("Confirm your password: ")
if confirm_password is None:
continue

# Continuous loop for user registration
while True:
# Prompt user to enter and confirm a password
while True:
password = getpass.getpass("Enter a password: ")
if not password.strip():
clear_screen()
print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL)
continue
else:
break

while True:
confirm_password = getpass.getpass("Confirm your password: ")
if not confirm_password.strip():
clear_screen()
print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL)
continue
else:
break
if password != confirm_password:
print_error_message("\nPasswords do not match. Please try again.")
continue

user_level = get_user_level()
if user_level is None:
continue

# Check if passwords match
if password == confirm_password:
# Prompt user to enter their user level
user_level = input("Enter your user level (1-3): ")

# Validate user level input
if user_level.isdigit() and 1 <= int(user_level) <= 3:
# Hash the password
hashed_password = hash_password(password)
hashed_password = hash_password(password)
users_collection.insert_one({
'username': username,
'hashed_password': hashed_password,
'level': int(user_level)
})

# Insert user data into the MongoDB collection
users_collection.insert_one({
'username': username,
'hashed_password': hashed_password,
'level': int(user_level)
})
print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return

print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return
else:
print(Fore.RED + "\nInvalid user level. Please enter a number between 1 and 3." + Style.RESET_ALL)
else:
clear_screen()
print(Fore.RED + "\nPasswords do not match. Please try again." + Style.RESET_ALL)
def get_username():
username = input("\nEnter a username: ").strip()
if username == "":
print_error_message("\nUsername cannot be empty. Please try again.")
return None
if users_collection.find_one({'username': username}):
print_error_message("\nUsername already exists. Please choose another one.")
time.sleep(2)
clear_screen()
return None
return username

def get_password(prompt):
password = getpass.getpass(prompt)
if not password.strip():
print_error_message("\nPassword cannot be empty. Please try again.")
return None
return password

def get_user_level():
user_level = input("Enter your user level (1-3): ")
if not user_level.isdigit() or not 1 <= int(user_level) <= 3:
print_error_message("\nInvalid user level. Please enter a number between 1 and 3.")
return None
return user_level

def print_error_message(message):
clear_screen()
print(Fore.RED + message + Style.RESET_ALL)
77 changes: 43 additions & 34 deletions sudo_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,76 @@
users_collection = db['users']

def sudo_admin():

clear_screen()

# Continuous loop for sudo user authentication
attempts = 0
while attempts < MAX_ATTEMPTS:
print(Fore.LIGHTMAGENTA_EX + "\n👤 ADMIN Sudo Login 👤" + Style.RESET_ALL)
print("\nPlease enter your credentials")
username = input("\nEnter your username: ")
password = getpass.getpass("Enter your password: ")

if not username or not password:
print(Fore.RED + "\nUsername and password are required." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
username, password = get_credentials()
if username is None or password is None:
continue

user = users_collection.find_one({'username': username})

if user:
stored_password = user['hashed_password']

# Verify the entered password with the stored password hash
if verify_password(stored_password, password):
# Throw error if user is logging in with ADMIN account
if username == "ADMIN":
print(Fore.GREEN +"\nUser Verified..." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return username
# Notify insufficient clearance
else:
print(Fore.RED + "\nYou do not have clearance to do this." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nADMIN user will be alerted." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nExiting..." + Style.RESET_ALL)
time.sleep(1)

# Log user's username and add it to audit log
log_action(username, f"Tried to access without clearance")
print_insufficient_clearance(username)
exit()

# Notify about incorrect password
else:
print(Fore.RED + "\nIncorrect password." + Style.RESET_ALL)
print_incorrect_password(username, attempts)
attempts += 1
time.sleep(2)
print(Fore.RED + f"\nRemaining attempts: {MAX_ATTEMPTS - attempts}" + Style.RESET_ALL)
log_action(username, "Failed attempted access via sudo")
time.sleep(2)
clear_screen()

# Notify about non-existing username
else:
print(Fore.RED + "\nUsername not found. Please try again." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
print_username_not_found()

print_max_attempts_reached()

def get_credentials():
username = input("\nEnter your username: ")
password = getpass.getpass("Enter your password: ")

if not username or not password:
print(Fore.RED + "\nUsername and password are required." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return None, None

return username, password

def print_insufficient_clearance(username):
print(Fore.RED + "\nYou do not have clearance to do this." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nADMIN user will be alerted." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nExiting..." + Style.RESET_ALL)
time.sleep(1)
log_action(username, "Tried to access without clearance")

def print_incorrect_password(username, attempts):
print(Fore.RED + "\nIncorrect password." + Style.RESET_ALL)
time.sleep(2)
print(Fore.RED + f"\nRemaining attempts: {MAX_ATTEMPTS - attempts}" + Style.RESET_ALL)
log_action(username, "Failed attempted access via sudo")
time.sleep(2)
clear_screen()

def print_username_not_found():
print(Fore.RED + "\nUsername not found. Please try again." + Style.RESET_ALL)
time.sleep(2)
clear_screen()

def print_max_attempts_reached():
print(Fore.RED + "\nMaximum login attempts reached" + Style.RESET_ALL)
time.sleep(1)
print("\nExiting...")
time.sleep(2)
exit()
exit()
86 changes: 46 additions & 40 deletions sudo_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,74 +13,80 @@
users_collection = db['users']

def sudo_user():

clear_screen()

# Continuous loop for sudo user authentication
attempts = 0

while attempts < MAX_ATTEMPTS:
print(Fore.LIGHTMAGENTA_EX + "\n👤 Sudo Login 👤" + Style.RESET_ALL)
print("\nPlease enter your credentials")
username = input("\nEnter your username: ")
password = getpass.getpass("Enter your password: ")

if not username or not password:
print(Fore.RED + "\nUsername and password are required." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
username, password = get_credentials()
if username is None or password is None:
continue

user = users_collection.find_one({'username': username})

if user:
stored_password = user['hashed_password']

# Verify the entered password with the stored password hash

if verify_password(stored_password, password):
user_level = user['level']

# Throw error if user is logging in with ADMIN account

if username == "ADMIN":
print("\nNot a valid username")
clear_screen()
return username

# Notify about user verification for high level users
elif user_level >= 2:
print(Fore.GREEN +"\nUser Verified..." + Style.RESET_ALL)
time.sleep(2)
clear_screen()
return username

# Notify insufficient clearance
else:
print(Fore.RED + "\nYou do not have clearance to do this." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nADMIN user will be alerted." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nExiting..." + Style.RESET_ALL)
time.sleep(1)

# Log user's username and add it to audit log
log_action(username, f"Tried to access without clearance")
print_insufficient_clearance(username)
exit()

# Notify about incorrect password
else:
print_incorrect_password(username, attempts)
attempts += 1
else:
print(Fore.RED + "\nIncorrect password." + Style.RESET_ALL)
attempts += 1
time.sleep(2)
print(Fore.RED + f"\nRemaining attempts: {MAX_ATTEMPTS - attempts}" + Style.RESET_ALL)
log_action(username, "Failed attempted access via sudo")
time.sleep(2)
clear_screen()

# Notify about non-existing username
else:
print(Fore.RED + "\nUsername not found. Please try again." + Style.RESET_ALL)
print_username_not_found()

print_max_attempts_reached()

def get_credentials():
username = input("\nEnter your username: ")
password = getpass.getpass("Enter your password: ")

if not username or not password:
print(Fore.RED + "\nUsername and password are required." + Style.RESET_ALL)
time.sleep(2)
clear_screen()

return None, None

return username, password

def print_insufficient_clearance(username):
print(Fore.RED + "\nYou do not have clearance to do this." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nADMIN user will be alerted." + Style.RESET_ALL)
time.sleep(1)
print(Fore.RED + "\nExiting..." + Style.RESET_ALL)
time.sleep(1)
log_action(username, "Tried to access without clearance")

def print_incorrect_password(username, attempts):
print(Fore.RED + "\nIncorrect password." + Style.RESET_ALL)
time.sleep(2)
print(Fore.RED + f"\nRemaining attempts: {MAX_ATTEMPTS - attempts}" + Style.RESET_ALL)
log_action(username, "Failed attempted access via sudo")
time.sleep(2)
clear_screen()

def print_username_not_found():
print(Fore.RED + "\nUsername not found. Please try again." + Style.RESET_ALL)
time.sleep(2)
clear_screen()

def print_max_attempts_reached():
print(Fore.RED + "\nMaximum login attempts reached" + Style.RESET_ALL)
time.sleep(1)
print("\nExiting...")
Expand Down
Loading
Loading