Skip to content

Commit e336392

Browse files
perf: API Permission Error Message Optimization
1 parent e2e4459 commit e336392

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

backend/apps/system/schemas/permission.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from common.core.db import engine
1414
from apps.system.schemas.system_schema import UserInfoDTO
1515

16+
from common.utils.locale import I18n
17+
i18n = I18n()
1618

1719
class SqlbotPermission(BaseModel):
1820
role: Optional[list[str]] = None
@@ -49,6 +51,7 @@ def decorator(func):
4951
@wraps(func)
5052
async def wrapper(*args, **kwargs):
5153
request = RequestContext.get_request()
54+
5255
current_user: UserInfoDTO = getattr(request.state, 'current_user', None)
5356
if not current_user:
5457
raise HTTPException(
@@ -57,6 +60,8 @@ async def wrapper(*args, **kwargs):
5760
)
5861
current_oid = current_user.oid
5962

63+
trans = i18n(request)
64+
6065
if current_user.isAdmin and not permission.type:
6166
return await func(*args, **kwargs)
6267
role_list = permission.role
@@ -65,9 +70,11 @@ async def wrapper(*args, **kwargs):
6570

6671
if role_list:
6772
if 'admin' in role_list and not current_user.isAdmin:
68-
raise Exception('no permission to execute, only for admin')
73+
#raise Exception('no permission to execute, only for admin')
74+
raise Exception(trans('i18n_permission.only_admin'))
6975
if 'ws_admin' in role_list and current_user.weight == 0 and not current_user.isAdmin:
70-
raise Exception('no permission to execute, only for workspace admin')
76+
#raise Exception('no permission to execute, only for workspace admin')
77+
raise Exception(trans('i18n_permission.only_ws_admin'))
7178
if not resource_type:
7279
return await func(*args, **kwargs)
7380
if keyExpression:
@@ -81,7 +88,8 @@ async def wrapper(*args, **kwargs):
8188
value = bound_args.args[index]
8289
if await check_ws_permission(current_oid, resource_type, value):
8390
return await func(*args, **kwargs)
84-
raise Exception('no permission to execute or resource do not exist!')
91+
#raise Exception('no permission to execute or resource do not exist!')
92+
raise Exception(trans('i18n_permission.permission_resource_limit'))
8593

8694
parts = keyExpression.split('.')
8795
if not bound_args.arguments.get(parts[0]):
@@ -91,7 +99,7 @@ async def wrapper(*args, **kwargs):
9199
value = getattr(value, part)
92100
if await check_ws_permission(current_oid, resource_type, value):
93101
return await func(*args, **kwargs)
94-
raise Exception('no permission to execute or resource do not exist!')
102+
raise Exception(trans('i18n_permission.permission_resource_limit'))
95103

96104
return await func(*args, **kwargs)
97105

backend/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"only_admin": "Only administrators are allowed to call!",
4242
"no_permission": "No permission to call {url}{msg}",
4343
"authenticate_invalid": "Authentication invalid【{msg}】",
44-
"token_expired": "Token expired"
44+
"token_expired": "Token expired",
45+
"only_ws_admin": "Workspace administrator access required!",
46+
"permission_resource_limit": "Access denied or resource not found!"
4547
},
4648
"i18n_llm": {
4749
"validate_error": "Validation failed [{msg}]",

backend/locales/ko-KR.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"only_admin": "관리자만 호출할 수 있습니다!",
4242
"no_permission": "{url}{msg} 호출 권한이 없습니다",
4343
"authenticate_invalid": "인증 무효 【{msg}】",
44-
"token_expired": "토큰이 만료됨"
44+
"token_expired": "토큰이 만료됨",
45+
"only_ws_admin": "워크스페이스 관리자만 사용 가능합니다!",
46+
"permission_resource_limit": "권한이 없거나 리소스를 찾을 수 없습니다!"
4547
},
4648
"i18n_llm": {
4749
"validate_error": "검증 실패 [{msg}]",

backend/locales/zh-CN.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"only_admin": "仅支持管理员调用!",
4242
"no_permission": "无权调用{url}{msg}",
4343
"authenticate_invalid": "认证无效【{msg}】",
44-
"token_expired": "Token 已过期"
44+
"token_expired": "Token 已过期",
45+
"only_ws_admin": "仅支持工作空间管理员调用!",
46+
"permission_resource_limit": "没有操作权限或资源不存在!"
4547
},
4648
"i18n_llm": {
4749
"validate_error": "校验失败[{msg}]",

0 commit comments

Comments
 (0)