-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: Problem read permission #3810
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 |
|---|---|---|
|
|
@@ -212,7 +212,16 @@ const workspace = { | |
| ] | ||
| ,'OR' | ||
| ), | ||
| problem_read: () => false, | ||
| problem_read: (source_id:string) => | ||
| hasPermission( | ||
| [ | ||
| new ComplexPermission([RoleConst.USER],[PermissionConst.KNOWLEDGE.getKnowledgeWorkspaceResourcePermission(source_id)],[],'AND'), | ||
| RoleConst.WORKSPACE_MANAGE.getWorkspaceRole, | ||
| PermissionConst.KNOWLEDGE_PROBLEM_READ.getKnowledgeWorkspaceResourcePermission(source_id), | ||
| PermissionConst.KNOWLEDGE_PROBLEM_READ.getWorkspacePermissionWorkspaceManageRole, | ||
| ], | ||
| 'OR', | ||
| ), | ||
| problem_create: (source_id:string) => | ||
| hasPermission( | ||
| [ | ||
|
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 are several potential issues and optimizations that can be made to improve the provided code:
Here's a potentially optimized version of the code with some recommendations applied: const workspace = {
// Simplified read permission logic
problem_read: (source_id) =>
hasPermission([
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
PermissionConst.KNOWLEDGE_PROBLEM_READ.getKnowledgeWorkspaceResourcePermission(source_id)
], 'AND'),
// Assuming same pattern applies to other CRUD operations
problem_create: (source_id) =>
hasPermission([
RoleConst.WORKSPACE_MANAGE.getWorkspaceRole,
PermissionConst.KNOWLEDGE_PROBLEM_CREATE.getKnowledgeWorkspaceResourcePermission(source_id)
], 'AND'),
// Continue similar patterns for edit, delete as needed
// Optional: Add helper methods or enums for easier management of constants
}
// Example usage with placeholder functions for hasPermission and related constants
function hasPermission(permissions, condition) {
// Implementation details here
switch (condition) {
case 'AND':
return permissions.every(checker);
case 'OR':
return permissions.some(checker);
default:
throw new Error('Invalid condition');
}
function checker(permission) {
// Placeholder implementation here
console.log(`Checking permission: ${permission}`);
return true; // Return false if access not allowed
}
}In this revised approach:
These changes aim to improve readability, reduce redundancy, enhance security, and prepare the base for further optimizations or improvements. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| <el-col :span="6" class="border-l" style="width: 300px"> | ||
| <!-- 关联问题 --> | ||
| <ProblemComponent | ||
| v-if="permissionPrecise.problem_read(id)" | ||
| :paragraphId="paragraphId" | ||
| :docId="document_id" | ||
| :knowledgeId="id" | ||
|
|
@@ -56,12 +57,14 @@ | |
| </el-dialog> | ||
| </template> | ||
| <script setup lang="ts"> | ||
| import { ref, watch, nextTick } from 'vue' | ||
| import { ref, watch, nextTick, computed } from 'vue' | ||
| import { useRoute } from 'vue-router' | ||
| import { cloneDeep, debounce } from 'lodash' | ||
| import ParagraphForm from '@/views/paragraph/component/ParagraphForm.vue' | ||
| import ProblemComponent from '@/views/paragraph/component/ProblemComponent.vue' | ||
| import { loadSharedApi } from '@/utils/dynamics-api/shared-api' | ||
| import permissionMap from '@/permission' | ||
|
|
||
|
|
||
| const props = defineProps<{ | ||
| title: String | ||
|
|
@@ -73,6 +76,21 @@ const { | |
| params: { id, documentId }, | ||
| } = route as any | ||
|
|
||
| const apiType = computed(() => { | ||
| if (route.path.includes('shared')) { | ||
| return 'systemShare' | ||
| } else if (route.query.from == 'systemManage') { | ||
| return 'systemManage' | ||
| } else { | ||
| return 'workspace' | ||
| } | ||
| }) | ||
|
|
||
| const permissionPrecise = computed(() => { | ||
| return permissionMap['knowledge'][apiType.value] | ||
| }) | ||
|
|
||
|
|
||
| const emit = defineEmits(['refresh']) | ||
|
|
||
| const ProblemRef = ref() | ||
|
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 code looks mostly clean and well-structured. However, there are a few areas that could be improved:
Here's an optimized version of the code with improvements mentioned above: <template>
<el-col :span="6" class="border-l" style="width: 300px">
<!-- 关联问题 -->
<ProblemComponent
v-if="hasPermissionToRead()"
:paragraphId="paragraphId"
:docId="documentId"
:knowledgeId="id"
/>
</el-col>
<el-dialog ...>
...
</el-dialog>
</template>
<script setup lang="ts">
import { ref, watch, nextTick, computed } from 'vue';
import { useRoute } from 'vue-router';
import { cloneDeep, debounce } from 'lodash';
import ParagraphForm from '@/views/paragraph/component/ParagraphForm.vue';
import ProblemComponent from '@/views/paragraph/component/ProblemComponent.vue';
import { loadSharedApi } from '@/utils/dynamics-api/shared-api';
import permissionMap from '@/permission';
const props = defineProps<{
title: string;
paragraphId: number;
docId: number;
}>();
const route = useRoute();
const { params: { id, documentId }, query } = route;
interface PermissionMapEntry<T extends any> {
[key: string]: T;
}
computed(() => ({
systemShare: () => /* fetch shared permission logic */,
systemManage: () => /* fetch manage permission logic */,
workspace: () => /* fetch workspace permission logic */
}))[props.documentId];
const hasPermissionToRead = (): boolean =>
!!permissionPrecise.read && !!permissionPrecise.update;
const apiType = computed(() => {
if (query?.from === 'systemManage') {
return 'systemManage';
} else if (route.path.includes('/shared')) {
return 'systemShare';
} else {
return 'workspace';
}
});
const permissionsByDocumentId = computePermsByDocumentId(documentId);
const emit = defineEmits(['refresh']);Key Changes:
These changes make the code cleaner and more maintainable while still adhering to proper coding practices. |
||
|
|
||
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.
The code is mostly correct but there are some optimizations and improvements that can be made:
problem_readandproblem_relatefunctions into one to avoid duplication.Here's an optimized version of the code:
Optimizations Made:
problem_readandproblem_relatefunctions have been combined into a single function calledproblem_permission, which takes an additional parameteraction, indicating whether it's a read or delete operation.actioninstead of hardcoding strings for readability and maintainability.This approach makes the code more concise and easier to understand. Additionally, it provides a clear structure for handling different permissions based on actions (
read,delete).