-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_repository.py
More file actions
325 lines (293 loc) · 11 KB
/
auth_repository.py
File metadata and controls
325 lines (293 loc) · 11 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
"""
SQLite User Repository Implementation
=====================================
Implements user data persistence using SQLite.
"""
import sqlite3
from auth_core import (
AdminUser,
IUserRepository,
PlayerUser,
Session,
User,
UserRole,
UserStatus,
)
class SQLiteUserRepository(IUserRepository):
"""SQLite implementation of user repository."""
def __init__(self, db_path: str = "tournament.db"):
self.db_path = db_path
self._init_database()
def _get_connection(self):
"""Get database connection with row factory."""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
def _init_database(self):
"""Initialize authentication tables."""
with self._get_connection() as conn:
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS admin_users (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
email TEXT,
status TEXT DEFAULT 'active',
created_at TEXT NOT NULL,
last_login TEXT
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS player_users (
id TEXT PRIMARY KEY,
player_id TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
date_of_birth TEXT NOT NULL,
status TEXT DEFAULT 'active',
created_at TEXT NOT NULL,
last_login TEXT,
FOREIGN KEY (player_id) REFERENCES players(id)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
role TEXT NOT NULL,
ip_address TEXT,
created_at TEXT NOT NULL,
expires_at TEXT NOT NULL
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS audit_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
admin_id TEXT NOT NULL,
action TEXT NOT NULL,
target_user_id TEXT,
details TEXT,
timestamp TEXT NOT NULL,
ip_address TEXT
)
""")
conn.commit()
def save_admin(self, admin: AdminUser) -> None:
"""Save admin user."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR REPLACE INTO admin_users
(id, username, password_hash, email, status, created_at, last_login)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
admin.id,
admin.username,
admin.password_hash,
admin.email,
admin.status.value,
admin.created_at,
admin.last_login,
),
)
conn.commit()
def save_player_user(self, player: PlayerUser) -> None:
"""Save player user."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR REPLACE INTO player_users
(id, player_id, name, date_of_birth, status, created_at, last_login)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
player.id,
player.player_id,
player.name,
player.date_of_birth,
player.status.value,
player.created_at,
player.last_login,
),
)
conn.commit()
def get_admin_by_username(self, username: str) -> AdminUser | None:
"""Get admin by username."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM admin_users WHERE username = ?", (username,)
).fetchone()
if row:
return AdminUser(
id=row["id"],
username=row["username"],
password_hash=row["password_hash"],
email=row["email"],
role=UserRole.ADMIN,
status=UserStatus(row["status"]),
created_at=row["created_at"],
last_login=row["last_login"],
)
return None
def get_player_by_name_dob(self, name: str, dob: str) -> PlayerUser | None:
"""Get player by name and date of birth."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM player_users WHERE name = ? AND date_of_birth = ?",
(name, dob),
).fetchone()
if row:
return PlayerUser(
id=row["id"],
player_id=row["player_id"],
name=row["name"],
date_of_birth=row["date_of_birth"],
role=UserRole.PLAYER,
status=UserStatus(row["status"]),
created_at=row["created_at"],
last_login=row["last_login"],
)
return None
def get_player_user_by_player_id(self, player_id: str) -> PlayerUser | None:
"""Get player user by player ID."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM player_users WHERE player_id = ?", (player_id,)
).fetchone()
if row:
return PlayerUser(
id=row["id"],
player_id=row["player_id"],
name=row["name"],
date_of_birth=row["date_of_birth"],
role=UserRole.PLAYER,
status=UserStatus(row["status"]),
created_at=row["created_at"],
last_login=row["last_login"],
)
return None
def get_user_by_id(self, user_id: str) -> User | None:
"""Get any user by ID."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM admin_users WHERE id = ?", (user_id,)
).fetchone()
if row:
return AdminUser(
id=row["id"],
username=row["username"],
password_hash=row["password_hash"],
email=row["email"],
role=UserRole.ADMIN,
status=UserStatus(row["status"]),
created_at=row["created_at"],
last_login=row["last_login"],
)
row = conn.execute(
"SELECT * FROM player_users WHERE id = ?", (user_id,)
).fetchone()
if row:
return PlayerUser(
id=row["id"],
player_id=row["player_id"],
name=row["name"],
date_of_birth=row["date_of_birth"],
role=UserRole.PLAYER,
status=UserStatus(row["status"]),
created_at=row["created_at"],
last_login=row["last_login"],
)
return None
def update_user_status(self, user_id: str, status: UserStatus) -> None:
"""Update user account status."""
with self._get_connection() as conn:
conn.execute(
"UPDATE admin_users SET status = ? WHERE id = ?",
(status.value, user_id),
)
conn.execute(
"UPDATE player_users SET status = ? WHERE id = ?",
(status.value, user_id),
)
conn.commit()
def update_admin_password(self, admin_id: str, new_password_hash: str) -> None:
"""Update admin password hash."""
with self._get_connection() as conn:
conn.execute(
"UPDATE admin_users SET password_hash = ? WHERE id = ?",
(new_password_hash, admin_id),
)
conn.commit()
def save_session(self, session: Session) -> None:
"""Save session."""
with self._get_connection() as conn:
conn.execute(
"""
INSERT OR REPLACE INTO sessions
(session_id, user_id, role, ip_address, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?)
""",
(
session.session_id,
session.user_id,
session.role.value,
session.ip_address,
session.created_at,
session.expires_at,
),
)
conn.commit()
def get_session(self, session_id: str) -> Session | None:
"""Get session by ID."""
with self._get_connection() as conn:
row = conn.execute(
"SELECT * FROM sessions WHERE session_id = ?", (session_id,)
).fetchone()
if row:
return Session(
session_id=row["session_id"],
user_id=row["user_id"],
role=UserRole(row["role"]),
ip_address=row["ip_address"],
created_at=row["created_at"],
expires_at=row["expires_at"],
)
return None
def delete_session(self, session_id: str) -> None:
"""Delete session."""
with self._get_connection() as conn:
conn.execute("DELETE FROM sessions WHERE session_id = ?", (session_id,))
conn.commit()
def update_last_login(self, user_id: str, timestamp: str) -> None:
"""Update last login timestamp."""
with self._get_connection() as conn:
conn.execute(
"UPDATE admin_users SET last_login = ? WHERE id = ?",
(timestamp, user_id),
)
conn.execute(
"UPDATE player_users SET last_login = ? WHERE id = ?",
(timestamp, user_id),
)
conn.commit()
def log_admin_action(
self,
admin_id: str,
action: str,
target_user_id: str = None,
details: str = None,
ip_address: str = None,
) -> None:
"""Log an admin action for audit trail."""
from tournament_core import now_iso
with self._get_connection() as conn:
conn.execute(
"""
INSERT INTO audit_log
(admin_id, action, target_user_id, details, timestamp, ip_address)
VALUES (?, ?, ?, ?, ?, ?)
""",
(admin_id, action, target_user_id, details, now_iso(), ip_address),
)
conn.commit()