Skip to content

Commit

Permalink
refactor: Add check_auth function
Browse files Browse the repository at this point in the history
  • Loading branch information
KageRyo committed Aug 8, 2024
1 parent edb0678 commit b575849
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions RyoURL/shortURL/apis/short.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,27 @@ class UrlSchema(Schema):
class ErrorSchema(Schema):
message: str

# 身分驗證的函式
def check_auth(request, required_type=None):
if not hasattr(request, 'auth'):
request.auth = None
if not request.auth or (required_type is not None and request.auth.user_type != required_type):
return False
return True

# 權限檢查裝飾器
def user_auth_required(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
if not hasattr(request, 'auth'):
request.auth = None
if not request.auth:
if not check_auth(request):
return api.create_response(request, {"message": "您必須登入才能執行此操作。"}, status=403)
return func(request, *args, **kwargs)
return wrapper

def admin_auth_required(func):
@wraps(func)
def wrapper(request, *args, **kwargs):
if not hasattr(request, 'auth'):
request.auth = None
if not request.auth or request.auth.user_type != 2:
if not check_auth(request, required_type=2):
return api.create_response(request, {"message": "您必須是管理員才能執行此操作。"}, status=403)
return func(request, *args, **kwargs)
return wrapper
Expand Down

0 comments on commit b575849

Please sign in to comment.