This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli.py
237 lines (189 loc) · 8.78 KB
/
cli.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
import django
from getpass import getpass
import sys
from rich.console import Console
from rich.table import Table
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "GateKeeper.settings")
django.setup()
from core.models import UserAccount
from GateKeeper.settings import DEFAULT_USER_PASSWORD
HELP = """ Short Command Description
/q quit Exit GateKeeper shell
/h help Get help
/va admin view Show admin list
/ca admin create Create new admin account
/vm mod view Show moderator list
/cm mod create Create new moderator account
/vu user view Show user list
/cu user create Create new user account
/rmid del account id [ID] Remove account by id
/rmu del account username [Username] Remove account by username"""
class UserManagement:
def __init__(self) -> None:
self.console = Console()
def get_input(self, field_name: str, message: str, min_input_len: int):
while True:
user_input = input(message)
if user_input is not None and len(user_input) >= min_input_len:
break
print(f"{field_name} is not acceptable! please try again.")
return user_input
def get_password(self):
while True:
password = getpass("Enter password: ")
repeated_password = getpass("Repeat password: ")
if password == repeated_password:
break
print("Passwords does not match! please try again.")
return password
def create_account(self, user_type):
username = self.get_input("Username", "Enter username: ", 4)
email = self.get_input("Email", "Enter email: ", 8)
first_name = self.get_input("First Name", "Enter First Name: ", 3)
last_name = self.get_input("Enter Last Name", "Enter Last Name: ", 0)
if user_type != UserAccount.type_user:
password = self.get_password()
else:
password = DEFAULT_USER_PASSWORD
try:
admin_user = UserAccount.objects.create_user(
username=username,
email=email,
first_name=first_name,
last_name=last_name,
account_type=user_type,
)
admin_user.is_staff = True
admin_user.is_superuser = True
admin_user.set_password(password)
admin_user.save()
except Exception as e:
print(f"an error occurred while creating the account\n{e}")
print(f"{user_type} user '{username}' created successfully.")
def delete_account_by_id(self, account_id):
try:
account = UserAccount.objects.get(id=account_id)
if not account:
self.console.print("Account not found!", style="bright_orange")
else:
self.print_user_list("Selected account", [account])
self.console.print("Are you sure you want to delete this account (Y/N)? ", style="bright_yellow",
end="")
user_choice = input()
if user_choice.lower() == "y" or user_choice.lower() == "yes":
account.delete()
self.console.print("Account has been deleted.")
except KeyboardInterrupt:
sys.exit()
except Exception as e:
print(f"an error occurred while creating the account\n{e}")
def delete_account_by_username(self, account_username):
try:
account = UserAccount.objects.get(username=account_username)
if not account:
self.console.print("Account not found!", style="bright_orange")
else:
self.print_user_list("Selected account", [account])
self.console.print("Are you sure you want to delete this account (Y/N)? ", style="bright_yellow",
end="")
user_choice = input()
if user_choice.lower() == "y" or user_choice.lower() == "yes":
account.delete()
self.console.print("Account has been deleted.")
except KeyboardInterrupt:
sys.exit()
except Exception as e:
print(f"an error occurred while creating the account\n{e}")
def create_admin(self):
self.create_account(UserAccount.type_admin)
def create_moderator(self):
self.create_account(UserAccount.type_moderator)
def create_user(self):
self.create_account(UserAccount.type_user)
def get_all_admins(self):
try:
admins = UserAccount.objects.all().filter(account_type=UserAccount.type_admin).order_by("id")
self.print_user_list("Admin List", admins)
except Exception as e:
print(f"an error occurred while doing the task\n{e}")
def get_all_mods(self):
try:
mods = UserAccount.objects.all().filter(account_type=UserAccount.type_moderator).order_by("id")
self.print_user_list("Moderator List", mods)
except Exception as e:
print(f"an error occurred while doing the task\n{e}")
def get_all_users(self):
try:
users = UserAccount.objects.all().filter(account_type=UserAccount.type_user).order_by("id")
self.print_user_list("User List", users)
except Exception as e:
print(f"an error occurred while doing the task\n{e}")
def print_user_list(self, title: str, query: list):
table = Table(title=title)
columns = ["ID", "Usernamee", "First Name", "Last Name", "Email", "Type", "Status"]
for column in columns:
table.add_column(column)
for row in query:
if row.is_active:
table.add_row(str(row.id),
row.username,
row.first_name,
row.last_name,
row.email,
row.type_admin,
'Active',
style='bright_green')
else:
table.add_row(str(row.id),
row.username,
row.first_name,
row.last_name,
row.email,
row.type_admin,
'Inactive',
style='bright_yellow')
self.console.print(table)
class Shell:
def __init__(self) -> None:
self.userManagement = UserManagement()
self.console = Console()
def start_shell(self):
try:
while True:
self.console.print("GareKeeper:~$ ", style='bright_green', end="")
user_input = input()
if user_input == "/q" or user_input == "quit":
sys.exit()
elif user_input == "/h" or user_input == "help":
# for HELP in HELPS:
self.console.print(HELP)
elif user_input == "/va" or user_input == "admin view":
self.userManagement.get_all_admins()
elif user_input == "/va" or user_input == "admin create":
self.userManagement.create_admin()
elif user_input == "/vm" or user_input == "mod view":
self.userManagement.get_all_mods()
elif user_input == "/cm" or user_input == "mod create":
self.userManagement.create_moderator()
elif user_input == "/vu" or user_input == "user view":
self.userManagement.get_all_users()
elif user_input == "/cu" or user_input == "user create":
self.userManagement.create_user()
elif "/rmid" in user_input or "del account id" in user_input:
self.userManagement.delete_account_by_id(int(user_input.split(" ")[-1]))
elif "/rmu" in user_input or "del account username" in user_input:
self.userManagement.delete_account_by_username(user_input.split(" ")[-1])
else:
pass
except KeyboardInterrupt:
sys.exit()
except EOFError:
sys.exit()
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "createadmin":
userManagement = UserManagement()
userManagement.create_admin()
elif len(sys.argv) > 1 and sys.argv[1] == "shell":
shell = Shell()
shell.start_shell()