-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
401 lines (264 loc) · 12.3 KB
/
Copy pathadmin.py
File metadata and controls
401 lines (264 loc) · 12.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import db
import auth
from utils import print_separator, format_timestamp, print_warning, pad_line
def ban_user(username, reason="No reason provided"):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_admin"] == 1:
return False, "Cannot ban an admin user."
current_user = auth.get_current_user()
if target["id"] == current_user["id"]:
return False, "You cannot ban yourself."
db.update_user(target["id"], is_banned=1, ban_reason=reason)
db.delete_user_sessions(target["id"])
db.admin_log(current_user["id"], "ban_user", target["id"], f"Banned @{username} for reason: {reason}")
return True, f"User @{username} has been banned."
def unban_user(username):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_banned"] == 0:
return False, "User is not banned."
db.update_user(target["id"], is_banned=0, ban_reason="")
current_user = auth.get_current_user()
db.admin_log(current_user["id"], "unban_user", target["id"], f"Unbanned @{username}")
return True, f"User @{username} has been unbanned."
def make_admin(username):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_admin"] == 1:
return False, "User is already an admin."
if target["is_banned"] == 1:
return False, "Cannot make a banned user an admin."
db.update_user(target["id"], is_admin=1)
current_user = auth.get_current_user()
db.admin_log(current_user["id"], "make_admin", target["id"], f"@{username} is now an admin")
return True, f"User @{username} is now an admin."
def remove_admin(username):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_admin"] == 0:
return False, "User is not an admin."
if target["username"] == "admin":
return False, "Cannot remove admin privileges from the main admin account."
current_user = auth.get_current_user()
if target["id"] == current_user["id"]:
return False, "You cannot remove your own admin privileges."
db.update_user(target["id"], is_admin=0)
db.admin_log(current_user["id"], "remove_admin", target["id"], f"@{username} is no longer an admin")
return True, f"User @{username} is no longer an admin."
def delete_post(post_id):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_post(post_id)
if target is None:
return False, "Post not found."
db.delete_post(post_id)
current_user = auth.get_current_user()
db.admin_log(current_user["id"], "delete_post", target["id"], f"Deleted post #{post_id}")
return True, f"Post #{post_id} has been deleted."
def verify_user(username):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_verified"] == 1:
return False, "User is already verified."
db.update_user(target["id"], is_verified=1)
current_user = auth.get_current_user()
db.admin_log(current_user["id"], "verify_user", target["id"], f"Verified @{username}")
return True, f"User @{username} has been verified."
def unverify_user(username):
if not auth.is_admin():
return False, "Admin privileges required."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
if target["is_verified"] == 0:
return False, "User is not verified."
db.update_user(target["id"], is_verified=0)
current_user = auth.get_current_user()
db.admin_log(current_user["id"], "unverify_user", target["id"], f"Unverified @{username}")
return True, f"User @{username} has been unverified."
def report_post(target, reason):
if not auth.is_logged():
return False, "You must be logged in."
post = db.get_post(target)
if post is None:
return False, "Post not found."
user = auth.get_current_user()
if db.already_reported(user["id"], "post", target):
return False, "You have already reported this post."
db.create_report(user["id"], "post", target, reason)
return True, "The post has been reported to the admins."
def report_comment(target, reason):
if not auth.is_logged():
return False, "You must be logged in."
comment = db.get_comment(target)
if comment is None:
return False, "Comment not found."
user = auth.get_current_user()
if db.already_reported(user["id"], "comment", target):
return False, "You have already reported this comment."
db.create_report(user["id"], "comment", target, reason)
return True, "The comment has been reported to the admins."
def report_user(username, reason):
if not auth.is_logged():
return False, "You must be logged in."
target = db.get_user_by_username(username)
if target is None:
return False, "User not found."
user = auth.get_current_user()
if db.already_reported(user["id"], "user", target["id"]):
return False, "You have already reported this user."
db.create_report(user["id"], "user", target["id"], reason)
return True, "The user has been reported to the admins."
def view_report(report_id):
if not auth.is_admin():
return False, "Admin privileges required."
report = db.get_report(report_id)
if report is None:
return False, "Report not found."
return True, report
def resolve_report(report_id):
if not auth.is_admin():
return False, "Admin privileges required."
report = db.get_report(report_id)
if report is None:
return False, "Report not found."
if report["status"] != "pending":
return False, "Report has already been resolved."
user = auth.get_current_user()
db.update_report(report_id, "resolved", user["id"])
db.admin_log(user["id"], "resolve_report", f"Report ID = {report_id}", f"Resolved report #{report_id}")
return True, f"Report #{report_id} has been resolved."
def dismiss_report(report_id):
if not auth.is_admin():
return False, "Admin privileges required."
report = db.get_report(report_id)
if report is None:
return False, "Report not found."
if report["status"] != "pending":
return False, "Report has already been resolved."
user = auth.get_current_user()
db.update_report(report_id, "dismissed", user["id"])
db.admin_log(user["id"], "dismiss_report", f"Report ID = {report_id}", f"Dismissed report #{report_id}")
return True, f"Report #{report_id} has been dismissed."
def get_stats():
if not auth.is_admin():
return None
stats = db.get_statistics()
return stats
def get_admin_logs(page=1):
if not auth.is_admin():
return False, "Admin privileges required."
offset = (page - 1) * 50
logs = db.get_admin_logs(limit=50, offset=offset)
return True, logs
def print_banner_admin():
if not auth.is_admin():
return
stats = get_stats()
WIDTH = 80
print()
print("╔" + "═" * WIDTH + "╗")
title_pad = (WIDTH - len(" ADMIN DASHBOARD ")) // 2
print("║" + " " * title_pad + " ADMIN DASHBOARD " + " " * (WIDTH - len(" ADMIN DASHBOARD ") - title_pad) + "║")
print("╠" + "═" * WIDTH + "╣")
print("║" + pad_line(" STATISTICS", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
print("║" + pad_line(f" Total Users: {stats["user_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Banned Users: {stats["banned_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Total Posts: {stats["post_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Total Likes: {stats["like_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Total Follows: {stats["follow_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Total Comments: {stats["comment_count"]}", WIDTH) + "║")
print("║" + pad_line(f" Total Reposts: {stats["repost_count"]}", WIDTH) + "║")
pending_reports = db.get_pending_reports_count()
print("║" + pad_line(f" Pending Reports: {pending_reports}", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
print("╠" + "═" * WIDTH + "╣")
print("║" + pad_line(" ADMIN COMMANDS", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
commands = [
"admin -> View platform statistics",
"admin ban <username> [reason] -> Ban a user",
"admin unban <username> -> Unban a user",
"admin deletepost <post_id> -> Delete a post",
"admin makeadmin <username> -> Grant admin privileges to a user",
"admin removeadmin <username> -> Revoke admin privileges from a user",
"admin verify <username> -> Verify a user",
"admin unverify <username> -> Unverify a user",
"admin reports [<status>] [<page>] -> View reports",
"admin resolve <report_id> -> Resolve a report",
"admin dismiss <report_id> -> Dismiss a report",
"admin logs [<page>] -> View admin logs"
]
for command in commands:
print("║" + pad_line(f" {command}", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
print("╚" + "═" * WIDTH + "╝")
def print_reports(status, page):
if not auth.is_admin():
return
offset = (page - 1) * 20
reports = db.get_reports(status=status, limit=20, offset=offset)
if not reports:
print_warning("No reports found.")
return
count = db.get_pending_reports_count()
WIDTH = 80
print()
print("╔" + "═" * WIDTH + "╗")
title_l = f" REPORTS - {status.upper()} (Page {page})"
title_pad = (WIDTH - len(title_l)) // 2
print("║" + " " * title_pad + title_l + " " * (WIDTH - len(title_l) - title_pad) + "║")
print("╠" + "═" * WIDTH + "╣")
if status == "pending":
print("║" + pad_line(f" Total Pending Reports: {count}", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
for report in reports:
format_time = format_timestamp(report["created"])
if report["type"] == "post":
target_info = f"Post #{report['target_id']}"
elif report["type"] == "comment":
target_info = f"Comment #{report['target_id']}"
elif report["type"] == "user":
user = db.get_user_by_id(report["target_id"])
target_info = f"User @{user['username']}"
print("║" + pad_line(f" Report ID: #{report['id']} | Type: {report['type']} | {format_time}", WIDTH) + "║")
print("║" + pad_line(f" Target: {target_info}", WIDTH) + "║")
print("║" + pad_line(f" Reported by: @{report['reporter_username']}", WIDTH) + "║")
print("║" + pad_line(f" Reason: {report['reason']}", WIDTH) + "║")
print("║" + pad_line(f" Status: {report['status']}", WIDTH) + "║")
print("║" + " " * WIDTH + "║")
print("╚" + "═" * WIDTH + "╝")
def print_admin_logs(page):
if not auth.is_admin():
return
success, logs = get_admin_logs(page)
if not success:
print_warning(f"Error: {logs}")
return
if not logs:
print_warning("No admin logs found.")
return
print(f"\n Admin Logs (Page {page}):")
for log in logs:
timestamp = format_timestamp(log["created"])
print(f"\n [{timestamp}] Admin @{log['admin_username']} performed action: {log['action']}")
if log["details"]:
print(f" Details: {log['details']}")
print_separator()