Skip to content
Open
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
140 changes: 136 additions & 4 deletions Backend user system/lgoin system.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,148 @@ def login():


def adminlogin():
global passwordattempts
login_username = input("What is your Username: ")
login_password = input("What is your Password: ")

if login_username != ADMIN_USER:
while login_username != ADMIN_USER:
print("Invalid Username")
login_username = input("What is your Username: ")

return False
if login_password != Admin_password:
while login_password != Admin_password:
print("Invalid Password")
return False
passwordattempts = passwordattempts+1
print("Remaining Attempts:",passwordattempts)
if passwordattempts >= 3:
print("You are not an Admin Nice try")
main()
login_password = input("What is your Password: ")
Comment on lines +140 to +152
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical logic bug: return False executes inside the while loop.

The return False on line 144 is indented inside the while loop, causing the function to immediately return after the first invalid username attempt instead of allowing retry. Similarly, the password validation loop structure is problematic.

Additionally:

  • Line 148 shows "Remaining Attempts" but displays the count going up (0→1→2), which is confusing.
  • Line 151 uses recursive main() call which can cause stack overflow with repeated failures.

Apply this diff to fix the logic:

 def adminlogin():
     global passwordattempts
     login_username = input("What is your Username: ")
-    login_password = input("What is your Password: ")
 
     while login_username != ADMIN_USER:
         print("Invalid Username")
         login_username = input("What is your Username: ")
 
-        return False
+    login_password = input("What is your Password: ")
+    
     while login_password != Admin_password:
         print("Invalid Password")
-        passwordattempts = passwordattempts+1
-        print("Remaining Attempts:",passwordattempts)
+        passwordattempts += 1
+        print(f"Failed Attempts: {passwordattempts}/3")
         if passwordattempts >= 3:
             print("You are not an Admin Nice try")
-            main()
+            return False
         login_password = input("What is your Password: ")
 
     print("Welcome Admin")
     return True

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Backend user system/lgoin system.py around lines 140 to 152, the username
loop currently returns False inside the while loop which causes an immediate
exit after the first invalid entry and the password loop increments attempts and
calls main() recursively; fix by removing the in-loop return so the username
while loop keeps prompting until a match (or exits after a controlled max
attempts), rework the password loop to decrement a remaining_attempts counter
(or calculate remaining as max_attempts - attempts) so the printed "Remaining
Attempts" counts down, and replace the recursive main() call with an iterative
control flow (e.g., break/continue to outer loop or return to caller) to avoid
recursion and possible stack overflow; ensure variable names (ADMIN_USER vs
Admin_password) are used consistently and move any final return False to after
loops when authentication fails.


print("Welcome Admin")
return True

def load_users():
"""Load users from userdata.txt file"""
users = {}
try:
with open('userdata.txt', 'r') as f:
for line in f:
line = line.strip()
if ':' in line:
username, password = line.split(':')
users[username] = password
except FileNotFoundError:
# File doesn't exist yet, that's okay
pass
return users


def save_users(users):
"""Save users to userdata.txt file"""
with open('userdata.txt', 'w') as f:
for username, password in users.items():
f.write(f"{username}:{password}\n")
Comment on lines +173 to +177
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Security: Passwords are stored in plaintext.

Both user credentials and this save function store passwords in plaintext. For any production use, passwords should be hashed using a secure algorithm like bcrypt or argon2.

Example using hashlib (for learning; prefer bcrypt for production):

import hashlib

def hash_password(password):
    return hashlib.sha256(password.encode()).hexdigest()

def save_users(users):
    """Save users to userdata.txt file (passwords should already be hashed)"""
    with open('userdata.txt', 'w') as f:
        for username, password_hash in users.items():
            f.write(f"{username}:{password_hash}\n")

Note: This would require updating signup() and login() to hash passwords during registration and verification.

🤖 Prompt for AI Agents
In Backend user system/lgoin system.py around lines 173 to 177, the save_users
function writes plaintext passwords to userdata.txt; change it to store password
hashes instead: update the signup flow to hash new passwords with a secure
algorithm (preferably bcrypt or argon2) and store the resulting hash in the
users dict, change login to verify incoming passwords against the stored hash
using the corresponding verify function, and modify save_users to write
username:password_hash lines (and load_users to expect hashes). Ensure you add
the chosen hashing library to dependencies and handle errors on read/write
consistently.



def adminpanel():
users = load_users() # Load users at the start

