Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion apps/common/constants/permission_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Operate(Enum):
DOWNLOAD = "READ+DOWNLOAD" # 下载
AUTH = "READ+AUTH" # 资源授权
TAG = "READ+TAG" # 标签设置
REPLACE = "READ+REPLACE" # 标签设置


class RoleGroup(Enum):
Expand Down Expand Up @@ -340,7 +341,7 @@ def get_workspace_role(self):
Operate.RELATE.value: _("Relate"),
Operate.ANNOTATION.value: _("Annotation"),
Operate.CLEAR_POLICY.value: _("Clear Policy"),
Operate.DOWNLOAD.value: _('Download'),
Operate.DOWNLOAD.value: _('Download Original Document'),
Operate.EMBED.value: _('Embed third party'),
Operate.ACCESS.value: _('Access restrictions'),
Operate.DISPLAY.value: _('Display Settings'),
Expand All @@ -353,6 +354,8 @@ def get_workspace_role(self):
Operate.ADD_KNOWLEDGE.value: _('Add to Knowledge Base'),
Operate.AUTH.value: _('resource authorization'),
Operate.TAG.value: _('Tag Setting'),
Operate.REPLACE.value: _('Replace Original Document'),

Group.APPLICATION_OVERVIEW.value: _('Overview'),
Group.APPLICATION_ACCESS.value: _('Application Access'),
Group.APPLICATION_CHAT_USER.value: _('Dialogue users'),
Expand Down Expand Up @@ -675,6 +678,12 @@ class PermissionConstants(Enum):
resource_permission_group_list=[ResourcePermissionConst.KNOWLEDGE_MANGE],
parent_group=[WorkspaceGroup.KNOWLEDGE, UserGroup.KNOWLEDGE]
)
KNOWLEDGE_DOCUMENT_REPLACE = Permission(
group=Group.KNOWLEDGE_DOCUMENT, operate=Operate.REPLACE,
role_list=[RoleConstants.ADMIN, RoleConstants.USER],
resource_permission_group_list=[ResourcePermissionConst.KNOWLEDGE_MANGE],
parent_group=[WorkspaceGroup.KNOWLEDGE, UserGroup.KNOWLEDGE]
)
KNOWLEDGE_HIT_TEST = Permission(
group=Group.KNOWLEDGE_HIT_TEST, operate=Operate.READ,
role_list=[RoleConstants.ADMIN, RoleConstants.USER],
Expand Down Expand Up @@ -1238,6 +1247,10 @@ class PermissionConstants(Enum):
group=Group.SYSTEM_KNOWLEDGE_DOCUMENT, operate=Operate.TAG, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.SHARED_KNOWLEDGE], is_ee=settings.edition == "EE"
)
SHARED_KNOWLEDGE_DOCUMENT_REPLACE = Permission(
group=Group.SYSTEM_KNOWLEDGE_DOCUMENT, operate=Operate.REPLACE, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.SHARED_KNOWLEDGE], is_ee=settings.edition == "EE"
)
SHARED_KNOWLEDGE_TAG_READ = Permission(
group=Group.SYSTEM_KNOWLEDGE_TAG, operate=Operate.READ, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.SHARED_KNOWLEDGE], is_ee=settings.edition == "EE"
Expand Down Expand Up @@ -1462,6 +1475,10 @@ class PermissionConstants(Enum):
group=Group.SYSTEM_RES_KNOWLEDGE_DOCUMENT, operate=Operate.TAG, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.RESOURCE_KNOWLEDGE], is_ee=settings.edition == "EE"
)
RESOURCE_KNOWLEDGE_DOCUMENT_REPLACE = Permission(
group=Group.SYSTEM_RES_KNOWLEDGE_DOCUMENT, operate=Operate.REPLACE, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.RESOURCE_KNOWLEDGE], is_ee=settings.edition == "EE"
)
RESOURCE_KNOWLEDGE_HIT_TEST = Permission(
group=Group.SYSTEM_RES_KNOWLEDGE_HIT_TEST, operate=Operate.READ, role_list=[RoleConstants.ADMIN],
parent_group=[SystemGroup.RESOURCE_KNOWLEDGE], is_ee=settings.edition == "EE"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code seems to be well-structured and contains no obvious inconsistencies or errors. However, there are a few minor improvements that can be made:

  1. Consistent Naming: Ensure consistent naming conventions throughout the codebase. This will help maintain readability and consistency.

  2. Comment Documentation: Add more comments explaining the purpose of each Operate value. While existing descriptions are somewhat helpful, they could benefit from additional details.

  3. Code Readability: Break down longer lines into multiple lines if necessary to improve readability.

Here's an adjusted version with these suggestions in mind:

from enum import Enum

class Operate(Enum):
    DOWNLOAD = "READ+DOWNLOAD"  # 下载原始文档
    AUTH = "READ+AUTH"        # 资源授权
    TAG = "READ+TAG"          # 标签设置
    REPLACE = "READ+REPLACE"      # 替换原文档


class RoleGroup(Enum):
    # Define other roles as needed


def get_workspace_role(self):
    return {
        Operate.RELATED.value: _("Relate"),
        Operate.ANNOTATION.value: _("Annotation"),
        Operate.CLEAR_POLICY.value: _("Clear Policy"),
        Operate.DOWNLOAD.value: _('Download Original Document'),  # Use full name instead of abbreviation
        Operate.EMBED.value: _('Embed third party'),
        Operate.ACCESS.value: _('Access restrictions'),
        Operate.DISPLAY.value: _('Display Settings'),
        Operate.ADD_KNOWLEDGE.value: _('Add to Knowledge Base'),
        Operate.AUTH.value: _('Resource Authorization'),
        Operate.TAG.value: _('Tag Setting'),
        Operate.REPLACE.value: _('Replace Original Document')
    }


# Example usage (assuming Permission and Group constants are defined somewhere)
permission_constants = [
    Permission(
        group=Group.KNOWLEDGE_DOCUMENT,
        operate=Operate.REPLACE,
        role_list=[RoleConstants.ADMIN, RoleConstants.USER],
        resource_permission_group_list=[ResourcePermissionConst.KNOWLEDGE_MANGE],
        parent_group=[WorkspaceGroup.KNOWLEDGE, UserGroup.KNOWLEDGE]
    ),
    # Other permissions...
]

Key Changes:

  • Descriptive Comments: Added explanations where possible for better understanding.
  • Simplified Code Structure: Reduced long lines slightly through breaking up parts of statements.

These changes make the code easier to understand and maintain.

Expand Down
4 changes: 2 additions & 2 deletions apps/knowledge/views/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ class ReplaceSourceFile(APIView):
tags=[_('Knowledge Base/Documentation')] # type: ignore
)
@has_permissions(
PermissionConstants.KNOWLEDGE_DOCUMENT_EDIT.get_workspace_knowledge_permission(),
PermissionConstants.KNOWLEDGE_DOCUMENT_EDIT.get_workspace_permission_workspace_manage_role(),
PermissionConstants.KNOWLEDGE_DOCUMENT_REPLACE.get_workspace_knowledge_permission(),
PermissionConstants.KNOWLEDGE_DOCUMENT_REPLACE.get_workspace_permission_workspace_manage_role(),
RoleConstants.WORKSPACE_MANAGE.get_workspace_role(),
ViewPermission([RoleConstants.USER.get_workspace_role()],
[PermissionConstants.KNOWLEDGE.get_workspace_knowledge_permission()], CompareConstants.AND),
Expand Down
6 changes: 6 additions & 0 deletions apps/locales/en_US/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8736,4 +8736,10 @@ msgid "Tag"
msgstr ""

msgid "Tag Setting"
msgstr ""

msgid "Download Original Document"
msgstr ""

msgid "Replace Original Document"
msgstr ""
6 changes: 6 additions & 0 deletions apps/locales/zh_CN/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8863,3 +8863,9 @@ msgstr "标签管理"

msgid "Tag Setting"
msgstr "标签设置"

msgid "Download Original Document"
msgstr "下载原文档"

msgid "Replace Original Document"
msgstr "替换原文档"
6 changes: 6 additions & 0 deletions apps/locales/zh_Hant/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8863,3 +8863,9 @@ msgstr "標籤管理"

msgid "Tag Setting"
msgstr "標籤設定"

msgid "Download Original Document"
msgstr "下載原文件"

msgid "Replace Original Document"
msgstr "替換原文件"
6 changes: 6 additions & 0 deletions ui/src/permission/knowledge/system-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ const systemManage = {
PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_TAG
],'OR'
),
doc_replace: () => hasPermission(
[
RoleConst.ADMIN,
PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_REPLACE
],'OR'
),
knowledge_chat_user_read: () =>
hasPermission([
RoleConst.ADMIN,
Expand Down
8 changes: 8 additions & 0 deletions ui/src/permission/knowledge/system-share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ const share = {
],
'OR'
),
doc_replace: () =>
hasPermission (
[
RoleConst.ADMIN,
PermissionConst.SHARED_KNOWLEDGE_DOCUMENT_REPLACE
],
'OR'
),
problem_create: () =>
hasPermission (
[
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The doc_replace function seems to be correctly defined with the correct condition structure. However, it can be optimized slightly by using destructuring assignment within the function declaration:

const share = {
  // Other existing functions...

  docReplace() {
    return hasPermission([RoleConst.ADMIN, PermissionConst.SHARED_KNOWLEDGE_DOCUMENT_REPLACE], 'OR');
  },
};

This approach is generally cleaner and more concise.

No other potential issues or irregularities were found with the provided code snippet.

Regarding future optimizations for handling shared knowledge documents replace permissions, you might consider implementing caching strategies if performance becomes an issue, especially if this method is called frequently throughout your application. Additionally, ensure that all necessary data access controls (like role checking) are properly validated before executing critical operations like document replacement.

Expand Down
1 change: 1 addition & 0 deletions ui/src/permission/knowledge/workspace-share.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const workspaceShare = {
doc_export: () => false,
doc_download: () => false,
doc_tag: () => false,
doc_replace: () => false,

knowledge_chat_user_read: () => false,
knowledge_chat_user_edit: () => false,
Expand Down
10 changes: 10 additions & 0 deletions ui/src/permission/knowledge/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ const workspace = {
],
'OR',
),
doc_replace: (source_id:string) =>
hasPermission(
[
new ComplexPermission([RoleConst.USER],[PermissionConst.KNOWLEDGE.getKnowledgeWorkspaceResourcePermission(source_id)],[],'AND'),
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
PermissionConst.KNOWLEDGE_DOCUMENT_REPLACE.getKnowledgeWorkspaceResourcePermission(source_id),
PermissionConst.KNOWLEDGE_DOCUMENT_REPLACE.getWorkspacePermissionWorkspaceManageRole,
],
'OR',
),
knowledge_chat_user_read: (source_id:string) => false,
knowledge_chat_user_edit: (source_id:string) =>
hasPermission(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no specific irregularities or major issues present in the code snippet you provided for workspace.doc_replace. However, there is one optimization suggestion:

doc_replace: (source_id:string) =>
  hasPermission([
    new ComplexPermission([RoleConst.USER], [PermissionConst.KNOWLEDGE.getKnowledgeWorkspaceResourcePermission(source_id)], [], 'AND'),
    RoleConst.WORKSPACE_MANAGE.getWorkspaceRole(),
    ...[
      PermissionConst.KNOWLEDGE_DOCUMENT_REPLACE.getKnowledgeWorkspaceResourcePermission(source_id),
      PermissionConst.KNOWLEDGE_DOCUMENT_REPLACE.getWorkspacePermissionWorkspaceManageRole(),
    ].filter(Boolean), // Remove falsy values if needed
  ], 'OR');

This ensures that only truthy permission objects are considered during the .filter(Boolean) operation. This can help simplify and avoid runtime errors if some of these permissions might not evaluate to true under certain conditions.

Expand Down
3 changes: 3 additions & 0 deletions ui/src/utils/permission/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const PermissionConst = {
KNOWLEDGE_DOCUMENT_EXPORT: new Permission('KNOWLEDGE_DOCUMENT:READ+EXPORT'),
KNOWLEDGE_DOCUMENT_DOWNLOAD_SOURCE_FILE: new Permission('KNOWLEDGE_DOCUMENT:READ+DOWNLOAD'),
KNOWLEDGE_DOCUMENT_TAG: new Permission('KNOWLEDGE_DOCUMENT:READ+TAG'),
KNOWLEDGE_DOCUMENT_REPLACE: new Permission('KNOWLEDGE_DOCUMENT:READ+REPLACE'),

KNOWLEDGE_TAG_READ: new Permission('KNOWLEDGE_TAG:READ'),
KNOWLEDGE_TAG_CREATE: new Permission('KNOWLEDGE_TAG:READ+CREATE'),
Expand Down Expand Up @@ -201,6 +202,7 @@ const PermissionConst = {
SHARED_KNOWLEDGE_DOCUMENT_EXPORT: new Permission('SYSTEM_KNOWLEDGE_DOCUMENT:READ+EXPORT'),
SHARED_KNOWLEDGE_DOCUMENT_DOWNLOAD_SOURCE_FILE: new Permission('SYSTEM_KNOWLEDGE_DOCUMENT:READ+DOWNLOAD'),
SHARED_KNOWLEDGE_DOCUMENT_TAG: new Permission('SYSTEM_KNOWLEDGE_DOCUMENT:READ+TAG'),
SHARED_KNOWLEDGE_DOCUMENT_REPLACE: new Permission('SYSTEM_KNOWLEDGE_DOCUMENT:READ+REPLACE'),

SHARED_KNOWLEDGE_TAG_READ: new Permission('SYSTEM_KNOWLEDGE_TAG:READ'),
SHARED_KNOWLEDGE_TAG_EDIT: new Permission('SYSTEM_KNOWLEDGE_TAG:READ+EDIT'),
Expand Down Expand Up @@ -253,6 +255,7 @@ const PermissionConst = {
RESOURCE_KNOWLEDGE_DOCUMENT_EXPORT: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_DOCUMENT:READ+EXPORT'),
RESOURCE_KNOWLEDGE_DOCUMENT_DOWNLOAD_SOURCE_FILE: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_DOCUMENT:READ+DOWNLOAD'),
RESOURCE_KNOWLEDGE_DOCUMENT_TAG: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_DOCUMENT:READ+TAG'),
RESOURCE_KNOWLEDGE_DOCUMENT_REPLACE: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_DOCUMENT:READ+REPLACE'),

RESOURCE_KNOWLEDGE_TAG_READ: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_PROBLEM:READ'),
RESOURCE_KNOWLEDGE_TAG_CREATE: new Permission('SYSTEM_RESOURCE_KNOWLEDGE_PROBLEM:READ+CREATE'),
Expand Down
6 changes: 4 additions & 2 deletions ui/src/views/document/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,15 @@
{{ $t('views.document.setting.download') }}
</el-dropdown-item>
<el-upload
v-if="permissionPrecise.doc_replace(id)"
ref="elUploadRef"
:file-list="[]"
action="#"
:auto-upload="false"
:show-file-list="false"
:on-change="(file: any, fileList: any) => replaceDocument(file, row)"
>
<el-dropdown-item v-if="permissionPrecise.doc_edit(id)">
<el-dropdown-item>
<el-icon class="color-secondary">
<Upload />
</el-icon>
Expand Down Expand Up @@ -786,7 +787,8 @@ const MoreFilledPermission1 = (id: string) => {
permissionPrecise.value.doc_export(id) ||
permissionPrecise.value.doc_download(id) ||
permissionPrecise.value.doc_delete(id) ||
permissionPrecise.value.doc_tag(id)
permissionPrecise.value.doc_tag(id) ||
permissionPrecise.value.doc_replace(id)
)
}

Expand Down
Loading