Skip to content

Commit

Permalink
[v1.2.2] Merge pull request #35 from KageRyo/develop
Browse files Browse the repository at this point in the history
Update to RyoURL v1.2.2
  • Loading branch information
KageRyo authored Aug 8, 2024
2 parents f7c6d73 + 6736348 commit 97553a4
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions RyoURL/shortURL/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions RyoURL/shortURL/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .auth import auth_router
from .short import url_router
from .auth_api import auth_router
from .short_api import url_router
File renamed without changes.
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
1 change: 0 additions & 1 deletion RyoURL/shortURL/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ djangorestframework-simplejwt
python-dotenv
requests

# 測試
pytest
pytest-django

# 監控與除錯
sentry-sdk
django-silk
Expand Down

0 comments on commit 97553a4

Please sign in to comment.