-
Notifications
You must be signed in to change notification settings - Fork 5
/
user_management.py
159 lines (130 loc) · 5.68 KB
/
user_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import time
from colorama import Fore, Style
from common_functions import clear_screen, hash_password, get_mongodb_uri
from register import register
from pymongo import MongoClient
from sudo_user_login import SudoAdmin
# Connect to MongoDB
uri = get_mongodb_uri()
client = MongoClient(uri)
db = client['animal_rescue']
users_collection = db['users']
def user_management():
SudoAdmin(users_collection.database).login()
# Continuous loop for user management options
while True:
clear_screen()
print(Fore.YELLOW + "\nUser Management" + Style.RESET_ALL)
print("\n1. Update user information")
print("2. Register new user")
print("3. Delete user")
print("4. Back to ADMIN dashboard")
option = input("\nPlease select an option: ")
# Perform actions based on user input
if option == '1':
update_user_information()
elif option == '2':
register()
elif option == '3':
delete_user()
elif option == '4':
print("\nReturning to ADMIN dashboard...")
time.sleep(2)
return
else:
print(Fore.RED + "\nInvalid option. Please try again." + Style.RESET_ALL)
time.sleep(2)
def reset_user_password(username):
# Check if the user is ADMIN
SudoAdmin(users_collection.database).login()
# Change password if the user exists and is not ADMIN
user = users_collection.find_one({'username': username})
if user and username != "ADMIN":
new_password = "password"
# Generate salt and hash password
hashed_password = hash_password(new_password)
# Update user's password and salt
users_collection.update_one({'username': username},
{'$set': {'hashed_password': hashed_password}})
print(Fore.GREEN + f"\nPassword for user '{username}' reset successfully!" + Style.RESET_ALL)
elif username == "ADMIN":
print(Fore.RED + "\nYou cannot change the password for the ADMIN user." + Style.RESET_ALL)
else:
print(Fore.RED + f"\nUser '{username}' not found." + Style.RESET_ALL)
time.sleep(2)
def get_username():
return input("\nEnter the username to update information: ")
def get_user(username):
return users_collection.find_one({'username': username})
def print_user_info(username, user):
clear_screen()
print(Fore.CYAN + "\nCurrent user information:" + Style.RESET_ALL)
print(Fore.GREEN + f"\nUsername: {username}" + Style.RESET_ALL)
print(Fore.GREEN + f"User Level: {user['level']}" + Style.RESET_ALL)
def get_option():
print("\nSelect the information you want to update:")
print("\n1. Username")
print("2. User Level")
print("3. Reset Password")
print("4. Cancel")
return input("\nEnter your choice: ")
def update_username(username):
new_username = input("Enter the new username: ")
if not users_collection.find_one({'username': new_username}):
users_collection.update_one({'username': username}, {'$set': {'username': new_username}})
print(Fore.GREEN + f"\nUsername updated successfully to '{new_username}'!" + Style.RESET_ALL)
else:
print(Fore.RED + f"\nUsername '{new_username}' already exists. Please choose a different username." + Style.RESET_ALL)
def update_user_level(username):
new_level = input("Enter the new user level: ")
if new_level.isdigit(): # Check if the input is a valid integer
new_level = int(new_level)
users_collection.update_one({'username': username}, {'$set': {'level': new_level}})
print(Fore.GREEN + f"\nUser level updated successfully for '{username}'!" + Style.RESET_ALL)
else:
print(Fore.RED + "\nInvalid user level. Please enter a valid level." + Style.RESET_ALL)
def update_user_information():
SudoAdmin(users_collection.database).login()
username = get_username()
user = get_user(username)
if user and username != "ADMIN":
print_user_info(username, user)
option = get_option()
if option == '1':
update_username(username)
elif option == '2':
update_user_level(username)
elif option == '3':
time.sleep(2)
reset_user_password(username)
elif option == '4':
print("\nOperation canceled.")
else:
print(Fore.RED + "\nInvalid option. Please try again." + Style.RESET_ALL)
elif username == "ADMIN":
print(Fore.RED + "\nYou cannot update information for the ADMIN user." + Style.RESET_ALL)
else:
print(Fore.RED + f"\nUser '{username}' not found." + Style.RESET_ALL)
time.sleep(2)
def delete_user():
SudoAdmin(users_collection.database).login()
# Prompt for username to be deleted
username = input("\nEnter the username to delete: ")
confirm_delete = input("Are you sure you want to delete this user? (y/n) ")
# Throw error if user tries to delete ADMIN user
if username == "ADMIN":
print("\nThe ADMIN user cannot be modified.")
time.sleep(2)
clear_screen()
delete_user()
# Process user's choice and perform deletion accordingly
else:
user = users_collection.find_one({'username': username})
if user and confirm_delete.lower() == 'y':
users_collection.delete_one({'username': username})
print(Fore.GREEN + f"\nUser '{username}' deleted successfully!" + Style.RESET_ALL)
elif user and confirm_delete.lower() == 'n':
print(Fore.RED + "User has not been deleted!" + Style.RESET_ALL)
else:
print(Fore.RED + f"\nUser '{username}' not found." + Style.RESET_ALL)
time.sleep(2)