Refactor codebase
This commit is contained in:
18
app/auth.py
18
app/auth.py
@@ -1,5 +1,5 @@
|
||||
from functools import wraps
|
||||
from flask import session, redirect, url_for, request
|
||||
from flask import session, redirect, url_for, request, jsonify
|
||||
from app.db import query_one
|
||||
|
||||
|
||||
@@ -19,3 +19,19 @@ def get_current_user():
|
||||
if user_id is None:
|
||||
return None
|
||||
return query_one("SELECT * FROM users WHERE id = %s", (user_id,))
|
||||
|
||||
|
||||
def privacy_guard(f):
|
||||
"""Decorator for API endpoints that take a user_id parameter.
|
||||
|
||||
If the requested user is private and is not the current session user,
|
||||
returns an empty JSON response instead of the actual data.
|
||||
"""
|
||||
@wraps(f)
|
||||
def decorated_function(user_id, *args, **kwargs):
|
||||
if user_id != session.get("user_id"):
|
||||
target = query_one("SELECT is_private FROM users WHERE id = %s", (user_id,))
|
||||
if target and target["is_private"]:
|
||||
return jsonify({})
|
||||
return f(user_id, *args, **kwargs)
|
||||
return decorated_function
|
||||
|
||||
Reference in New Issue
Block a user