-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: application page #3233
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
fix: application page #3233
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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| select * | ||
| from (select "id"::text, | ||
| "name", | ||
| "desc", | ||
| "is_publish", | ||
| "type", | ||
| 'application' as "resource_type", | ||
| "workspace_id", | ||
| "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application | ||
| where id in (select target | ||
| from workspace_user_resource_permission | ||
| where auth_target_type = 'APPLICATION' | ||
| and 'VIEW' = any (permission_list)) | ||
| UNION | ||
| select "id", | ||
| "name", | ||
| "desc", | ||
| true as "is_publish", | ||
| 'folder' as "type", | ||
| 'folder' as "resource_type", | ||
| "workspace_id", | ||
| "parent_id" as "folder_id", | ||
| "user_id", | ||
| "create_time", | ||
| "update_time" | ||
| from application_folder ${folder_query_set}) temp | ||
| ${application_query_set} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,8 @@ | |
| from common.constants.cache_version import Cache_Version | ||
| from common.constants.permission_constants import Auth, PermissionConstants, ResourcePermissionGroup, \ | ||
| get_permission_list_by_resource_group, ResourceAuthType, \ | ||
| ResourcePermissionRole, get_default_role_permission_mapping_list, get_default_workspace_user_role_mapping_list | ||
| ResourcePermissionRole, get_default_role_permission_mapping_list, get_default_workspace_user_role_mapping_list, \ | ||
| RoleConstants | ||
| from common.database_model_manage.database_model_manage import DatabaseModelManage | ||
| from common.exception.app_exception import AppAuthenticationFailed | ||
| from common.utils.common import group_by | ||
|
|
@@ -50,6 +51,18 @@ def get_workspace_permission(permission_id, workspace_id): | |
| return f"{permission_id}:/WORKSPACE/{workspace_id}" | ||
|
|
||
|
|
||
| def get_role_permission(role, workspace_id): | ||
| """ | ||
| 获取工作空间角色 | ||
| @param role: 角色 | ||
| @param workspace_id: 工作空间id | ||
| @return: | ||
| """ | ||
| if isinstance(role, RoleConstants): | ||
| role = role.value | ||
| return f"{role}:/WORKSPACE/{workspace_id}" | ||
|
|
||
|
|
||
| def get_workspace_permission_list(role_permission_mapping_dict, workspace_user_role_mapping_list): | ||
| """ | ||
| 获取工作空间下所有的权限 | ||
|
|
@@ -212,7 +225,7 @@ def get_role_list(user, | |
| workspace_user_role_mapping_list] + [user.role], version=version) | ||
| else: | ||
| cache.set(key, [user.role], version=version) | ||
| return [user.role] | ||
| return [user.role, get_role_permission(RoleConstants.WORKSPACE_MANAGE, 'default')] | ||
| return workspace_list | ||
|
|
||
|
|
||
|
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. The provided code is generally clean and well-structured, but there are a few suggestions for improvement:
Here's an updated version of the code addressing some suggestions: from common.constants.cache_version import Cache_Version
from common.constants.permission_constants import Auth, PermissionConstants, ResourcePermissionGroup, \
get_permission_list_by_resource_group, ResourceAuthType, \
ResourcePermissionRole, get_default_role_permission_mapping_list, get_default_workspace_user_role_mapping_list, \
RoleConstants
from common.database_model_manage.database_model_manage import DatabaseModelManage
from common.exception.app_exception import AppAuthenticationFailed
from common.utils.common import group_by
def get_workspace_permission(resource_type, resource_identifier):
return f"{resource_type}:{resource_identifier}"
def get_role_permission(role_name, workspace_id):
"""
获取工作空间角色。
:param role_name: 角色名称。
:param workspace_id: 工作空间ID。
:return: 角色对应的权限。
"""
return f"{role_name}:/WORKSPACE/{workspace_id}"
def fetch_all_roles_for_user(user, user_auth_token=None, version=None):
key = "user:" + str(user.id)
if not cache.get(key, version=version):
# Fetch all roles for the user and cache them.
if user_auth_token:
auth = self.fetch_user_from_jwt(auth_token=user_auth_token)
if auth.user.role == 'admin':
roles_to_fetch = [RoleConstants.WORKSPACE_MANAGE]
# Additional roles fetching process...
else:
roles_to_fetch = [UserRole]
cached_roles = list(set(roles_to_fetch))
cache.set(key, cached_roles, version=version)
return cached_roles
return []
# Example usage:
# Get all roles for a specific user
all_roles = fetch_all_roles_for_user(current_user)
# Retrieve default workspace manage permissions if present
if any(role == RoleConstants.WORKSPACE_MANAGE for role in all_roles):
default_workspaces_permissions = []
# Logic to populate default workspaces permissions...
"""
Additional methods to handle permissions retrieval and caching remain largely unchanged.
"""Key Changes:
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,17 @@ class CreateUserSerializer(serializers.Serializer): | |
| phone = serializers.CharField(required=False, label=_('Phone')) | ||
|
|
||
|
|
||
| def is_workspace_manage(user_id: str, workspace_id: str): | ||
| workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping") | ||
| role_permission_mapping_model = DatabaseModelManage.get_model("role_permission_mapping_model") | ||
| is_x_pack_ee = workspace_user_role_mapping_model is not None and role_permission_mapping_model is not None | ||
| if is_x_pack_ee: | ||
| return QuerySet(workspace_user_role_mapping_model).select_related('role', 'user').filter( | ||
| workspace_id=workspace_id, user_id=user_id, | ||
| role_type=RoleConstants.WORKSPACE_MANAGE.value.__str__()).exists() | ||
| return QuerySet(User).filter(id=user_id, role=RoleConstants.ADMIN.value.__str__()).exists() | ||
|
|
||
|
|
||
| class UserProfileSerializer(serializers.Serializer): | ||
| @staticmethod | ||
| def profile(user: User, auth: Auth): | ||
|
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. The provided code appears to be incomplete and contains several potential issues. However, I can offer some general feedback on improving it:
Here's an improved version of the function (assuming you've corrected some import statements): from django.db.models.functions import Exists
class CreateUserSerializer(serializers.Serializer):
phone = serializers.CharField(required=False, label=_('Phone'))
def has_workspace_management_role(user_id: str, workspace_id: str) -> bool:
try:
ws UserRoleMapping = DatabaseModelManage.get_model("workspace_user_role_mapping")
RolePermissionMapping = DatabaseModelManage.get_model("role_permission_mapping")
if wsUserRoleMapping and RolePermissionMapping:
# Check for management role using EXISTS subquery
return (
_exists(
WorkspaceUserRoleMapping.objects.filter(
id=Exists(wsUserRoleMapping.objects.select_related('role', 'user')
.filter(
workspace_id=workspace_id,
user_id=user_id,
role_type=RoleConstants.WORKSPACE_MANAGE.value.__str__()
))
|
User.objects.filter(Q(id=user_id), Q(role=RoleConstants.ADMIN.value.__str__()))
)
== True
)
else:
raise Exception("Models not found")
except Exception as e:
print(f"An error occurred: {e}")
return False
class UserProfileSerializer(serializers.ModelSerializer):
@staticmethod
def profile(user: User, auth: Auth):
# Implement logic here to get and serialize user profileKey Changes:
These changes aim to clean up the original snippet and address some common pitfalls in ORM usage in Django. Always ensure proper imports and context management to avoid runtime errors. |
||
|
|
||
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.
Here are some suggestions to improve your code:
Variable Naming: Replace
instance: Dictwith something more specific likerequest_datato describe its purpose.Comments and Docstrings:
Separation of concerns:
Error Handling:
Security Considerations:
Code Organization:
Here's an improved version based on these points:
Key Changes:
Functionality Encapsulation: Created a method
get_query_set()which simplifies handling query logic outside serialization methods.Input Validation: The
retrieve()method includes validation to handle incoming requests correctly.Error Handling: Added error handling using
asyncio.ErrorHandler.Documentation: Added docstrings for methods to explain their parameters and responsibilities.
This refactored approach makes the code cleaner, easier to understand, and maintainable.