-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: User profile returns permission role information #3005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| from rest_framework import serializers | ||
| import uuid_utils.compat as uuid | ||
| from common.constants.exception_code_constants import ExceptionCodeConstants | ||
| from common.constants.permission_constants import RoleConstants | ||
| from common.constants.permission_constants import RoleConstants, Auth | ||
| from common.utils.common import valid_license, password_encrypt | ||
| from users.models import User | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
@@ -39,18 +39,20 @@ class CreateUserSerializer(serializers.Serializer): | |
|
|
||
| class UserProfileSerializer(serializers.Serializer): | ||
| @staticmethod | ||
| def profile(user: User): | ||
| def profile(user: User, auth: Auth): | ||
| """ | ||
| 获取用户详情 | ||
| :param user: 用户对象 | ||
| :return: | ||
| 获取用户详情 | ||
| @param user: 用户对象 | ||
| @param auth: 认证对象 | ||
| @return: | ||
| """ | ||
|
|
||
| return {'id': user.id, | ||
| 'username': user.username, | ||
| 'nick_name': user.nick_name, | ||
| 'email': user.email, | ||
| 'role': user.role, | ||
| 'permissions': [str(p) for p in []], | ||
| 'role': auth.role_list, | ||
| 'permissions': auth.permission_list, | ||
| 'is_edit_password': user.password == 'd880e722c47a34d8e9fce789fc62389d' if user.role == 'ADMIN' else False, | ||
| 'language': user.language} | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There appear to be several issues with this code:
Here’s an optimized version of the code address those points: # Remove duplicate import
import uuid_utils.compat as uuid
from common.constants.exception_code_constants import ExceptionCodeConstants
from common.constants.permission_constants import RoleConstants, Auth
from common.utils.common import valid_license, password_encrypt
from users.models import User
from django.utils.translation import gettext_lazy as _
class CreateUserSerializer(serializers.Serializer):
# Other fields...
class UserProfileSerializer(serializers.Serializer):
@staticmethod
def profile(user: User, auth: Auth):
"""
获取用户详情
@param user: 用户对象
@param auth: 认证对象
@return:
"""
role = auth.role_list if hasattr(auth, 'role_list') else []
permission_list = auth.permission_list if hasattr(auth, 'permission_list') else []
return {
'id': user.id,
'username': user.username,
'nick_name': user.nick_name,
'email': user.email,
'role': role,
'permissions': permission_list,
'is_edit_password': user.has_usable_password(),
'language': user.language
}Key Changes:
These changes make the code more robust and easier to understand. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ class UserProfileView(APIView): | |
| tags=[_("User management")], | ||
| responses=UserProfileAPI.get_response()) | ||
| def get(self, request: Request): | ||
| return result.success(UserProfileSerializer().profile(request.user)) | ||
| return result.success(UserProfileSerializer().profile(request.user, request.auth)) | ||
|
|
||
|
|
||
| class TestPermissionsUserView(APIView): | ||
|
|
@@ -41,7 +41,7 @@ class TestPermissionsUserView(APIView): | |
| responses=UserProfileAPI.get_response()) | ||
| @has_permissions(PermissionConstants.USER_EDIT) | ||
| def get(self, request: Request): | ||
| return result.success(UserProfileSerializer().profile(request.user)) | ||
| return result.success(UserProfileSerializer().profile(request.user, request.auth)) | ||
|
|
||
|
|
||
| class TestWorkspacePermissionUserView(APIView): | ||
|
|
@@ -55,7 +55,7 @@ class TestWorkspacePermissionUserView(APIView): | |
| parameters=TestWorkspacePermissionUserApi.get_parameters()) | ||
| @has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission()) | ||
| def get(self, request: Request, workspace_id): | ||
| return result.success(UserProfileSerializer().profile(request.user)) | ||
| return result.success(UserProfileSerializer().profile(request.user, request.auth)) | ||
|
|
||
|
|
||
| class UserManage(APIView): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your code is mostly fine, but there are a few things that could be improved:
Here’s an optimized version with improvements: from django.http import JsonResponse
class BaseUserProfileView(APIView):
serializer_class = UserProfileSerializer
response_schema = UserProfileAPI.get_response()
def post(self, request: Request) -> HttpResponse:
raise NotImplementedError("POST method has not been implemented.")
def get(self, request: Request) -> HttpResponse:
user_profile = self.serializer_class(profile=request.user).data
return JsonResponse(user_profile)
def has_permissions(permission_constants=None):
# Example implementation assuming PermissionConstants class exists elsewhere in the project
if permission_constants:
# Add logic to verify permission here
pass
class UserProfileView(BaseUserProfileView):
permission_classes = [PermissionConstants.USER_READ]
class TestPermissionsUserView(ProfileBaseView):
@has_permissions(PermissionConstants.USER_EDIT)
def get(self, request: Request) -> HttpResponse:
return super().get(request)
class TestWorkspacePermissionUserView(ProfileBaseView):
@has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission())
def get(self, request: Request, workspace_id) -> JsonResponse:
return super().get(request)
class UserManage(BaseUserProfileView):
def post(self, request: Request) -> JsonResponse:
# Implement logic here to handle requests
pass
# Define your serializers and other dependencies
class UserProfileSerializer(serializers.Serializer):
# Define fields here as per your requirements
pass
class ProfileBaseView(APIView):
serializer_class = None
response_schema = {}
def post(self, request: Request) -> HttpResponse:
data = self.request.data
try:
instance = self.serializer_class(data=data).save()
return JsonResponse(instance)
except ValidationError as e:
return JsonResponse(str(e.errors), status=400)Key Improvements:
These changes make the code cleaner and easier to manage. Adjust the permissions check based on your actual security needs. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code appears to have some formatting changes and variable name corrections. Here's a suggested revision:
The
permission_listis now specified usingUnion, which allows it to be either a list ofPermissionConstantsorList[Any]. This might be appropriate depending on your context, but ensure that all elements in the list implement the necessary methods or interfaces.Replaced the comma
,with an underscore_before**keywordsto match PEP 8 style guide guidelines.These adjustments should not introduce any significant issues, but if you're working with specific types for permissions or roles, consider clarifying their definitions.