From c3c6967ed8501ddb88889c98f33c98d64ac2c8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=B1=E5=87=8C?= Date: Thu, 8 Aug 2024 12:10:44 +0800 Subject: [PATCH 1/4] init: rm unuse library out --- requirements.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 66998f3..9e62925 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,10 +9,6 @@ djangorestframework-simplejwt python-dotenv requests -# 測試 -pytest -pytest-django - # 監控與除錯 sentry-sdk django-silk From edb0678aa56b150cb4c0d575259e885e0076cca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=B1=E5=87=8C?= Date: Thu, 8 Aug 2024 12:12:11 +0800 Subject: [PATCH 2/4] modify: remove unused library --- RyoURL/shortURL/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/RyoURL/shortURL/views.py b/RyoURL/shortURL/views.py index f7bb4d1..52e89b3 100644 --- a/RyoURL/shortURL/views.py +++ b/RyoURL/shortURL/views.py @@ -6,7 +6,6 @@ from django.utils import timezone from django.http import HttpResponse, HttpResponseRedirect from django.db.models import F -from typing import Optional from .models import Url From b5758495f70c376280e0fd6b7c73435dc43006d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=B1=E5=87=8C?= Date: Thu, 8 Aug 2024 16:11:15 +0800 Subject: [PATCH 3/4] refactor: Add check_auth function --- RyoURL/shortURL/apis/short.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/RyoURL/shortURL/apis/short.py b/RyoURL/shortURL/apis/short.py index 50bdb8d..56dba48 100644 --- a/RyoURL/shortURL/apis/short.py +++ b/RyoURL/shortURL/apis/short.py @@ -27,13 +27,19 @@ 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 @@ -41,9 +47,7 @@ def wrapper(request, *args, **kwargs): 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 From 6736348f8cbec33bb2df9c253982718841308693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=B1=E5=87=8C?= Date: Thu, 8 Aug 2024 16:15:21 +0800 Subject: [PATCH 4/4] refactor: rename api data --- RyoURL/shortURL/api.py | 4 ++-- RyoURL/shortURL/apis/__init__.py | 4 ++-- RyoURL/shortURL/apis/{auth.py => auth_api.py} | 0 RyoURL/shortURL/apis/{short.py => short_api.py} | 0 4 files changed, 4 insertions(+), 4 deletions(-) rename RyoURL/shortURL/apis/{auth.py => auth_api.py} (100%) rename RyoURL/shortURL/apis/{short.py => short_api.py} (100%) diff --git a/RyoURL/shortURL/api.py b/RyoURL/shortURL/api.py index 7c292ce..7c2b876 100644 --- a/RyoURL/shortURL/api.py +++ b/RyoURL/shortURL/api.py @@ -4,8 +4,8 @@ from pydantic import AnyUrl -from .apis.auth import auth_router, JWTAuth -from .apis.short import url_router +from .apis.auth_api import auth_router, JWTAuth +from .apis.short_api import url_router # 自定義 JSON 編碼器和渲染器 class CustomJSONEncoder(DjangoJSONEncoder): diff --git a/RyoURL/shortURL/apis/__init__.py b/RyoURL/shortURL/apis/__init__.py index b41a2b7..a6a0806 100644 --- a/RyoURL/shortURL/apis/__init__.py +++ b/RyoURL/shortURL/apis/__init__.py @@ -1,2 +1,2 @@ -from .auth import auth_router -from .short import url_router \ No newline at end of file +from .auth_api import auth_router +from .short_api import url_router \ No newline at end of file diff --git a/RyoURL/shortURL/apis/auth.py b/RyoURL/shortURL/apis/auth_api.py similarity index 100% rename from RyoURL/shortURL/apis/auth.py rename to RyoURL/shortURL/apis/auth_api.py diff --git a/RyoURL/shortURL/apis/short.py b/RyoURL/shortURL/apis/short_api.py similarity index 100% rename from RyoURL/shortURL/apis/short.py rename to RyoURL/shortURL/apis/short_api.py