sprava uzivatelu
This commit is contained in:
+43
-10
@@ -4,10 +4,39 @@ from fastapi import HTTPException, Request, status
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from app.db.database import get_connection
|
||||
from app.db.migrations import run_migrations
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
|
||||
def _table_columns(con, table_name: str) -> set[str]:
|
||||
rows = con.execute(f"PRAGMA table_info({table_name})").fetchall()
|
||||
return {row["name"] for row in rows}
|
||||
|
||||
|
||||
def _user_select_columns(con) -> str:
|
||||
columns = _table_columns(con, "users")
|
||||
enabled_select = "COALESCE(is_enabled, 1) AS is_enabled" if "is_enabled" in columns else "1 AS is_enabled"
|
||||
return f"id, username, display_name, email, password_hash, role, is_active, {enabled_select}, created_at, updated_at"
|
||||
|
||||
|
||||
def mark_last_login(user_id: int) -> None:
|
||||
con = get_connection()
|
||||
columns = _table_columns(con, "users")
|
||||
if "last_login_at" in columns:
|
||||
con.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET last_login_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""",
|
||||
(user_id,),
|
||||
)
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
||||
|
||||
@@ -20,11 +49,12 @@ def verify_password(plain_password: str, password_hash: str) -> bool:
|
||||
|
||||
|
||||
def get_user_by_username(username: str) -> dict[str, Any] | None:
|
||||
run_migrations()
|
||||
con = get_connection()
|
||||
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_user_select_columns(con)}
|
||||
FROM users
|
||||
WHERE username = ?
|
||||
""",
|
||||
@@ -40,10 +70,11 @@ def find_or_create_oauth_user(username: str, display_name: str, email: str) -> d
|
||||
display_name = (display_name or username).strip() or username
|
||||
email = (email or "").strip()
|
||||
|
||||
run_migrations()
|
||||
con = get_connection()
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_user_select_columns(con)}
|
||||
FROM users
|
||||
WHERE username = ?
|
||||
""",
|
||||
@@ -82,8 +113,8 @@ def find_or_create_oauth_user(username: str, display_name: str, email: str) -> d
|
||||
con.commit()
|
||||
user_id = row["id"] if row else con.execute("SELECT last_insert_rowid() AS id").fetchone()["id"]
|
||||
user = con.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_user_select_columns(con)}
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
""",
|
||||
@@ -95,11 +126,12 @@ def find_or_create_oauth_user(username: str, display_name: str, email: str) -> d
|
||||
|
||||
|
||||
def get_user_by_id(user_id: int) -> dict[str, Any] | None:
|
||||
run_migrations()
|
||||
con = get_connection()
|
||||
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT id, username, display_name, email, password_hash, role, is_active, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_user_select_columns(con)}
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
""",
|
||||
@@ -113,12 +145,13 @@ def get_user_by_id(user_id: int) -> dict[str, Any] | None:
|
||||
def authenticate_user(username: str, password: str) -> dict[str, Any] | None:
|
||||
user = get_user_by_username(username.strip())
|
||||
|
||||
if not user or not user.get("is_active"):
|
||||
if not user or not user.get("is_active") or not user.get("is_enabled", 1):
|
||||
return None
|
||||
|
||||
if not verify_password(password, user.get("password_hash") or ""):
|
||||
return None
|
||||
|
||||
mark_last_login(int(user["id"]))
|
||||
return user
|
||||
|
||||
|
||||
@@ -134,7 +167,7 @@ def current_user(request: Request) -> dict[str, Any] | None:
|
||||
return None
|
||||
|
||||
user = get_user_by_id(user_id)
|
||||
if not user or not user.get("is_active"):
|
||||
if not user or not user.get("is_active") or not user.get("is_enabled", 1):
|
||||
request.session.pop("user_id", None)
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user