1
This commit is contained in:
1
backend/app/utils/__init__.py
Normal file
1
backend/app/utils/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
BIN
backend/app/utils/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
backend/app/utils/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
backend/app/utils/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
backend/app/utils/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/app/utils/__pycache__/auth.cpython-310.pyc
Normal file
BIN
backend/app/utils/__pycache__/auth.cpython-310.pyc
Normal file
Binary file not shown.
BIN
backend/app/utils/__pycache__/auth.cpython-311.pyc
Normal file
BIN
backend/app/utils/__pycache__/auth.cpython-311.pyc
Normal file
Binary file not shown.
BIN
backend/app/utils/__pycache__/response.cpython-310.pyc
Normal file
BIN
backend/app/utils/__pycache__/response.cpython-310.pyc
Normal file
Binary file not shown.
BIN
backend/app/utils/__pycache__/response.cpython-311.pyc
Normal file
BIN
backend/app/utils/__pycache__/response.cpython-311.pyc
Normal file
Binary file not shown.
25
backend/app/utils/auth.py
Normal file
25
backend/app/utils/auth.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from functools import wraps
|
||||
|
||||
from flask import jsonify
|
||||
from flask_jwt_extended import get_jwt, get_jwt_identity, verify_jwt_in_request
|
||||
|
||||
from app.models import User
|
||||
|
||||
|
||||
def current_user() -> User | None:
|
||||
identity = get_jwt_identity()
|
||||
if not identity:
|
||||
return None
|
||||
return User.query.get(int(identity))
|
||||
|
||||
|
||||
def admin_required(fn):
|
||||
@wraps(fn)
|
||||
def wrapper(*args, **kwargs):
|
||||
verify_jwt_in_request()
|
||||
claims = get_jwt()
|
||||
if not claims.get("is_admin", False):
|
||||
return jsonify({"code": 403, "message": "需要管理员权限"}), 403
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
9
backend/app/utils/response.py
Normal file
9
backend/app/utils/response.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
def ok(data=None, message="success"):
|
||||
return jsonify({"code": 0, "message": message, "data": data or {}})
|
||||
|
||||
|
||||
def fail(message="error", code=400, data=None):
|
||||
return jsonify({"code": code, "message": message, "data": data or {}}), code
|
||||
Reference in New Issue
Block a user