|
6 | 6 | @date:2025/4/14 19:25 |
7 | 7 | @desc: |
8 | 8 | """ |
| 9 | +import json |
| 10 | + |
9 | 11 | from django.core.cache import cache |
10 | 12 | from django.db.models import QuerySet |
11 | 13 | from django.utils.translation import gettext_lazy as _ |
|
17 | 19 | from common.auth.authentication import has_permissions |
18 | 20 | from common.constants.cache_version import Cache_Version |
19 | 21 | from common.constants.permission_constants import PermissionConstants, Permission, Group, Operate, RoleConstants |
| 22 | +from common.exception.app_exception import AppApiException |
20 | 23 | from common.log.log import log |
21 | 24 | from common.result import result |
22 | 25 | from common.utils.common import query_params_to_single_dict |
| 26 | +from common.utils.rsa_util import decrypt |
23 | 27 | from maxkb.const import CONFIG |
24 | 28 | from models_provider.api.model import DefaultModelResponse |
25 | 29 | from tools.serializers.tool import encryption |
@@ -299,7 +303,11 @@ class RePasswordView(APIView): |
299 | 303 | get_operation_object=lambda r, k: {'name': r.user.username}, |
300 | 304 | get_details=get_re_password_details) |
301 | 305 | def post(self, request: Request): |
302 | | - serializer_obj = RePasswordSerializer(data=request.data) |
| 306 | + request_data = request.data |
| 307 | + if request_data.get("encrypted", False): |
| 308 | + request_data['password'] = decrypt(request_data.get('password')) |
| 309 | + request_data['re_password'] = decrypt(request_data.get('re_password')) |
| 310 | + serializer_obj = RePasswordSerializer(data=request_data) |
303 | 311 | return result.success(serializer_obj.reset_password()) |
304 | 312 |
|
305 | 313 |
|
@@ -371,7 +379,18 @@ class ResetCurrentUserPasswordView(APIView): |
371 | 379 | @has_permissions(PermissionConstants.CHANGE_PASSWORD, RoleConstants.ADMIN, RoleConstants.USER, |
372 | 380 | RoleConstants.WORKSPACE_MANAGE) |
373 | 381 | def post(self, request: Request): |
374 | | - serializer_obj = ResetCurrentUserPassword(data=request.data) |
| 382 | + request_data = request.data |
| 383 | + encrypted_data = request_data.get("encryptedData", "") |
| 384 | + if encrypted_data: |
| 385 | + try: |
| 386 | + decrypted_raw = decrypt(encrypted_data) |
| 387 | + # decrypt 可能返回非 JSON 字符串,防护解析异常 |
| 388 | + decrypted_data = json.loads(decrypted_raw) if decrypted_raw else {} |
| 389 | + if isinstance(decrypted_data, dict): |
| 390 | + request_data = decrypted_data |
| 391 | + except Exception as e: |
| 392 | + raise AppApiException(500, _("Invalid encrypted data")) |
| 393 | + serializer_obj = ResetCurrentUserPassword(data=request_data) |
375 | 394 | if serializer_obj.reset_password(request.user.id): |
376 | 395 | version, get_key = Cache_Version.TOKEN.value |
377 | 396 | cache.delete(get_key(token=request.auth), version=version) |
|
0 commit comments