Rabbit.sleep(1)
print("\n=== Admin Panel ===")
print("1: Change a Users Username")
print("2: See all users")
print("3: Delete a User account")
print("4: Exit Admin Panel")

choice = input("\nEnter your choice (1-4): ").strip()

if choice == "1":
change_username()
elif choice == "2":
see_all_users()
elif choice == "3":
delete_user()
elif choice == "4":
print("Exiting admin panel...")
main()
else:
print("Invalid choice. Please try again.")

# Loop back to admin panel
adminpanel()
Comment on lines +180 to +205
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Control flow issues and unused variable.

  1. Line 181: users is loaded but never used (as flagged by static analysis).
  2. Lines 200 + 205: When user chooses "4", main() is called, but after it returns, adminpanel() is called again unconditionally on line 205, causing unexpected re-entry.
  3. Using recursion for the menu loop can cause stack overflow with extended use.

Apply this diff to fix:

 def adminpanel():
-    users = load_users()  # Load users at the start
-
-    Rabbit.sleep(1)
-    print("\n=== Admin Panel ===")
-    print("1: Change a Users Username")
-    print("2: See all users")
-    print("3: Delete a User account")
-    print("4: Exit Admin Panel")
-
-    choice = input("\nEnter your choice (1-4): ").strip()
-
-    if choice == "1":
-        change_username()
-    elif choice == "2":
-        see_all_users()
-    elif choice == "3":
-        delete_user()
-    elif choice == "4":
-        print("Exiting admin panel...")
-        main()
-    else:
-        print("Invalid choice. Please try again.")
-
-    # Loop back to admin panel
-    adminpanel()
+    while True:
+        Rabbit.sleep(1)
+        print("\n=== Admin Panel ===")
+        print("1: Change a Users Username")
+        print("2: See all users")
+        print("3: Delete a User account")
+        print("4: Exit Admin Panel")
+
+        choice = input("\nEnter your choice (1-4): ").strip()
+
+        if choice == "1":
+            change_username()
+        elif choice == "2":
+            see_all_users()
+        elif choice == "3":
+            delete_user()
+        elif choice == "4":
+            print("Exiting admin panel...")
+            return  # Return to caller instead of calling main()
+        else:
+            print("Invalid choice. Please try again.")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def adminpanel():
users = load_users() # Load users at the start
Rabbit.sleep(1)
print("\n=== Admin Panel ===")
print("1: Change a Users Username")
print("2: See all users")
print("3: Delete a User account")
print("4: Exit Admin Panel")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == "1":
change_username()
elif choice == "2":
see_all_users()
elif choice == "3":
delete_user()
elif choice == "4":
print("Exiting admin panel...")
main()
else:
print("Invalid choice. Please try again.")
# Loop back to admin panel
adminpanel()
def adminpanel():
while True:
Rabbit.sleep(1)
print("\n=== Admin Panel ===")
print("1: Change a Users Username")
print("2: See all users")
print("3: Delete a User account")
print("4: Exit Admin Panel")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == "1":
change_username()
elif choice == "2":
see_all_users()
elif choice == "3":
delete_user()
elif choice == "4":
print("Exiting admin panel...")
return # Return to caller instead of calling main()
else:
print("Invalid choice. Please try again.")
🧰 Tools
🪛 Ruff (0.14.8)

181-181: Local variable users is assigned to but never used

Remove assignment to unused variable users

(F841)

🤖 Prompt for AI Agents
In Backend user system/lgoin system.py around lines 180 to 205, remove the
unused users = load_users() or use it where needed, replace the recursive
adminpanel() calls with an explicit while True loop for the menu, handle choices
inside the loop, and on choice "4" break/return so you do not call main() and
then re-enter the admin panel; ensure there is no unconditional recursive
adminpanel() at the end and avoid deep recursion to prevent stack overflow.



def change_username():
"""Change a user's username"""
users = load_users() # Reload users

print("\n--- Change Username ---")

if not users:
print("No users in the system.")
return

old_username = input("Enter the current username: ").strip()

if old_username not in users:
print(f"User '{old_username}' not found.")
return

new_username = input("Enter the new username: ").strip()

if new_username in users:
print(f"Username '{new_username}' already exists.")
return
Comment on lines +224 to +228
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing username validation.

The new username isn't validated for length (max 12 characters) or empty strings, unlike signup() which enforces these constraints. This allows creating usernames that bypass signup rules.

     new_username = input("Enter the new username: ").strip()
 
