Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/folders/views/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class FolderView(APIView):
tags=[_('Folder')] # type: ignore
)
@has_permissions(
lambda r, kwargs: Permission(group=Group(f"{kwargs.get('source')}_FOLDER"), operate=Operate.CREATE,
lambda r, kwargs: Permission(group=Group(f"{kwargs.get('source')}_FOLDER"), operate=Operate.EDIT,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}/{kwargs.get('source')}/{r.data.get('parent_id')}"),
lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.EDIT,
lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.CREATE,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}:ROLE/WORKSPACE_MANAGE"
),
lambda r, kwargs: ViewPermission([RoleConstants.USER.get_workspace_role()],
Expand Down Expand Up @@ -151,7 +151,7 @@ def get(self, request: Request, workspace_id: str, source: str, folder_id: str):
tags=[_('Folder')] # type: ignore
)
@has_permissions(
lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.EDIT,
lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.DELETE,
resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}:ROLE/WORKSPACE_MANAGE"
),
lambda r, kwargs: Permission(group=Group(f"{kwargs.get('source')}_FOLDER"), operate=Operate.EDIT,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the provided code snippet, I identified several issues and suggestions for improvement:

Issues Found:

  1. Permission Logic Divergence:

    • The create permission checks different group names ({kwargs.get('source')}_FOLDER)
      and resource paths compared to the edit permission.
  2. Duplicate Permissions:

    • There is duplicated logic for checking permissions with resource_path=f"/WORKSPACE/{kwargs.get('workspace_id'):ROLE/WORKSPACE_MANAGE".
  3. Resource Path Formatting:

    • The resource_path formatting differs between CREATE, EDIT, and DELETE.
  4. Type Hint Usage:

    • Typing hints like # type: ignore should be used judiciously and ideally removed if applicable.

Suggestions:

class FolderView(APIView):
    tags=[_('Folder')]  # No need for type hint here

@has_permissions(
    lambda r, kwargs: Permission(group=Group(f"{kwargs.get('source')}_FOLDER"), operate=Operate.CREATE,
                                 resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}/{kwargs.get('source')}/new"),
    
    lambda r, kwargs: Permission(group=Group(kwargs.get('source')), operate=Operate.EDIT,
                                 resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}:ROLE/WORKSPACE_MANAGE")
)
def create_view(request: Request, workspace_id: str, source: str) -> Response:
    ...

@has_permissions(
    lambda r, kwargs: Permission(group=Group(f"{kwargs.get('source')}_FOLDER"), operate=Operate.DELETE,
                                 resource_path=f"/WORKSPACE/{kwargs.get('workspace_id')}/{kwargs.get('source')}")
)
def delete_view(request: Request, workspace_id: str, source: str) -> Response:
    ...

Key Changes:

  1. Consistent Resource Paths:

    • All permissions now share the same resource_path pattern for clarity and consistency.
  2. Simplified Duplicate Code:

    • Combined duplicate permission conditions into simpler calls where possible.
  3. Removed Type Hint:

    • Removed unnecessary type:ignore lines.

These changes should improve readability, maintainability, and reduce potential errors associated with the original code.

Expand Down
Loading