refactor(permissions): enhance role-based access controls across knowledge base and agent views

- Updated visibility conditions for various UI elements based on user roles, ensuring that only users with appropriate permissions (Admin, Contributor, or KB creator) can access certain actions.
- Introduced computed properties to manage permissions for sharing knowledge bases, managing data sources, and executing graph extraction.
- Adjusted the AgentList and KnowledgeBase components to reflect these changes, improving user experience by preventing unauthorized actions from being displayed.
- Ensured that the backend routes are aligned with the updated frontend role checks, enhancing security and clarity in user interactions.
This commit is contained in:
wizardchen
2026-05-15 17:56:58 +08:00
committed by lyingbug
parent e7f9a6580a
commit c6a2f6c8cc
9 changed files with 104 additions and 28 deletions
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import {
@@ -14,10 +14,17 @@ import { humanizeCron, relativeTime } from '@/utils/cronHumanize'
import DataSourceEditorDialog from './DataSourceEditorDialog.vue'
import DataSourceSyncLogs from './DataSourceSyncLogs.vue'
import DataSourceTypeIcon from './DataSourceTypeIcon.vue'
import { useAuthStore } from '@/stores/auth'
const props = defineProps<{ kbId: string }>()
const emit = defineEmits<{ (e: 'count', value: number): void }>()
const { t } = useI18n()
const authStore = useAuthStore()
// 后端 /datasource 的 list/logs 是 Viewer+,但所有写操作(POST/PUT/DELETE
// 以及 sync/pause/resume/validate)都是 Admin+。低权限用户保留只读视图,
// 增删改和触发同步全部隐藏,而不是按下去再撞 403。
const canManageDataSource = computed(() => authStore.hasRole('admin'))
const dataSources = ref<DataSource[]>([])
const loading = ref(false)
@@ -220,7 +227,7 @@ onBeforeUnmount(stopPolling)
<div class="ds-empty-text">
<p class="ds-empty-title">{{ t('datasource.empty') }}</p>
</div>
<t-button theme="primary" variant="outline" @click="openCreate">
<t-button v-if="canManageDataSource" theme="primary" variant="outline" @click="openCreate">
<template #icon><t-icon name="add" /></template>
{{ t('datasource.addFirst') }}
</t-button>
@@ -243,7 +250,7 @@ onBeforeUnmount(stopPolling)
</div>
<div class="ds-card-actions">
<t-tooltip :content="isSyncRunning(ds) ? t('datasource.logStatus.running') : t('datasource.syncNow')">
<t-tooltip v-if="canManageDataSource" :content="isSyncRunning(ds) ? t('datasource.logStatus.running') : t('datasource.syncNow')">
<t-button
size="small"
variant="text"
@@ -261,7 +268,7 @@ onBeforeUnmount(stopPolling)
<template #icon><t-icon name="root-list" /></template>
</t-button>
</t-tooltip>
<t-dropdown trigger="click" :min-column-width="120">
<t-dropdown v-if="canManageDataSource" trigger="click" :min-column-width="120">
<t-tooltip :content="t('datasource.moreActions')">
<t-button size="small" variant="text" shape="square">
<template #icon><t-icon name="ellipsis" /></template>
@@ -326,7 +333,7 @@ onBeforeUnmount(stopPolling)
</div>
</div>
<div class="ds-card-add" @click="openCreate">
<div v-if="canManageDataSource" class="ds-card-add" @click="openCreate">
<div class="ds-card-add-icon">
<t-icon name="add" size="20px" />
</div>
@@ -43,6 +43,7 @@
<div class="setting-control full-width">
<div class="tags-control-group">
<t-button
v-if="canRunGraphExtract"
theme="default"
size="medium"
:disabled="!modelStatus.llm.available"
@@ -79,6 +80,7 @@
<div class="setting-control full-width">
<div class="text-control-group">
<t-button
v-if="canRunGraphExtract"
theme="default"
size="medium"
:disabled="!modelStatus.llm.available"
@@ -266,6 +268,7 @@
<div class="setting-control">
<div class="action-buttons">
<t-button
v-if="canRunGraphExtract"
theme="primary"
:disabled="!modelStatus.llm.available || !localGraphExtract.text"
:loading="extracting"
@@ -298,8 +301,15 @@ import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import { extractTextRelations, fabriText, fabriTag, type Node, type Relation } from '@/api/initialization'
import { getSystemInfo } from '@/api/system'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
// canRunGraphExtract 对应后端 POST /initialization/extract/{fabri-tag,fabri-text,
// text-relation} 的 g.Admin() 守卫——这三个都是会调用大模型 + 写库的 admin
// 工具。Contributor 看到按钮点了只会撞 403。
const canRunGraphExtract = computed(() => authStore.hasRole('admin'))
interface GraphExtractConfig {
enabled: boolean
@@ -5,8 +5,8 @@
<p class="section-desc">{{ $t('knowledgeEditor.share.description') }}</p>
</div>
<div class="section-body">
<!-- 共享表单 -->
<div class="share-form">
<!-- 共享表单:仅 KB creator 或租户 Admin+ 可见,其他角色看到的是只读列表。 -->
<div v-if="canShare" class="share-form">
<div class="form-item">
<label class="form-label">{{ $t('organization.share.selectOrg') }}</label>
<div class="share-input-row">
@@ -127,7 +127,7 @@
</span>
</div>
</div>
<div class="share-actions">
<div v-if="canShare" class="share-actions">
<t-select
:value="share.permission"
size="small"
@@ -180,9 +180,15 @@ function getOrgForShare(organizationId: string) {
interface Props {
kbId: string
// 后端 POST/PUT/DELETE /knowledge-bases/:id/shares 受 OwnedKBOrAdmin
// 守卫:仅 KB creator 或租户 Admin+ 能改动共享。父组件根据 KB.creator_id
// 计算后传入,缺省 false 表示只读。
canShare?: boolean
}
const props = defineProps<Props>()
const props = withDefaults(defineProps<Props>(), {
canShare: false,
})
const loadingOrgs = ref(false)
const loadingShares = ref(false)