forked from ugonfor/secure-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
674 lines (538 loc) Β· 21.8 KB
/
app.py
File metadata and controls
674 lines (538 loc) Β· 21.8 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import eventlet
eventlet.monkey_patch()
import sqlite3
import uuid
import bcrypt
from flask import Flask, render_template, request, redirect, url_for, session, flash, g
from flask_socketio import SocketIO, send, emit, join_room
from datetime import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
DATABASE = 'market.db'
socketio = SocketIO(app, async_mode='eventlet')
def log_admin_action(admin_id, action):
db = get_db()
cursor = db.cursor()
log_id = str(uuid.uuid4())
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute(
"INSERT INTO admin_logs (id, admin_id, action, timestamp) VALUES (?, ?, ?, ?)",
(log_id, admin_id, action, timestamp)
)
db.commit()
# λ°μ΄ν°λ² μ΄μ€ μ°κ²° κ΄λ¦¬: μμ²λ§λ€ μ°κ²° μμ± ν μ¬μ©, μ’
λ£ μ close
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row # κ²°κ³Όλ₯Ό dictμ²λΌ μ¬μ©νκΈ° μν¨
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
# ν
μ΄λΈ μμ± (μ΅μ΄ μ€ν μμλ§)
def init_db():
with app.app_context():
db = get_db()
cursor = db.cursor()
# μ¬μ©μ ν
μ΄λΈ μμ±
cursor.execute("""
CREATE TABLE IF NOT EXISTS user (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
bio TEXT,
is_active BOOLEAN DEFAULT 1
)
""")
# μν ν
μ΄λΈ μμ±
cursor.execute("""
CREATE TABLE IF NOT EXISTS product (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT NOT NULL,
price TEXT NOT NULL,
seller_id TEXT NOT NULL
)
""")
# μ κ³ ν
μ΄λΈ μμ±
cursor.execute("""
CREATE TABLE IF NOT EXISTS report (
id TEXT PRIMARY KEY,
reporter_id TEXT NOT NULL,
target_id TEXT NOT NULL,
reason TEXT NOT NULL
)
""")
# κ΄λ¦¬μ λ‘κ·Έ ν
μ΄λΈ μμ±
cursor.execute("""
CREATE TABLE IF NOT EXISTS admin_logs (
id TEXT PRIMARY KEY,
admin_id TEXT NOT NULL,
action TEXT NOT NULL,
timestamp TEXT NOT NULL
)
""")
# balance μ»¬λΌ μΆκ°
try:
cursor.execute("ALTER TABLE user ADD COLUMN balance INTEGER DEFAULT 10000")
except sqlite3.OperationalError:
pass # μ΄λ―Έ 컬λΌμ΄ μ‘΄μ¬νλ©΄ 무μ
# is_admin μ»¬λΌ μΆκ°
try:
cursor.execute("ALTER TABLE user ADD COLUMN is_admin BOOLEAN DEFAULT 0")
except sqlite3.OperationalError:
pass
db.commit()
# κΈ°λ³Έ λΌμ°νΈ
@app.route('/')
def index():
if 'user_id' in session:
return redirect(url_for('dashboard'))
return render_template('index.html')
# νμκ°μ
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE username = ?", (username,))
if cursor.fetchone():
flash('μ΄λ―Έ μ‘΄μ¬νλ μ¬μ©μμ
λλ€.')
return redirect(url_for('register'))
hashed_pw = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
user_id = str(uuid.uuid4())
cursor.execute("INSERT INTO user (id, username, password) VALUES (?, ?, ?)",
(user_id, username, hashed_pw))
db.commit()
flash('νμκ°μ
μ΄ μλ£λμμ΅λλ€.')
return redirect(url_for('login'))
return render_template('register.html')
# λ‘κ·ΈμΈ
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE username = ?", (username,))
user = cursor.fetchone()
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
if not user['is_active']:
flash('ν΄λ©΄ μνμ κ³μ μ
λλ€. κ΄λ¦¬μμκ² λ¬ΈμνμΈμ.')
return redirect(url_for('login'))
session['user_id'] = user['id']
session['is_admin'] = int(user['is_admin'])
# κ΄λ¦¬μμΌ κ²½μ° κ΄λ¦¬μ νμ΄μ§λ‘ μ΄λ
if int(user['is_admin']) == 1:
flash('κ΄λ¦¬μ λ‘κ·ΈμΈ μ±κ³΅!')
return redirect(url_for('admin_panel'))
flash('λ‘κ·ΈμΈ μ±κ³΅!')
return redirect(url_for('dashboard'))
else:
flash('μμ΄λ λλ λΉλ°λ²νΈκ° μ¬λ°λ₯΄μ§ μμ΅λλ€.')
return redirect(url_for('login'))
return render_template('login.html')
# λ‘κ·Έμμ
@app.route('/logout')
def logout():
session.pop('user_id', None)
flash('λ‘κ·Έμμλμμ΅λλ€.')
return redirect(url_for('index'))
# λμ보λ: μ¬μ©μ μ 보μ μ 체 μν 리μ€νΈ νμ
@app.route('/dashboard')
def dashboard():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
# νμ¬ μ¬μ©μ
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
current_user = cursor.fetchone()
# π κ²μ ν€μλ μ²λ¦¬
keyword = request.args.get('q', '')
if keyword:
cursor.execute("""
SELECT product.*, user.username AS seller_name
FROM product
JOIN user ON product.seller_id = user.id
WHERE product.title LIKE ?
""", (f'%{keyword}%',))
else:
cursor.execute("""
SELECT product.*, user.username AS seller_name
FROM product
JOIN user ON product.seller_id = user.id
""")
all_products = cursor.fetchall()
return render_template('dashboard.html', user=current_user, products=all_products, keyword=keyword)
# νλ‘ν νμ΄μ§: bio μ
λ°μ΄νΈ κ°λ₯
@app.route('/profile', methods=['GET', 'POST'])
def profile():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
# μ¬μ©μ μ 보 μ‘°ν
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
user = cursor.fetchone()
if request.method == 'POST':
bio = request.form.get('bio', '')
current_pw = request.form.get('current_password', '')
new_pw = request.form.get('new_password', '')
# λΉλ°λ²νΈ λ³κ²½ μμ²μ΄ μμ κ²½μ°
if current_pw and new_pw:
if bcrypt.checkpw(current_pw.encode('utf-8'), user['password']):
hashed_new_pw = bcrypt.hashpw(new_pw.encode('utf-8'), bcrypt.gensalt())
cursor.execute("UPDATE user SET password = ? WHERE id = ?", (hashed_new_pw, user['id']))
flash('λΉλ°λ²νΈκ° λ³κ²½λμμ΅λλ€.')
else:
flash('νμ¬ λΉλ°λ²νΈκ° μ¬λ°λ₯΄μ§ μμ΅λλ€.')
return redirect(url_for('profile'))
# μκ°κΈ μ
λ°μ΄νΈ
cursor.execute("UPDATE user SET bio = ? WHERE id = ?", (bio, user['id']))
db.commit()
flash('νλ‘νμ΄ μ
λ°μ΄νΈλμμ΅λλ€.')
return redirect(url_for('profile'))
return render_template('profile.html', user=user)
@app.route('/product/new', methods=['GET', 'POST'])
def new_product():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
price = request.form['price']
db = get_db()
cursor = db.cursor()
product_id = str(uuid.uuid4())
cursor.execute(
"INSERT INTO product (id, title, description, price, seller_id) VALUES (?, ?, ?, ?, ?)",
(product_id, title, description, price, session['user_id'])
)
db.commit()
return render_template('new_product.html', message='μνμ΄ λ±λ‘λμμ΅λλ€.')
@app.route('/product/<product_id>')
def view_product(product_id):
db = get_db()
cursor = db.cursor()
# μν μ 보 μ‘°ν
cursor.execute("SELECT * FROM product WHERE id = ?", (product_id,))
product = cursor.fetchone()
if not product:
flash('μνμ μ°Ύμ μ μμ΅λλ€.')
return redirect(url_for('dashboard'))
# νλ§€μ μ 보 μ‘°ν
cursor.execute("SELECT * FROM user WHERE id = ?", (product['seller_id'],))
seller = cursor.fetchone()
return render_template('view_product.html', product=product, seller=seller)
@app.route('/report/<target_id>', methods=['GET', 'POST'])
def report(target_id):
if 'user_id' not in session:
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
if request.method == 'POST':
reason = request.form['reason']
report_id = str(uuid.uuid4())
cursor.execute(
"INSERT INTO report (id, reporter_id, target_id, reason) VALUES (?, ?, ?, ?)",
(report_id, session['user_id'], target_id, reason)
)
db.commit()
# μ¬μ©μ μ¬λΆ νμΈ
cursor.execute("SELECT * FROM user WHERE id = ?", (target_id,))
user = cursor.fetchone()
if user:
# λ°λ‘ ν΄λ©΄ μ²λ¦¬
cursor.execute("UPDATE user SET is_active = 0 WHERE id = ?", (target_id,))
db.commit()
flash('ν΄λΉ μ¬μ©μκ° μ κ³ λ‘ μΈν΄ ν΄λ©΄ μ²λ¦¬λμμ΅λλ€.')
else:
# μνμ μ¦μ μμ
cursor.execute("DELETE FROM product WHERE id = ?", (target_id,))
db.commit()
flash('ν΄λΉ μνμ μ κ³ λμ΄ μμ λμμ΅λλ€.')
return redirect(url_for('dashboard'))
return render_template('report.html', target_id=target_id)
@app.route('/reports')
def view_reports():
db = get_db()
cursor = db.cursor()
cursor.execute("""
SELECT report.*,
u1.username AS reporter_name,
u2.username AS target_name
FROM report
LEFT JOIN user u1 ON report.reporter_id = u1.id
LEFT JOIN user u2 ON report.target_id = u2.id
""")
reports = cursor.fetchall()
return render_template('view_reports.html', reports=reports)
@app.route('/my-products')
def my_products():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM product WHERE seller_id = ?", (session['user_id'],))
products = cursor.fetchall()
return render_template('my_products.html', products=products)
@app.route('/product/edit/<product_id>', methods=['GET', 'POST'])
def edit_product(product_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
# μν κ°μ Έμ€κΈ°
cursor.execute("SELECT * FROM product WHERE id = ?", (product_id,))
product = cursor.fetchone()
# νλ§€μ λ³ΈμΈ νμΈ
if not product or product['seller_id'] != session['user_id']:
flash('μμ κΆνμ΄ μμ΅λλ€.')
return redirect(url_for('dashboard'))
if request.method == 'POST':
title = request.form['title']
description = request.form['description']
price = request.form['price']
cursor.execute("""
UPDATE product
SET title = ?, description = ?, price = ?
WHERE id = ?
""", (title, description, price, product_id))
db.commit()
flash('μνμ΄ μμ λμμ΅λλ€.')
return redirect(url_for('my_products'))
return render_template('edit_product.html', product=product)
@app.route('/product/delete/<product_id>')
def delete_product(product_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
# μν κ°μ Έμ€κΈ°
cursor.execute("SELECT * FROM product WHERE id = ?", (product_id,))
product = cursor.fetchone()
if not product:
flash('μνμ μ°Ύμ μ μμ΅λλ€.')
return redirect(url_for('admin_panel'))
# νμ¬ μ¬μ©μ μ‘°ν
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
current_user = cursor.fetchone()
# κ΄λ¦¬μ λλ λ³ΈμΈλ§ μμ κ°λ₯
if int(current_user['is_admin']) != 1 and product['seller_id'] != session['user_id']:
flash('μμ κΆνμ΄ μμ΅λλ€.')
return redirect(url_for('dashboard'))
# μμ μ²λ¦¬
cursor.execute("DELETE FROM product WHERE id = ?", (product_id,))
db.commit()
if int(current_user['is_admin']) == 1:
log_admin_action(current_user['id'], f"μν {product_id} μμ ")
flash('μνμ΄ μμ λμμ΅λλ€.')
# κ΄λ¦¬μλ©΄ κ΄λ¦¬μ νμ΄μ§λ‘, μΌλ° μ¬μ©μλ λ΄ μν λͺ©λ‘μΌλ‘ μ΄λ
if int(current_user['is_admin']) == 1:
return redirect(url_for('admin_panel'))
else:
return redirect(url_for('my_products'))
@app.route('/user/<user_id>')
def view_user(user_id):
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE id = ?", (user_id,))
user = cursor.fetchone()
if not user:
flash('μ¬μ©μλ₯Ό μ°Ύμ μ μμ΅λλ€.')
return redirect(url_for('dashboard'))
return render_template('view_user.html', user=user)
@app.route('/chat/<receiver_id>')
def chat(receiver_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
if session['user_id'] == receiver_id:
flash('μκΈ° μμ κ³Όλ μ±ν
ν μ μμ΅λλ€.')
return redirect(url_for('dashboard'))
room_id = '_'.join(sorted([session['user_id'], receiver_id]))
return redirect(url_for('chat_room', room_id=room_id))
@app.route('/chat/room/<room_id>')
def chat_room(room_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
ids = room_id.split('_')
user_id = session['user_id']
if user_id in ids:
receiver_id = ids[1] if ids[0] == user_id else ids[0]
else:
flash('μλͺ»λ μ±ν
λ°© μ κ·Όμ
λλ€.')
return redirect(url_for('dashboard'))
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE id = ?", (receiver_id,))
receiver = cursor.fetchone()
if not receiver:
flash('λν μλλ₯Ό μ°Ύμ μ μμ΅λλ€.')
return redirect(url_for('dashboard'))
print("DEBUG - receiver:", receiver['id'], receiver['username'])
return render_template("chat_private.html", room_id=room_id, receiver=receiver)
@socketio.on('join')
def handle_join(data):
print(f"join room: {data['room']}")
join_room(data['room'])
@socketio.on('private_message')
def handle_private_message(data):
print(f"msg to {data['room']}: {data['message']}")
emit('private_message', {'message': data['message']}, room=data['room'])
@app.route('/chat')
def global_chat():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
user = cursor.fetchone()
return render_template('chat.html', user=user)
# μ€μκ° μ±ν
: ν΄λΌμ΄μΈνΈκ° λ©μμ§λ₯Ό 보λ΄λ©΄ μ 체 λΈλ‘λμΊμ€νΈ
@socketio.on('send_message')
def handle_send_message_event(data):
data['message_id'] = str(uuid.uuid4())
send(data, broadcast=True)
@app.route('/transfer', methods=['GET', 'POST'])
def transfer():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
if request.method == 'POST':
receiver_username = request.form['receiver_username']
amount = int(request.form['amount'])
# νμ¬ μ μ μ 보
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
sender = cursor.fetchone()
# λ°λ μ μ μ 보
cursor.execute("SELECT * FROM user WHERE username = ?", (receiver_username,))
receiver = cursor.fetchone()
if not receiver:
flash('λ°λ μ¬μ©μκ° μ‘΄μ¬νμ§ μμ΅λλ€.')
return redirect(url_for('transfer'))
if sender['id'] == receiver['id']:
flash('μκΈ° μμ μκ²λ μ‘κΈν μ μμ΅λλ€.')
return redirect(url_for('transfer'))
if sender['balance'] < amount:
flash('μμ‘μ΄ λΆμ‘±ν©λλ€.')
return redirect(url_for('transfer'))
# μ‘κΈ μ²λ¦¬
cursor.execute("UPDATE user SET balance = balance - ? WHERE id = ?", (amount, sender['id']))
cursor.execute("UPDATE user SET balance = balance + ? WHERE id = ?", (amount, receiver['id']))
db.commit()
flash(f"{receiver['username']}λμκ² {amount}μμ μ‘κΈνμ΅λλ€.")
return redirect(url_for('dashboard'))
return render_template('transfer.html')
@app.route('/admin')
def admin_panel():
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
current_user = cursor.fetchone()
# λͺ
ννκ² μ μλ‘ μ²΄ν¬
if int(current_user['is_admin']) != 1:
flash('κ΄λ¦¬μλ§ μ κ·Ό κ°λ₯ν νμ΄μ§μ
λλ€.')
return redirect(url_for('dashboard'))
cursor.execute("SELECT * FROM user")
users = cursor.fetchall()
cursor.execute("SELECT * FROM product")
products = cursor.fetchall()
cursor.execute("""
SELECT log.*, u.username AS admin_name
FROM admin_logs log
JOIN user u ON log.admin_id = u.id
ORDER BY log.timestamp DESC
LIMIT 20
""")
logs = cursor.fetchall()
return render_template('admin.html', users=users, products=products, logs=logs)
@app.route('/admin/delete_user/<user_id>')
def delete_user(user_id):
db = get_db()
cursor = db.cursor()
cursor.execute("DELETE FROM user WHERE id = ?", (user_id,))
db.commit()
log_admin_action(session['user_id'], f"μ¬μ©μ {user_id} μμ ")
flash("μ¬μ©μκ° μμ λμμ΅λλ€.")
return redirect(url_for('admin_panel'))
# μ: adminμ΄λΌλ usernameμ κ°μ§ μ μ λ₯Ό κ΄λ¦¬μ μ§μ
# μλ μ½λ app.pyμ μΌμμ μΌλ‘ μΆκ° (μ΄ν μμ ν΄λ λ¨)
@app.route('/make-admin/<username>')
def make_admin(username):
db = get_db()
cursor = db.cursor()
cursor.execute("UPDATE user SET is_admin = 1 WHERE username = ?", (username,))
db.commit()
return f"{username} κ³μ μ΄ κ΄λ¦¬μ κΆνμ κ°κ² λμμ΅λλ€."
@app.route('/admin/update_balance/<user_id>', methods=['POST'])
def update_balance(user_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
# νμ¬ λ‘κ·ΈμΈν μ μ κ° κ΄λ¦¬μ νμΈ
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
admin_user = cursor.fetchone()
if not admin_user or int(admin_user['is_admin']) != 1:
flash('κ΄λ¦¬μ κΆνμ΄ νμν©λλ€.')
return redirect(url_for('dashboard'))
try:
new_balance = int(request.form['new_balance'])
cursor.execute("UPDATE user SET balance = ? WHERE id = ?", (new_balance, user_id))
db.commit()
log_admin_action(session['user_id'], f"{user_id}μ ν¬μΈνΈλ₯Ό {new_balance}μμΌλ‘ μμ ")
flash('ν¬μΈνΈκ° μμ λμμ΅λλ€.')
except:
flash('μλͺ»λ μμ²μ
λλ€.')
return redirect(url_for('admin_panel'))
@app.route('/admin/toggle_admin/<user_id>')
def toggle_admin(user_id):
if 'user_id' not in session:
flash('λ‘κ·ΈμΈμ΄ νμν©λλ€.')
return redirect(url_for('login'))
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user WHERE id = ?", (session['user_id'],))
admin_user = cursor.fetchone()
if not admin_user or int(admin_user['is_admin']) != 1:
flash('κ΄λ¦¬μ κΆνμ΄ νμν©λλ€.')
return redirect(url_for('dashboard'))
cursor.execute("SELECT * FROM user WHERE id = ?", (user_id,))
target_user = cursor.fetchone()
if not target_user:
flash("ν΄λΉ μ¬μ©μλ₯Ό μ°Ύμ μ μμ΅λλ€.")
return redirect(url_for('admin_panel'))
# κΆν ν κΈ
new_status = 0 if target_user['is_admin'] else 1
cursor.execute("UPDATE user SET is_admin = ? WHERE id = ?", (new_status, user_id))
db.commit()
# λ‘κ·Έ κΈ°λ‘
status_text = 'λΆμ¬' if new_status == 1 else 'ν΄μ '
log_admin_action(session['user_id'], f"{user_id}μ λν΄ κ΄λ¦¬μ κΆν {status_text}")
flash("κΆνμ΄ λ³κ²½λμμ΅λλ€.")
return redirect(url_for('admin_panel'))
if __name__ == '__main__':
init_db() # μ± μ»¨ν
μ€νΈ λ΄μμ ν
μ΄λΈ μμ±
socketio.run(app, host='0.0.0.0', port=5000, debug=True)