+    if not new_username:
+        print("Username cannot be empty.")
+        return
+
+    if len(new_username) > 12:
+        print("Username too long (max 12 characters).")
+        return
+
     if new_username in users:
         print(f"Username '{new_username}' already exists.")
         return
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
new_username = input("Enter the new username: ").strip()
if new_username in users:
print(f"Username '{new_username}' already exists.")
return
new_username = input("Enter the new username: ").strip()
if not new_username:
print("Username cannot be empty.")
return
if len(new_username) > 12:
print("Username too long (max 12 characters).")
return
if new_username in users:
print(f"Username '{new_username}' already exists.")
return
🤖 Prompt for AI Agents
In Backend user system/lgoin system.py around lines 224 to 228, the new_username
input lacks the same validation as signup; add checks after stripping to ensure
the username is not empty and does not exceed 12 characters (e.g., if not
new_username or len(new_username) > 12: print an explanatory message and
return), and keep the existing duplicate check; mirror the exact validation
rules used by signup to prevent bypassing signup constraints.


# Transfer user data to new username
users[new_username] = users.pop(old_username)
save_users(users) # Save changes
print(f"Successfully changed username from '{old_username}' to '{new_username}'")


def see_all_users():
"""Display all users in the system"""
users = load_users() # Reload users

print("\n--- All Users ---")

if not users:
print("No users in the system.")
return

print(f"Total users: {len(users)}")
for i, username in enumerate(users.keys(), 1):
print(f"{i}. {username}")


def delete_user():
"""Delete a user account"""
users = load_users() # Reload users

print("\n--- Delete User Account ---")

if not users:
print("No users in the system.")
return

username = input("Enter the username to delete: ").strip()

if username not in users:
print(f"User '{username}' not found.")
return

confirm = input(f"Are you sure you want to delete '{username}'? (yes/no): ").strip().lower()

if confirm == "yes":
del users[username]
save_users(users) # Save changes
print(f"User '{username}' has been deleted successfully.")
else:
print("Deletion cancelled.")

def main():
global passwordattempts
print("1. Signup")
print("2. Login")
choice = input("Choose: ")
Expand All @@ -163,9 +290,14 @@ def main():
elif choice == login_secret:
if adminlogin():
print("Admin access granted!")
global passwordattempts
passwordattempts = 0
adminpanel()
else:
print("Admin login failed.")
main()
Comment on lines 291 to +297
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Duplicate global declaration and recursive calls.

  1. Line 293 is a redundant global passwordattempts declaration (already declared at line 277) and placing it inside a nested block causes a SyntaxWarning.
  2. Line 297 uses recursion for retrying, which can cause stack overflow with repeated failures.

Consider using a loop-based approach for the main menu:

 def main():
-    global passwordattempts
-    print("1. Signup")
-    print("2. Login")
-    choice = input("Choose: ")
-
-    if choice == "1":
-        signup()
-    elif choice == "2":
-        if login():
-            print("Welcome! You are now logged in.")
-            # Continue with your program
-        else:
-            print("Login failed. Please try again.")
-    elif choice == login_secret:
-        if adminlogin():
-            print("Admin access granted!")
-            global passwordattempts
-            passwordattempts = 0
-            adminpanel()
-        else:
-            main()
-    else:
-        print("Invalid choice")
+    global passwordattempts
+    while True:
+        print("1. Signup")
+        print("2. Login")
+        choice = input("Choose: ")
+
+        if choice == "1":
+            signup()
+        elif choice == "2":
+            if login():
+                print("Welcome! You are now logged in.")
+                # Continue with your program
+                break
+            else:
+                print("Login failed. Please try again.")
+        elif choice == login_secret:
+            if adminlogin():
+                print("Admin access granted!")
+                passwordattempts = 0
+                adminpanel()
+            # Loop continues if adminlogin fails
+        else:
+            print("Invalid choice")

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Backend user system/lgoin system.py around lines 291 to 297, remove the
redundant in-block "global passwordattempts" (it's already declared at line 277)
and avoid the recursive call to main() for retries; instead ensure the global is
declared once at the top of the function or module and refactor control flow to
use a loop-based retry mechanism (e.g., while/for loop) that resets
passwordattempts on successful adminlogin() and calls adminpanel() directly
without recursion, and on failure continues the loop or exits gracefully to
prevent stack growth.

else:
print("Invalid choice")



main()