修复:同步店长和美甲师默认后台权限
This commit is contained in:
+178
-26
@@ -29,8 +29,12 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -47,6 +51,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
|
||||
private static final String DELETED_PHONE_MARKER = "_deleted_";
|
||||
private static final Long SYSTEM_PERMISSION_ASSIGNER_ID = 0L;
|
||||
private static final List<String> ADMIN_DEFAULT_PERMISSION_CODES = List.of(
|
||||
"home:view",
|
||||
"dashboard:view",
|
||||
@@ -83,6 +88,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
"coupon:view",
|
||||
"order:view"
|
||||
);
|
||||
private static final Set<String> MANAGED_DEFAULT_PERMISSION_CODES = buildManagedDefaultPermissionCodes();
|
||||
private static final Map<String, PermissionSeed> DEFAULT_PERMISSION_SEEDS = buildDefaultPermissionSeeds();
|
||||
|
||||
// 注入用户数据访问层、JWT工具类和店铺服务
|
||||
private final UserMapper userMapper;
|
||||
@@ -94,6 +101,161 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
private final VerificationCodeService verificationCodeService;
|
||||
|
||||
private static Set<String> buildManagedDefaultPermissionCodes() {
|
||||
LinkedHashSet<String> permissionCodes = new LinkedHashSet<>();
|
||||
permissionCodes.addAll(ADMIN_DEFAULT_PERMISSION_CODES);
|
||||
permissionCodes.addAll(SHOP_MANAGER_DEFAULT_PERMISSION_CODES);
|
||||
permissionCodes.addAll(NAIL_ARTIST_DEFAULT_PERMISSION_CODES);
|
||||
return Set.copyOf(permissionCodes);
|
||||
}
|
||||
|
||||
private static Map<String, PermissionSeed> buildDefaultPermissionSeeds() {
|
||||
Map<String, PermissionSeed> seeds = new HashMap<>();
|
||||
seeds.put("home:view", new PermissionSeed("查看首页", "首页", "查看系统首页入口与概览"));
|
||||
seeds.put("dashboard:view", new PermissionSeed("查看仪表盘", "仪表盘", "查看后台仪表盘统计"));
|
||||
seeds.put("user:view", new PermissionSeed("查看用户", "用户管理", "查看用户列表与详情"));
|
||||
seeds.put("user:create", new PermissionSeed("新增用户", "用户管理", "创建用户账号"));
|
||||
seeds.put("user:edit", new PermissionSeed("编辑用户", "用户管理", "修改用户基础信息"));
|
||||
seeds.put("user:disable", new PermissionSeed("启停用户", "用户管理", "启用或禁用用户账号"));
|
||||
seeds.put("user:reset_password", new PermissionSeed("重置密码", "用户管理", "重置用户密码"));
|
||||
seeds.put("user:delete", new PermissionSeed("删除用户", "用户管理", "删除用户账号"));
|
||||
seeds.put("user:balance:view", new PermissionSeed("查看余额资产", "用户资产", "查看用户余额、累计充值与流水"));
|
||||
seeds.put("user:balance:manage", new PermissionSeed("调整余额", "用户资产", "充值、手工加减余额与关联订单"));
|
||||
seeds.put("user:vip:manage", new PermissionSeed("管理VIP", "用户资产", "手工设置VIP等级或恢复自动判级"));
|
||||
seeds.put("product:view", new PermissionSeed("查看商品", "商品管理", "查看商品列表与详情"));
|
||||
seeds.put("product:create", new PermissionSeed("新增商品", "商品管理", "创建商品"));
|
||||
seeds.put("product:edit", new PermissionSeed("编辑商品", "商品管理", "编辑商品信息"));
|
||||
seeds.put("product:delete", new PermissionSeed("删除商品", "商品管理", "删除商品"));
|
||||
seeds.put("product:category:manage", new PermissionSeed("管理商品分类", "商品管理", "维护商品分类与归类信息"));
|
||||
seeds.put("shop:view", new PermissionSeed("查看店铺", "店铺管理", "查看店铺列表与详情"));
|
||||
seeds.put("shop:create", new PermissionSeed("新增店铺", "店铺管理", "创建店铺"));
|
||||
seeds.put("shop:edit", new PermissionSeed("编辑店铺", "店铺管理", "编辑店铺信息"));
|
||||
seeds.put("shop:delete", new PermissionSeed("删除店铺", "店铺管理", "删除店铺"));
|
||||
seeds.put("shop:staff:manage", new PermissionSeed("管理员工", "店铺管理", "管理店铺员工与归属"));
|
||||
seeds.put("permission:view", new PermissionSeed("查看权限", "权限管理", "查看权限清单"));
|
||||
seeds.put("permission:create", new PermissionSeed("新增权限", "权限管理", "新增系统权限定义"));
|
||||
seeds.put("permission:edit", new PermissionSeed("编辑权限", "权限管理", "编辑系统权限定义"));
|
||||
seeds.put("permission:delete", new PermissionSeed("删除权限", "权限管理", "删除系统权限定义"));
|
||||
seeds.put("permission:assign", new PermissionSeed("分配权限", "权限管理", "给用户分配后台权限"));
|
||||
seeds.put("tag:view", new PermissionSeed("查看标签", "标签管理", "查看标签列表与详情"));
|
||||
seeds.put("tag:create", new PermissionSeed("新增标签", "标签管理", "创建标签"));
|
||||
seeds.put("tag:edit", new PermissionSeed("编辑标签", "标签管理", "编辑标签"));
|
||||
seeds.put("tag:delete", new PermissionSeed("删除标签", "标签管理", "删除标签"));
|
||||
seeds.put("tag:assign", new PermissionSeed("分配标签", "标签管理", "给商品或优惠券分配标签"));
|
||||
seeds.put("coupon:view", new PermissionSeed("查看优惠券", "优惠券管理", "查看优惠券与发放情况"));
|
||||
seeds.put("coupon:create", new PermissionSeed("新增优惠券", "优惠券管理", "创建优惠券模板"));
|
||||
seeds.put("coupon:edit", new PermissionSeed("编辑优惠券", "优惠券管理", "编辑优惠券模板"));
|
||||
seeds.put("coupon:delete", new PermissionSeed("删除优惠券", "优惠券管理", "删除优惠券模板"));
|
||||
seeds.put("coupon:assign", new PermissionSeed("发放优惠券", "优惠券管理", "向用户发放普通优惠券"));
|
||||
seeds.put("order:view", new PermissionSeed("查看订单", "订单管理", "查看订单列表与详情"));
|
||||
seeds.put("order:process", new PermissionSeed("处理订单", "订单管理", "处理订单状态或由美甲师完结订单"));
|
||||
seeds.put("order:edit", new PermissionSeed("编辑订单", "订单管理", "编辑订单信息的预留权限"));
|
||||
seeds.put("order:delete", new PermissionSeed("删除订单", "订单管理", "删除订单的预留权限"));
|
||||
seeds.put("nail_artist:view", new PermissionSeed("查看美甲师", "美甲师管理", "查看美甲师信息与排班入口"));
|
||||
seeds.put("nail_artist:edit", new PermissionSeed("编辑美甲师", "美甲师管理", "编辑美甲师信息"));
|
||||
return Map.copyOf(seeds);
|
||||
}
|
||||
|
||||
private void synchronizeManagedDefaultPermissions(Long userId, Integer userType) {
|
||||
List<String> desiredPermissionCodes = getDefaultPermissionCodes(userType);
|
||||
List<PermissionDO> managedPermissions = permissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
.in(PermissionDO::getPermissionCode, MANAGED_DEFAULT_PERMISSION_CODES)
|
||||
);
|
||||
|
||||
if (managedPermissions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Long> permissionIdByCode = managedPermissions.stream()
|
||||
.collect(Collectors.toMap(PermissionDO::getPermissionCode, PermissionDO::getId, (left, right) -> left));
|
||||
List<Long> managedPermissionIds = managedPermissions.stream()
|
||||
.map(PermissionDO::getId)
|
||||
.collect(Collectors.toList());
|
||||
Set<Long> desiredPermissionIds = desiredPermissionCodes.stream()
|
||||
.map(permissionIdByCode::get)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<UserPermissionDO> currentPermissions = userPermissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(UserPermissionDO.class)
|
||||
.eq(UserPermissionDO::getUserId, userId)
|
||||
.in(UserPermissionDO::getPermissionId, managedPermissionIds)
|
||||
);
|
||||
|
||||
Set<Long> currentPermissionIds = currentPermissions.stream()
|
||||
.map(UserPermissionDO::getPermissionId)
|
||||
.collect(Collectors.toSet());
|
||||
List<Long> stalePermissionIds = currentPermissions.stream()
|
||||
.map(UserPermissionDO::getPermissionId)
|
||||
.filter(permissionId -> !desiredPermissionIds.contains(permissionId))
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!stalePermissionIds.isEmpty()) {
|
||||
userPermissionMapper.delete(
|
||||
Wrappers.lambdaQuery(UserPermissionDO.class)
|
||||
.eq(UserPermissionDO::getUserId, userId)
|
||||
.in(UserPermissionDO::getPermissionId, stalePermissionIds)
|
||||
);
|
||||
}
|
||||
|
||||
for (Long permissionId : desiredPermissionIds) {
|
||||
if (currentPermissionIds.contains(permissionId)) {
|
||||
continue;
|
||||
}
|
||||
UserPermissionDO userPermissionDO = new UserPermissionDO();
|
||||
userPermissionDO.setUserId(userId);
|
||||
userPermissionDO.setPermissionId(permissionId);
|
||||
userPermissionDO.setCreateBy(SYSTEM_PERMISSION_ASSIGNER_ID);
|
||||
userPermissionMapper.insert(userPermissionDO);
|
||||
}
|
||||
}
|
||||
|
||||
private void ensurePermissionDefinitions() {
|
||||
List<PermissionDO> existingPermissions = permissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
.in(PermissionDO::getPermissionCode, MANAGED_DEFAULT_PERMISSION_CODES)
|
||||
);
|
||||
|
||||
Set<String> existingCodes = existingPermissions.stream()
|
||||
.map(PermissionDO::getPermissionCode)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
for (String permissionCode : MANAGED_DEFAULT_PERMISSION_CODES) {
|
||||
if (existingCodes.contains(permissionCode)) {
|
||||
continue;
|
||||
}
|
||||
PermissionSeed permissionSeed = DEFAULT_PERMISSION_SEEDS.get(permissionCode);
|
||||
if (permissionSeed == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PermissionDO permissionDO = new PermissionDO();
|
||||
permissionDO.setPermissionCode(permissionCode);
|
||||
permissionDO.setPermissionName(permissionSeed.permissionName());
|
||||
permissionDO.setPermissionGroup(permissionSeed.permissionGroup());
|
||||
permissionDO.setDescription(permissionSeed.description());
|
||||
permissionDO.setIsDelete(0);
|
||||
permissionMapper.insert(permissionDO);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getDefaultPermissionCodes(Integer userType) {
|
||||
return switch (userType == null ? -1 : userType) {
|
||||
case 4 -> ADMIN_DEFAULT_PERMISSION_CODES;
|
||||
case 2 -> SHOP_MANAGER_DEFAULT_PERMISSION_CODES;
|
||||
case 3 -> NAIL_ARTIST_DEFAULT_PERMISSION_CODES;
|
||||
default -> List.of();
|
||||
};
|
||||
}
|
||||
|
||||
private void assignDefaultPermissions(Long userId, Integer userType) {
|
||||
syncDefaultPermissions(userId, userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新用户
|
||||
* @param userDTO 用户数据传输对象
|
||||
@@ -176,33 +338,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
* @param userId 用户ID
|
||||
* @param userType 用户类型
|
||||
*/
|
||||
private void assignDefaultPermissions(Long userId, Integer userType) {
|
||||
List<String> permissionCodes = switch (userType == null ? -1 : userType) {
|
||||
case 4 -> ADMIN_DEFAULT_PERMISSION_CODES;
|
||||
case 2 -> SHOP_MANAGER_DEFAULT_PERMISSION_CODES;
|
||||
case 3 -> NAIL_ARTIST_DEFAULT_PERMISSION_CODES;
|
||||
default -> List.of();
|
||||
};
|
||||
|
||||
if (permissionCodes.isEmpty()) {
|
||||
private void syncDefaultPermissions(Long userId, Integer userType) {
|
||||
if (userId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<PermissionDO> permissions = permissionMapper.selectList(
|
||||
Wrappers.lambdaQuery(PermissionDO.class)
|
||||
.eq(PermissionDO::getIsDelete, 0)
|
||||
.in(PermissionDO::getPermissionCode, permissionCodes)
|
||||
);
|
||||
|
||||
if (!permissions.isEmpty()) {
|
||||
for (PermissionDO permission : permissions) {
|
||||
UserPermissionDO userPermissionDO = new UserPermissionDO();
|
||||
userPermissionDO.setUserId(userId);
|
||||
userPermissionDO.setPermissionId(permission.getId());
|
||||
userPermissionDO.setCreateBy(userId); // 设置创建人为当前用户
|
||||
userPermissionMapper.insert(userPermissionDO);
|
||||
}
|
||||
}
|
||||
ensurePermissionDefinitions();
|
||||
synchronizeManagedDefaultPermissions(userId, userType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,6 +354,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
* @return 更新是否成功
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean updateUser(Long id, UserDTO userDTO) {
|
||||
// 查询用户是否存在
|
||||
UserDO userDO = userMapper.selectById(id);
|
||||
@@ -507,11 +650,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setPassword(ENCODED_UNSET_PASSWORD);
|
||||
|
||||
userMapper.insert(userDO);
|
||||
assignDefaultPermissions(userDO.getId(), userDO.getType());
|
||||
syncDefaultPermissions(userDO.getId(), userDO.getType());
|
||||
return userDO;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public LoginResponseVO adminLogin(LoginRequestDTO loginRequestDTO) {
|
||||
UserDO userDO = userMapper.selectOne(
|
||||
Wrappers.lambdaQuery(UserDO.class)
|
||||
@@ -529,6 +673,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("您没有后台管理权限");
|
||||
}
|
||||
|
||||
syncDefaultPermissions(userDO.getId(), userType);
|
||||
|
||||
Long resolvedShopId = resolveAdminLoginShopId(userDO, loginRequestDTO.getShopId());
|
||||
Long currentShopId = normalizeShopId(userDO.getShopId());
|
||||
if (!Objects.equals(resolvedShopId, currentShopId)) {
|
||||
@@ -705,7 +851,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
}
|
||||
|
||||
userDO.setUpdateTime(LocalDateTime.now());
|
||||
return userMapper.updateById(userDO) > 0;
|
||||
boolean updated = userMapper.updateById(userDO) > 0;
|
||||
if (updated) {
|
||||
syncDefaultPermissions(userDO.getId(), userDO.getType());
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -942,4 +1092,6 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("手机号已被使用");
|
||||
}
|
||||
}
|
||||
private record PermissionSeed(String permissionName, String permissionGroup, String description) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# 2026-03-21 店长与美甲师默认后台权限同步
|
||||
|
||||
## 问题背景
|
||||
|
||||
- 新增店长、美甲师账号后,部分账号进入后台会出现“没有权限”或后台菜单为空。
|
||||
- 现有逻辑虽然在创建用户时尝试分配默认权限,但对以下场景兜底不够:
|
||||
- 数据库里缺少对应的 `t_permission` 种子数据
|
||||
- 旧账号在修复前已经创建完成,后续登录时不会自动补齐权限
|
||||
- 用户类型变更后,默认权限没有同步刷新
|
||||
|
||||
## 本次处理
|
||||
|
||||
- 在 [UserServiceImpl.java](/E:/code/xiaowang/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/service/impl/UserServiceImpl.java) 中补充默认权限种子定义。
|
||||
- 新增“默认权限同步”逻辑:
|
||||
- 自动补齐店长、美甲师、管理员默认权限所依赖的权限定义
|
||||
- 创建用户时同步默认权限
|
||||
- 后台登录时对已有后台账号补做一次默认权限同步
|
||||
- 编辑用户后,如果角色发生变化,会重新同步默认权限
|
||||
- 系统自动分配的默认权限统一用系统标识 `create_by = 0` 写入,便于区分系统默认赋权。
|
||||
|
||||
## 影响说明
|
||||
|
||||
- 新创建的店长、美甲师账号会自动获得默认后台权限。
|
||||
- 之前已经创建但缺权限的后台账号,在下一次后台登录时会自动补齐默认权限。
|
||||
- 这次没有改前端页面和数据库结构,也不需要你手工执行 SQL。
|
||||
|
||||
## 验证
|
||||
|
||||
- 已执行:`mvn -q -DskipTests compile`
|
||||
- 结果:通过
|
||||
Reference in New Issue
Block a user