forked from thaisonho/csc10008---Socket-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
68 lines (60 loc) · 2.47 KB
/
users.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
import sqlite3
import hashlib
import os
class UserManager:
def __init__(self, db_file='users.db'):
self.db_file = db_file
self._initialize_database()
def _initialize_database(self):
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
storage_dir TEXT NOT NULL
)
""")
conn.commit()
def _hash_password(self, password):
return hashlib.sha256(password.encode()).hexdigest()
def add_user(self, username, password):
# Tự động tạo đường dẫn lưu trữ dựa trên tên người dùng
storage_dir = os.path.join('user_storage', username)
hashed_password = self._hash_password(password)
try:
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO users (username, password, storage_dir)
VALUES (?, ?, ?)
""", (username, hashed_password, storage_dir))
conn.commit()
os.makedirs(storage_dir, exist_ok=True)
return True
except sqlite3.IntegrityError:
return False # Người dùng đã tồn tại
def verify_user(self, username, password):
hashed_password = self._hash_password(password)
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM users WHERE username = ? AND password = ?
""", (username, hashed_password))
return cursor.fetchone() is not None
def user_exists(self, username):
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT * FROM users WHERE username = ?
""", (username,))
return cursor.fetchone() is not None
def get_user_storage(self, username):
with sqlite3.connect(self.db_file) as conn:
cursor = conn.cursor()
cursor.execute("""
SELECT storage_dir FROM users WHERE username = ?
""", (username,))
result = cursor.fetchone()
return result[0] if result else None