feat(role): 根据role判断用户的权限

This commit is contained in:
sophie committed 2026-09-18 09:21:04 +08:00
1 parent 80d78130f8
commit a24bd94d43
93 files changed
+4472 -387

No files matched your search

@@ -1,34 +1,54 @@
/**
* features/admin/modules/permissions/PermissionsPage.vue
* 用途:应用权限矩阵独立页面(用户 / 真实姓名 / 部门 + Agent 勾选)。
* 依赖:usePermissionsAdmin、BatchPermissionModal、PaginationBar。
* 逻辑:路由 /admin/permissions;左侧用户相关列 sticky,右侧为各应用权限列。
* 用途:应用权限页。已种子权限组时以角色包为主、矩阵只读;否则保留历史按人勾选。
* 依赖:usePermissionsAdmin、RoleEditModal、UserPickerDialog、PaginationBar、roleLabel。
* 逻辑:路由 /admin/permissions;点人数打开选人弹窗,确定后必须确认增减才写库。
*/
<script setup lang="ts">
import { onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Download, FileText, Users } from 'lucide-vue-next';
import { Download, FileText, Plus, Shield, Users } from 'lucide-vue-next';
import BatchPermissionModal from '@/features/admin/modules/users/components/BatchPermissionModal.vue';
import PaginationBar from '@/shared/components/PaginationBar.vue';
import UserPickerDialog from '@/shared/components/UserPickerDialog.vue';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import { adminRoleLabel, LEGACY_ROLE_OPTIONS } from '@/features/admin/utils/roleLabel';
import RoleEditModal from './components/RoleEditModal.vue';
import RoleMembersConfirmDialog from './components/RoleMembersConfirmDialog.vue';
import { usePermissionsAdmin } from './composables/usePermissionsAdmin';
const { t } = useI18n();
const {
agents,
permissionFilters,
selectedUserIds,
paginatedUsers,
permissionPage,
permissionPageSize,
permissionPageSizeOptions,
permissionTotalRows,
permissionTotalPages,
allUsersSelectedOnPage,
isLoading,
const {
agents,
roles,
catalog,
pickerUsers,
permissionFilters,
selectedUserIds,
paginatedUsers,
permissionPage,
permissionPageSize,
permissionPageSizeOptions,
permissionTotalRows,
permissionTotalPages,
allUsersSelectedOnPage,
isLoading,
isSaving,
activeTab,
rolesConfigured,
showBatchPermissionModal,
batchPermissionForm,
showRoleEditor,
editingExisting,
roleDraft,
editingMemberCount,
showRoleSaveConfirm,
pickerRole,
showPicker,
pickerSelectedIds,
membersPreview,
showMembersConfirm,
loadAll,
toggleUserSelection,
toggleAllUsers,
@@ -36,8 +56,19 @@ const { t } = useI18n();
closeBatchPermissionModal,
submitBatchPermission,
toggleUserAgentPermission,
userHasAgent,
exportPermissions,
importPermissions,
openCreateRole,
openEditRole,
closeRoleEditor,
requestSaveRole,
persistRole,
removeRole,
openMembersPicker,
handlePickerConfirm,
cancelMembersConfirm,
applyMembers,
} = usePermissionsAdmin();
onMounted(() => {
@@ -55,11 +86,13 @@ onMounted(() => {
<div class="flex flex-col md:flex-row items-start md:items-center justify-between gap-4">
<div>
<h2 class="section-title">{{ t('admin.perm.title') }}</h2>
<p class="section-desc mt-1">{{ t('admin.perm.subtitle') }}</p>
<p class="section-desc mt-1">
{{ rolesConfigured ? t('admin.permRoles.subtitle') : t('admin.perm.subtitle') }}
</p>
</div>
<div class="flex gap-3">
<div class="flex flex-wrap gap-3">
<button
v-if="selectedUserIds.length > 0"
v-if="!rolesConfigured && selectedUserIds.length > 0"
type="button"
class="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-xl hover:bg-indigo-700 transition-all shadow-lg shadow-indigo-200 active:scale-95"
@click="openBatchPermissionModal"
@@ -68,6 +101,7 @@ onMounted(() => {
<span class="font-medium">{{ t('admin.perm.batchAssign') }}</span>
</button>
<label
v-if="!rolesConfigured"
class="flex items-center gap-2 px-4 py-2 btn-secondary cursor-pointer text-sm font-medium transition-all shadow-sm"
>
<input type="file" accept=".csv" class="hidden" @change="importPermissions" />
@@ -82,128 +116,239 @@ onMounted(() => {
<Download class="w-4 h-4" />
{{ t('admin.perm.exportCsv') }}
</button>
<button
v-if="rolesConfigured"
type="button"
class="flex items-center gap-2 px-4 py-2 bg-brand-600 text-white rounded-xl hover:bg-brand-700 text-sm font-medium"
@click="openCreateRole"
>
<Plus class="w-4 h-4" />
{{ t('admin.permRoles.create') }}
</button>
</div>
</div>
<div class="admin-table-filters--inline">
<input
v-model="permissionFilters.username"
:placeholder="t('admin.perm.filterName')"
class="admin-table-filter-input w-48"
/>
<input
v-model="permissionFilters.department"
:placeholder="t('admin.perm.filterDept')"
class="admin-table-filter-input w-48"
/>
<select v-model="permissionFilters.role" class="admin-table-filter-input w-32">
<option value="">{{ t('admin.perm.allRoles') }}</option>
<option value="user">{{ t('admin.role.user') }}</option>
<option value="hr">{{ t('admin.role.hr') }}</option>
<option value="admin">{{ t('admin.role.admin') }}</option>
</select>
<div v-if="rolesConfigured" class="flex gap-1 border-b border-default">
<button
type="button"
class="px-4 py-2 text-sm font-medium"
:class="activeTab === 'roles' ? 'border-b-2 border-brand-600 text-brand-700' : 'text-muted'"
@click="activeTab = 'roles'"
>
{{ t('admin.permRoles.tabs.roles') }}
</button>
<button
type="button"
class="px-4 py-2 text-sm font-medium"
:class="activeTab === 'matrix' ? 'border-b-2 border-brand-600 text-brand-700' : 'text-muted'"
@click="activeTab = 'matrix'"
>
{{ t('admin.permRoles.tabs.matrix') }}
</button>
</div>
<div class="admin-table-section admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th class="px-4 py-4 w-12 text-center sticky left-0 admin-table-sticky-head z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="allUsersSelectedOnPage"
@change="toggleAllUsers"
/>
</th>
<th
class="px-6 py-4 text-left text-sm sticky left-12 admin-table-sticky-head z-20 w-56"
>
{{ t('admin.perm.headers.user') }}
</th>
<th
class="px-6 py-4 text-left text-sm sticky left-[17rem] admin-table-sticky-head z-20 w-36"
>
{{ t('admin.perm.headers.realName') }}
</th>
<th
class="px-6 py-4 text-left text-sm sticky left-[26rem] admin-table-sticky-head z-20 w-40 border-r border-subtle shadow-[2px_0_5px_-2px_rgba(0,0,0,0.1)]"
>
{{ t('admin.perm.headers.dept') }}
</th>
<th
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center text-sm min-w-[100px]"
>
<div class="flex flex-col items-center gap-1">
<span>{{ agent.name }}</span>
<span class="text-[10px] text-muted font-normal border border-default px-1 rounded">
{{ agent.type }}
</span>
</div>
</th>
</tr>
</thead>
<tbody>
<tr v-for="user in paginatedUsers" :key="user.id">
<td class="px-4 py-4 text-center sticky left-0 admin-table-sticky-cell z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="selectedUserIds.includes(user.id)"
@change="toggleUserSelection(user.id)"
/>
</td>
<td class="px-6 py-4 sticky left-12 admin-table-sticky-cell z-20">
<div class="flex items-center gap-3">
<div
class="w-8 h-8 rounded-full bg-gradient-to-br from-brand-100 to-indigo-100 text-brand-600 flex items-center justify-center font-bold text-sm"
>
{{ user.username.charAt(0).toUpperCase() }}
</div>
<div class="font-medium text-primary text-sm truncate max-w-[120px]" :title="user.username">
{{ user.username }}
</div>
</div>
</td>
<td class="px-6 py-4 sticky left-[17rem] admin-table-sticky-cell z-20">
<div class="text-sm text-primary truncate" :title="user.real_name || '-'">
{{ user.real_name || '-' }}
</div>
</td>
<td
class="px-6 py-4 sticky left-[26rem] admin-table-sticky-cell z-20 border-r border-subtle shadow-[2px_0_5px_-2px_rgba(0,0,0,0.05)]"
>
<div class="text-sm text-muted truncate" :title="user.department">
{{ user.department || '-' }}
</div>
</td>
<td
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center border-l border-dashed border-subtle"
>
<input
type="checkbox"
class="admin-checkbox-lg"
:checked="user.agent_ids.includes(agent.id)"
@change="toggleUserAgentPermission(user, agent.id)"
/>
</td>
</tr>
</tbody>
</table>
<div v-if="rolesConfigured && activeTab === 'roles'" class="space-y-4">
<template v-if="rolesConfigured">
<div class="admin-table-section admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.name') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.description') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.members') }}</th>
<th class="px-6 py-4 text-left text-sm">{{ t('admin.permRoles.headers.apps') }}</th>
<th class="px-6 py-4 text-right text-sm">{{ t('admin.user.headers.actions') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="role in roles" :key="role.code">
<td class="px-6 py-4">
<div class="font-medium text-primary">{{ role.name }}</div>
<div class="mt-1 flex flex-wrap gap-1.5">
<span
v-if="role.is_default"
class="inline-flex rounded-full border border-brand-100 bg-brand-50 px-2 py-0.5 text-[11px] text-brand-700"
>
{{ t('admin.permRoles.badge.default') }}
</span>
<span
v-if="role.is_platform_admin"
class="inline-flex items-center gap-1 rounded-full border border-purple-100 bg-purple-50 px-2 py-0.5 text-[11px] text-purple-700"
>
<Shield class="w-3 h-3" />
{{ t('admin.permRoles.badge.admin') }}
</span>
</div>
</td>
<td class="px-6 py-4 text-sm text-muted max-w-xs">{{ role.description || '-' }}</td>
<td class="px-6 py-4">
<button
v-if="!role.is_platform_admin"
type="button"
class="text-brand-600 hover:text-brand-800 font-semibold"
@click="openMembersPicker(role)"
>
{{ t('admin.permRoles.memberCount', { count: role.member_count }) }}
</button>
<span v-else class="text-sm text-muted">
{{ t('admin.permRoles.memberCount', { count: role.member_count }) }}
</span>
</td>
<td class="px-6 py-4 text-sm text-muted">
{{
role.is_platform_admin
? t('admin.permRoles.allApps')
: (role.agent_names || []).join('、') || '-'
}}
</td>
<td class="px-6 py-4 text-right whitespace-nowrap">
<button
type="button"
class="text-brand-600 hover:text-brand-900 font-medium text-xs px-3 py-1.5 rounded-lg hover:bg-brand-50"
@click="openEditRole(role)"
>
{{ t('admin.edit') }}
</button>
<button
v-if="!role.is_system && !role.is_platform_admin"
type="button"
class="text-red-600 hover:text-red-800 font-medium text-xs px-3 py-1.5 rounded-lg hover:bg-red-50"
@click="removeRole(role)"
>
{{ t('admin.delete') }}
</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
<PaginationBar
v-model:page="permissionPage"
v-model:page-size="permissionPageSize"
:total="permissionTotalRows"
:pages="permissionTotalPages"
:page-size-options="permissionPageSizeOptions"
class="mt-5"
/>
<template v-if="!rolesConfigured || activeTab === 'matrix'">
<p v-if="rolesConfigured" class="text-sm text-muted">{{ t('admin.permRoles.matrixHint') }}</p>
<div class="admin-table-filters--inline">
<input
v-model="permissionFilters.username"
:placeholder="t('admin.perm.filterName')"
class="admin-table-filter-input w-48"
/>
<input
v-model="permissionFilters.department"
:placeholder="t('admin.perm.filterDept')"
class="admin-table-filter-input w-48"
/>
<select v-model="permissionFilters.role" class="admin-table-filter-input w-40">
<option value="">{{ t('admin.perm.allRoles') }}</option>
<option v-for="role in roles" :key="role.code" :value="role.code">{{ role.name }}</option>
<template v-if="!roles.length">
<option
v-for="option in LEGACY_ROLE_OPTIONS"
:key="option.code"
:value="option.code"
>
{{ t(option.i18nKey) }}
</option>
</template>
</select>
</div>
<div class="admin-table-section admin-table-wrap">
<table class="admin-table">
<thead>
<tr>
<th v-if="!rolesConfigured" class="px-4 py-4 w-12 text-center sticky left-0 admin-table-sticky-head z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="allUsersSelectedOnPage"
@change="toggleAllUsers"
/>
</th>
<th class="px-6 py-4 text-left text-sm sticky left-0 admin-table-sticky-head z-20 w-56">
{{ t('admin.perm.headers.user') }}
</th>
<th class="px-6 py-4 text-left text-sm w-36">{{ t('admin.perm.headers.realName') }}</th>
<th class="px-6 py-4 text-left text-sm w-40 border-r border-subtle">
{{ t('admin.perm.headers.dept') }}
</th>
<th class="px-6 py-4 text-left text-sm w-32">{{ t('admin.user.headers.role') }}</th>
<th
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center text-sm min-w-[100px]"
>
<div class="flex flex-col items-center gap-1">
<span>{{ agent.name }}</span>
<span class="text-[10px] text-muted font-normal border border-default px-1 rounded">
{{ agent.type }}
</span>
</div>
</th>
</tr>
</thead>
<tbody>
<tr v-for="user in paginatedUsers" :key="user.id">
<td v-if="!rolesConfigured" class="px-4 py-4 text-center sticky left-0 admin-table-sticky-cell z-20">
<input
type="checkbox"
class="w-4 h-4 rounded text-brand-600 focus:ring-brand-500"
:checked="selectedUserIds.includes(user.id)"
@change="toggleUserSelection(user.id)"
/>
</td>
<td class="px-6 py-4 sticky left-0 admin-table-sticky-cell z-20">
<div class="flex items-center gap-3">
<div
class="w-8 h-8 rounded-full bg-gradient-to-br from-brand-100 to-indigo-100 text-brand-600 flex items-center justify-center font-bold text-sm"
>
{{ user.username.charAt(0).toUpperCase() }}
</div>
<div class="font-medium text-primary text-sm truncate max-w-[120px]" :title="user.username">
{{ user.username }}
</div>
</div>
</td>
<td class="px-6 py-4">
<div class="text-sm text-primary truncate" :title="user.real_name || '-'">
{{ user.real_name || '-' }}
</div>
</td>
<td class="px-6 py-4 border-r border-subtle">
<div class="text-sm text-muted truncate" :title="user.department">
{{ user.department || '-' }}
</div>
</td>
<td class="px-6 py-4 text-sm text-primary">
{{ adminRoleLabel(t, user.role, user.role_name) }}
</td>
<td
v-for="agent in agents"
:key="agent.id"
class="px-4 py-4 text-center border-l border-dashed border-subtle"
>
<input
type="checkbox"
class="admin-checkbox-lg"
:checked="userHasAgent(user, agent.id)"
:disabled="rolesConfigured"
@change="toggleUserAgentPermission(user, agent.id)"
/>
</td>
</tr>
</tbody>
</table>
</div>
<PaginationBar
v-model:page="permissionPage"
v-model:page-size="permissionPageSize"
:total="permissionTotalRows"
:pages="permissionTotalPages"
:page-size-options="permissionPageSizeOptions"
class="mt-5"
/>
</template>
</template>
<BatchPermissionModal
@@ -215,5 +360,44 @@ onMounted(() => {
@close="closeBatchPermissionModal"
@save="submitBatchPermission"
/>
<RoleEditModal
:visible="showRoleEditor"
:draft="roleDraft"
:catalog="catalog"
:is-create="!editingExisting"
:is-platform-admin="Boolean(roles.find((row) => row.code === roleDraft.code)?.is_platform_admin)"
:is-saving="isSaving"
@close="closeRoleEditor"
@save="requestSaveRole"
@update:draft="roleDraft = $event"
/>
<DeleteConfirmDialog
v-model="showRoleSaveConfirm"
:title="t('admin.permRoles.saveConfirm.title')"
:message="t('admin.permRoles.saveConfirm.message', { count: editingMemberCount })"
:confirm-text="t('admin.save')"
confirm-tone="primary"
:confirm-loading="isSaving"
@confirm="persistRole"
/>
<UserPickerDialog
v-model="showPicker"
:users="pickerUsers"
:selected-ids="pickerSelectedIds"
:title="t('admin.permRoles.picker.title', { name: pickerRole?.name || '' })"
:subtitle="t('admin.permRoles.picker.subtitle')"
@confirm="handlePickerConfirm"
/>
<RoleMembersConfirmDialog
:visible="showMembersConfirm"
:preview="membersPreview"
:is-saving="isSaving"
@cancel="cancelMembersConfirm"
@confirm="applyMembers"
/>
</section>
</template>