权限的管理和校验修改
This commit is contained in:
@@ -136,55 +136,32 @@ router.beforeEach((to, _from, next) => {
|
||||
try {
|
||||
const adminUserInfo = JSON.parse(adminUserInfoStr)
|
||||
console.log('Parsed admin user info:', adminUserInfo)
|
||||
// 管理员用户(type === 4)拥有所有权限
|
||||
if (adminUserInfo.type === 4) {
|
||||
console.log('Admin user, allowing access')
|
||||
next()
|
||||
}
|
||||
// 店长用户(type === 2)拥有所有权限
|
||||
else if (adminUserInfo.type === 2) {
|
||||
console.log('Shop manager user, allowing access')
|
||||
next()
|
||||
}
|
||||
// 美甲师用户(type === 3)需要检查权限
|
||||
else if (adminUserInfo.type === 3) {
|
||||
console.log('美甲师 user, checking permissions')
|
||||
// 获取用户权限
|
||||
const userPermissionsStr = localStorage.getItem('userPermissions')
|
||||
console.log('User permissions:', userPermissionsStr)
|
||||
if (userPermissionsStr) {
|
||||
const userPermissions = JSON.parse(userPermissionsStr)
|
||||
// 检查用户是否拥有该权限
|
||||
if (userPermissions.includes(permissionCode)) {
|
||||
console.log('User has required permission:', permissionCode)
|
||||
next()
|
||||
} else {
|
||||
console.log('User missing required permission:', permissionCode)
|
||||
// 权限不足,检查是否是首页
|
||||
if (to.path === '/admin') {
|
||||
// 如果是首页,允许访问(作为默认页面)
|
||||
console.log('Allowing access to home as default page')
|
||||
next()
|
||||
} else {
|
||||
// 对于其他页面,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
}
|
||||
// 所有用户类型都根据实际权限检查
|
||||
console.log('Checking permissions for user type:', adminUserInfo.type)
|
||||
// 获取用户权限
|
||||
const userPermissionsStr = localStorage.getItem('userPermissions')
|
||||
console.log('User permissions:', userPermissionsStr)
|
||||
if (userPermissionsStr) {
|
||||
const userPermissions = JSON.parse(userPermissionsStr)
|
||||
// 检查用户是否拥有该权限
|
||||
if (userPermissions.includes(permissionCode)) {
|
||||
console.log('User has required permission:', permissionCode)
|
||||
next()
|
||||
} else {
|
||||
console.log('No user permissions found')
|
||||
// 没有权限信息,允许访问首页临时,直到权限加载完成
|
||||
if (to.path === '/admin') {
|
||||
console.log('Allowing access to home to load permissions')
|
||||
next()
|
||||
} else {
|
||||
// 对于其他页面,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
console.log('User missing required permission:', permissionCode)
|
||||
// 权限不足,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
} else {
|
||||
console.log('Unknown user type, redirecting to login')
|
||||
// 未知用户类型,重定向到登录页
|
||||
next('/admin/login')
|
||||
console.log('No user permissions found')
|
||||
// 没有权限信息,允许访问首页临时,直到权限加载完成
|
||||
if (to.path === '/admin') {
|
||||
console.log('Allowing access to home to load permissions')
|
||||
next()
|
||||
} else {
|
||||
// 对于其他页面,重定向到首页
|
||||
next('/admin')
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error('Failed to parse user info:', parseError)
|
||||
|
||||
@@ -148,20 +148,12 @@ const selectedKeys = ref<string[]>([route.path])
|
||||
const filteredMenuItems = computed(() => {
|
||||
// 访问 userPermissions 以建立依赖关系
|
||||
userPermissions.value
|
||||
// 管理员用户(type === 4)拥有所有权限
|
||||
if (userInfo.value?.type === 4) {
|
||||
return menuItems
|
||||
}
|
||||
// 其他用户类型根据实际权限过滤菜单
|
||||
// 所有用户类型都根据实际权限过滤菜单
|
||||
return menuItems.filter(item => {
|
||||
// 空权限码的菜单项(如首页)总是可见
|
||||
if (!item.permissionCode) {
|
||||
return true
|
||||
}
|
||||
// 美甲师用户(type === 3)不显示仪表盘
|
||||
if (userInfo.value?.type === 3 && item.key === '/admin/dashboard') {
|
||||
return false
|
||||
}
|
||||
return hasPermission(item.permissionCode)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
<a-tabs v-model:activeKey="activeTab">
|
||||
<a-tab-pane key="permissions" tab="权限列表">
|
||||
<template #extra>
|
||||
<a-button v-if="currentLoginUser?.type === 4 || hasPermission('permission:create')" type="primary" @click="showAddPermissionModal">
|
||||
<a-button v-if="hasPermission('permission:create')" type="primary" @click="showAddPermissionModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -28,11 +28,11 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button v-if="currentLoginUser?.type === 4 || hasPermission('permission:edit')" type="link" size="small" @click="showEditPermissionModal(record)">
|
||||
<a-button v-if="hasPermission('permission:edit')" type="link" size="small" @click="showEditPermissionModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="currentLoginUser?.type === 4 || hasPermission('permission:delete')"
|
||||
v-if="hasPermission('permission:delete')"
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDeletePermission(record)"
|
||||
>
|
||||
@@ -46,7 +46,7 @@
|
||||
</a-table>
|
||||
</a-tab-pane>
|
||||
|
||||
<a-tab-pane key="user-permissions" tab="用户权限分配">
|
||||
<a-tab-pane v-if="hasPermission('permission:assign')" key="user-permissions" tab="用户权限分配">
|
||||
<a-row :gutter="16">
|
||||
<a-col :span="8">
|
||||
<a-card title="选择用户" :bordered="false">
|
||||
@@ -86,7 +86,7 @@
|
||||
:bordered="false"
|
||||
>
|
||||
<template #extra>
|
||||
<a-button v-if="selectedUser && (currentLoginUser?.type === 4 || hasPermission('permission:assign'))" type="primary" @click="handleSaveUserPermissions">
|
||||
<a-button v-if="selectedUser && hasPermission('permission:assign')" type="primary" @click="handleSaveUserPermissions">
|
||||
保存权限
|
||||
</a-button>
|
||||
</template>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button v-if="isAdmin || hasPermission('product:create')" type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('product:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -58,11 +58,11 @@
|
||||
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button v-if="isAdmin || hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="isAdmin || hasPermission('product:delete')"
|
||||
v-if="hasPermission('product:delete')"
|
||||
title="确定要删除这个商品吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button v-if="isAdmin || hasPermission('shop:create')" type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('shop:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -52,11 +52,11 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button v-if="isAdmin || hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="isAdmin || hasPermission('shop:delete')"
|
||||
v-if="hasPermission('shop:delete')"
|
||||
title="确定要删除这个店铺吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button v-if="isAdmin || hasPermission('user:create')" type="primary" @click="showAddModal">
|
||||
<a-button v-if="hasPermission('user:create')" type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
@@ -48,14 +48,14 @@
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button v-if="isAdmin || hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
<a-button v-if="hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-button v-if="isAdmin || hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)">
|
||||
<a-button v-if="hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)">
|
||||
重置密码
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
v-if="isAdmin || hasPermission('user:delete')"
|
||||
v-if="hasPermission('user:delete')"
|
||||
title="确定要删除这个用户吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
|
||||
@@ -332,3 +332,107 @@ create index idx_vip_level
|
||||
comment '按VIP等级查询的索引';
|
||||
|
||||
|
||||
|
||||
-- 清空现有权限数据(可选,根据需要执行)
|
||||
DELETE FROM t_user_permission WHERE 1=1;
|
||||
DELETE FROM t_permission WHERE 1=1;
|
||||
|
||||
-- 插入完整的系统权限
|
||||
INSERT INTO t_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) VALUES
|
||||
-- 首页权限
|
||||
('home:view', '查看首页', '首页', '查看系统首页', NOW(), NOW(), 0),
|
||||
|
||||
-- 仪表盘权限
|
||||
('dashboard:view', '查看仪表盘', '仪表盘', '查看系统仪表盘', NOW(), NOW(), 0),
|
||||
|
||||
-- 用户管理权限
|
||||
('user:view', '查看用户', '用户管理', '查看用户列表和详情', NOW(), NOW(), 0),
|
||||
('user:create', '新增用户', '用户管理', '新增用户', NOW(), NOW(), 0),
|
||||
('user:edit', '编辑用户', '用户管理', '编辑用户信息', NOW(), NOW(), 0),
|
||||
('user:reset_password', '重置密码', '用户管理', '重置用户密码', NOW(), NOW(), 0),
|
||||
('user:delete', '删除用户', '用户管理', '删除用户', NOW(), NOW(), 0),
|
||||
|
||||
-- 商品管理权限
|
||||
('product:view', '查看商品', '商品管理', '查看商品列表和详情', NOW(), NOW(), 0),
|
||||
('product:create', '新增商品', '商品管理', '新增商品', NOW(), NOW(), 0),
|
||||
('product:edit', '编辑商品', '商品管理', '编辑商品信息', NOW(), NOW(), 0),
|
||||
('product:delete', '删除商品', '商品管理', '删除商品', NOW(), NOW(), 0),
|
||||
('product:category:manage', '管理商品分类', '商品管理', '管理商品分类', NOW(), NOW(), 0),
|
||||
|
||||
-- 店铺管理权限
|
||||
('shop:view', '查看店铺', '店铺管理', '查看店铺列表和详情', NOW(), NOW(), 0),
|
||||
('shop:create', '新增店铺', '店铺管理', '新增店铺', NOW(), NOW(), 0),
|
||||
('shop:edit', '编辑店铺', '店铺管理', '编辑店铺信息', NOW(), NOW(), 0),
|
||||
('shop:delete', '删除店铺', '店铺管理', '删除店铺', NOW(), NOW(), 0),
|
||||
('shop:staff:manage', '管理店铺员工', '店铺管理', '管理店铺员工', NOW(), NOW(), 0),
|
||||
|
||||
-- 权限管理权限
|
||||
('permission:view', '查看权限', '权限管理', '查看权限列表', NOW(), NOW(), 0),
|
||||
('permission:create', '新增权限', '权限管理', '新增系统权限', NOW(), NOW(), 0),
|
||||
('permission:edit', '编辑权限', '权限管理', '编辑系统权限', NOW(), NOW(), 0),
|
||||
('permission:delete', '删除权限', '权限管理', '删除系统权限', NOW(), NOW(), 0),
|
||||
('permission:assign', '分配权限', '权限管理', '为用户分配权限', NOW(), NOW(), 0);
|
||||
|
||||
|
||||
-- 为管理员(type=4)分配所有权限
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
|
||||
SELECT
|
||||
u.id as user_id,
|
||||
p.id as permission_id,
|
||||
NOW() as create_time,
|
||||
NOW() as update_time
|
||||
FROM t_user u
|
||||
CROSS JOIN t_permission p
|
||||
WHERE u.type = 4
|
||||
AND p.is_delete = 0;
|
||||
|
||||
-- 为店长(type=2)分配默认权限
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
|
||||
SELECT
|
||||
u.id as user_id,
|
||||
p.id as permission_id,
|
||||
NOW() as create_time,
|
||||
NOW() as update_time
|
||||
FROM t_user u
|
||||
CROSS JOIN t_permission p
|
||||
WHERE u.type = 2
|
||||
AND p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'home:view',
|
||||
'dashboard:view',
|
||||
'user:view',
|
||||
'user:create',
|
||||
'user:edit',
|
||||
'user:reset_password',
|
||||
'user:delete',
|
||||
'product:view',
|
||||
'product:create',
|
||||
'product:edit',
|
||||
'product:delete',
|
||||
'product:category:manage',
|
||||
'shop:view',
|
||||
'shop:edit',
|
||||
'permission:view',
|
||||
'permission:assign'
|
||||
);
|
||||
|
||||
|
||||
-- 为美甲师(type=3)分配默认权限
|
||||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time)
|
||||
SELECT
|
||||
u.id as user_id,
|
||||
p.id as permission_id,
|
||||
NOW() as create_time,
|
||||
NOW() as update_time
|
||||
FROM t_user u
|
||||
CROSS JOIN t_permission p
|
||||
WHERE u.type = 3
|
||||
AND p.is_delete = 0
|
||||
AND p.permission_code IN (
|
||||
'home:view',
|
||||
'user:view',
|
||||
'user:reset_password',
|
||||
'product:view',
|
||||
'product:create',
|
||||
'product:edit'
|
||||
);
|
||||
Reference in New Issue
Block a user