-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: Chat_log_permission #3967
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: Chat_log_permission #3967
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 |
---|---|---|
|
@@ -50,7 +50,9 @@ | |
</el-form-item> | ||
</el-form> | ||
<SelectKnowledgeDocument | ||
v-if="detail.workspace_id" | ||
ref="SelectKnowledgeDocumentRef" | ||
:post-knowledge-handler="postKnowledgeHandler" | ||
:apiType="apiType" | ||
@changeKnowledge="changeKnowledge" | ||
@changeDocument="changeDocument" | ||
|
@@ -75,6 +77,10 @@ import type { FormInstance, FormRules } from 'element-plus' | |
import imageApi from '@/api/image' | ||
import { t } from '@/locales' | ||
import { loadSharedApi } from '@/utils/dynamics-api/shared-api' | ||
import { Permission } from '@/utils/permission/type' | ||
import { hasPermission } from '@/utils/permission' | ||
import { PermissionConst, RoleConst } from '@/utils/permission/data' | ||
|
||
const route = useRoute() | ||
const { | ||
params: { id }, | ||
|
@@ -87,6 +93,18 @@ const apiType = computed(() => { | |
} | ||
}) | ||
|
||
const postKnowledgeHandler = (knowledgeList: Array<any>) => { | ||
return knowledgeList.filter(item => { | ||
if (apiType.value === 'workspace') { | ||
return hasPermission([RoleConst.WORKSPACE_MANAGE.getWorkspaceRole(), | ||
new Permission("KNOWLEDGE_DOCUMENT:READ+EDIT").getWorkspacePermissionWorkspaceManageRole, | ||
new Permission("KNOWLEDGE_DOCUMENT:READ+EDIT").getWorkspaceResourcePermission('KNOWLEDGE', item.id)], 'OR') | ||
} else if (apiType.value === 'systemManage') { | ||
return hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_EDIT],'OR') | ||
} | ||
}) | ||
} | ||
|
||
const emit = defineEmits(['refresh']) | ||
|
||
const formRef = ref() | ||
|
@@ -127,7 +145,6 @@ const SelectKnowledgeDocumentRef = ref() | |
const dialogVisible = ref<boolean>(false) | ||
const loading = ref(false) | ||
const detail = ref<any>({}) | ||
|
||
const form = ref<any>({ | ||
chat_id: '', | ||
record_id: '', | ||
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. Here are some general comments and suggestions for your code:
Here is an optimized version with these considerations: <template>
<el-form ref="formRef" :model="form" :rules="formRules">
<!-- ...existing fields... -->
</el-form>
<SelectKnowledgeDocument
v-if="detail.workspace_id"
ref="SelectKnowledgeDocumentRef"
:post-knowledge-handler="postKnowledgeHandler"
:api-type="apiType"
@changeKnowledge="changeKnowledge"
@changeDocument="changeDocument"
/>
</template>
<script lang="ts">
import { ref, reactive, computed } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import SelectKnowledgeDocument from '@/components/common/SelectKnowledgeDocument.vue'
// Import other dependencies
export default {
name: 'YourComponentName',
setup() {
const formRef = ref<FormInstance>()
// Existing code...
const apiType = computed(() => {...})
const postKnowledgeHandler = (knowledgeList: Array<any>): Array<any> => {
return knowledgeList.filter(item => {
if (
apiType.value === 'workspace' &&
hasPermission([
{
roleKey: RoleConst.WORKSPACE_MANAGE.roleKey,
resourcePermissions: ['KNOWLEDGE'],
resourceId: item.id,
},
{
roleKey: PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_READ_WRITE.roleKey,
resourceKey: RoleConst.WORKSPACE_RESOURCE_PERMISSION.resourceKey,
resourceName: 'KNOWLEDGE',
resourceId: item.id,
},
])
) {
return true
}
if (
apiType.value === 'systemManage' &&
hasPermission([RoleConst.ADMIN.roleKey, PermissionConst.RESOURCES.KNOWLEDGE_DOCUMENT_EDIT.roleKey])
) {
return true
}
return false
})
}
const form = reactive({
chat_id: '',
record_id: ''
})
// Existing code...
return {
formRef,
form,
formRules,
selectKnowledgeDialogVisible,
loading,
detail,
apiType,
postKnowledgeHandler,
emit,
}
},
}
</script> Key Improvements:
Make sure to adjust according to specific project requirements and existing architecture. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,6 +222,7 @@ | |
:close-on-press-escape="false" | ||
> | ||
<SelectKnowledgeDocument | ||
:post-knowledge-handler="postKnowledgeHandler" | ||
ref="SelectKnowledgeDocumentRef" | ||
:apiType="apiType" | ||
:workspace-id="detail.workspace_id" | ||
|
@@ -252,6 +253,10 @@ import { t } from '@/locales' | |
import { ElTable } from 'element-plus' | ||
import permissionMap from '@/permission' | ||
import { loadSharedApi } from '@/utils/dynamics-api/shared-api' | ||
import { Permission } from '@/utils/permission/type' | ||
import { hasPermission } from '@/utils/permission' | ||
import { PermissionConst, RoleConst } from '@/utils/permission/data' | ||
|
||
const route = useRoute() | ||
|
||
const apiType = computed(() => { | ||
|
@@ -349,7 +354,18 @@ const filter = ref<any>({ | |
min_trample: 0, | ||
comparer: 'and', | ||
}) | ||
|
||
const postKnowledgeHandler = (knowledgeList: Array<any>) => { | ||
return knowledgeList.filter(item => { | ||
if (apiType.value === 'workspace') { | ||
return hasPermission([RoleConst.WORKSPACE_MANAGE.getWorkspaceRole(), | ||
new Permission("KNOWLEDGE_DOCUMENT:READ+EDIT").getWorkspacePermissionWorkspaceManageRole, | ||
new Permission("KNOWLEDGE_DOCUMENT:READ+EDIT").getWorkspaceResourcePermission('KNOWLEDGE', item.id)], 'OR') | ||
} else if (apiType.value === 'systemManage') { | ||
return hasPermission([RoleConst.ADMIN, PermissionConst.RESOURCE_KNOWLEDGE_DOCUMENT_EDIT],'OR') | ||
} | ||
}) | ||
|
||
} | ||
function filterChange(val: string) { | ||
if (val === 'clear') { | ||
filter.value = cloneDeep(defaultFilter) | ||
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 code looks well-written, but there are a few enhancements and optimizations you can consider:
Here's an updated version of your code with these improvements: <script lang="ts">
import { defineComponent, ref,computed } from 'vue'
import SelectKnowledgeDocument from '@/components/SelectKnowledgeDocument.vue'
import { loadSharedApi } from '@/utils/dynamics-api/shared-api'
import route from '@/router'
import permissionMap from '@/permission'
interface APIType {
workspace?: boolean;
systemManage?: boolean;
}
export default defineComponent({
props: {
closeOnPressEscape: {
type: Boolean,
default: false,
},
},
setup(props) {
interface FilterValues {
keyWords: string;
doc_types: string[];
status_list: string[];
created_before_time: number | '';
max_level_of_difficulty: number;
min_trample: number;
comparer: 'and' | 'or';
}
const selectKnowledgeDocumentRef = ref<SelectKnowledgeDocument>();
const apiType = computed<APIType>(() => {
// Determine API type logic here
});
const detail = ref<{ [key: string]: any }>({}); // Adjust according to your structure
const filter = ref<FilterValues>({
keyWords: '',
doc_types: [],
status_list: ['active'],
created_before_time: '',
max_level_of_difficulty: Infinity,
min_trample: 0,
comparer: 'and',
});
function onSelectionChange(knowledgeList: unknown[]): void {
if (!apiType.value || !selectKnowledgeDocumentRef.value) return;
const filteredDocuments = postKnowledgeHandler(knowledgeList);
if (Array.isArray(filteredDocuments)) {
// Handle_filtered_documents_here
}
}
const hasPermissionWithRole = (rolesAndResources: Array<string>, action: string): boolean => {
return rolesAndResources.some(role =>
hasPermission(
Object.values(permissionMap).filter(p => p.roleId.startsWith(role))[0]
.permissions[action] ?? []
)
);
};
const postKnowledgeHandler = (
knowledgeList: Array<{
id: string;
[key: string]: any; // Adjust properties based on actual data
}>
): Array<{
id: string;
name: string;
// Add other relevant fields for the returned object
}> => {
return knowledgeList.filter((item) => {
if (apiType.value?.workspace) {
return hasPermissionWithRole([RoleConst.WORKSPACE_MANAGE], 'WRITE KNOWLEDGE');
} else if (apiType.value?.systemManage) {
return hasPermissionWithRole(['admin', 'resource_knowledge_document_edit'], 'WRITE KNOWLEDGE');
}
return true; // Default to accepting all items when no specific conditions match
});
};
function filterChange(val: string): void {
if (val === 'clear') {
Object.assign(filter.value, defaultValue.filterObject());
}
}
return {
selectKnowledgeDocumentRef,
apiType,
detail,
filter,
onSelectionChange,
filterChange,
};
},
});
</script> Key Changes:
These changes should make the code more robust, readable, and potentially optimize it for production use. |
||
|
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.
There are no errors detected in this code snippet. However, there is an instance of incorrect type annotation that can be optimized:
In the
defineProps
statement where you definepostKnowledgeHandler
, the function parameter should have a more specific type thanArray<any>
. Given that it's intended to handle arrays but doesn't process them further, using a union type like(knowledge_list: Array<string>) => Array<string>
would be more accurate, assuming knowledge points will always be represented as strings.Also, consider initializing
optionLoading
variable. IfloadSharedApi.getKnowledgeList(optionLoading)
expects a loading indicator, make sure it's defined outsideasync
functions or passed properly within.Here’s an updated version with these recommendations:
This makes the types more precise while maintaining readability and clarity.