权限修改
This commit is contained in:
@@ -148,7 +148,7 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
if (!isAllowAccountLogin) {
|
||||
throw new ApiNotPrintException("当前分店账号被禁止登录");
|
||||
}
|
||||
StpKit.USER.login(user.getId(), user.getAccount(), shopInfo.getId(), shopInfo.getShopName(), isStaff ? MyStpLogic.LoginType.STAFF : MyStpLogic.LoginType.MANAGER, user.getIsAdmin());
|
||||
StpKit.USER.login(user.getId(), user.getAccount(), shopInfo.getId(), shopInfo.getShopName(), isStaff ? MyStpLogic.LoginType.STAFF : MyStpLogic.LoginType.MANAGER, user.getIsAdmin(), platType);
|
||||
// 查询角色
|
||||
List<SysRole> roleList = sysRoleService.getByUserId(user.getId());
|
||||
List<String> roleNames = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
|
||||
@@ -156,7 +156,7 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
roleNames.add("admin");
|
||||
}
|
||||
// 权限赋予
|
||||
List<String> promissionList = sysMenuMapper.selectByUserId(user.getId(), null).stream().map(SysMenu::getPermission).filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
||||
List<String> promissionList = sysMenuMapper.selectByUserId(user.getId(), "PC".equals(platType) ? 1 : 0).stream().map(SysMenu::getPermission).filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
||||
// 加入员工权限
|
||||
if (shopStaffPromissionList != null && !shopStaffPromissionList.isEmpty()) {
|
||||
promissionList.addAll(shopStaffPromissionList);
|
||||
|
||||
@@ -124,6 +124,7 @@ public class PadProdServiceImpl implements PadProdService {
|
||||
return padDetailDTO;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean add(Long shopId, PadDetailAddDTO padDetailAddDTO) {
|
||||
long count = shopProdCategoryService.count(new QueryWrapper().eq(ShopProdCategory::getId, padDetailAddDTO.getProductCategoryId()));
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.czg.service.account.service.impl;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.czg.account.dto.menu.MenuAddDTO;
|
||||
import com.czg.account.dto.menu.MenuEditDTO;
|
||||
import com.czg.account.entity.SysMenu;
|
||||
@@ -38,7 +39,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
@Override
|
||||
public List<MenuVO> getMenu() {
|
||||
long sysUserId = StpKit.USER.getLoginIdAsLong();
|
||||
List<SysMenu> allMenus = mapper.selectByUserId(sysUserId, null);
|
||||
List<SysMenu> allMenus = mapper.selectByUserId(sysUserId, "PC".equals(StpKit.USER.getPlatForm()) ? 1 : 0);
|
||||
List<SysUsersRoles> roleList = sysUsersRolesMapper.selectListByQuery(query().select(SysUsersRoles::getRoleId).eq(SysUsersRoles::getUserId, sysUserId));
|
||||
List<Long> roleIdList = roleList.stream().map(SysUsersRoles::getRoleId).toList();
|
||||
Long shopId = StpKit.USER.getShopId(0L);
|
||||
@@ -98,7 +99,11 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
if (menuAddDTO.getType() == 3 && menuAddDTO.getPermission() == null) {
|
||||
throw new ApiNotPrintException("权限不为空");
|
||||
}
|
||||
return save(BeanUtil.copyProperties(menuAddDTO, SysMenu.class));
|
||||
SysMenu sysMenu = BeanUtil.copyProperties(menuAddDTO, SysMenu.class);
|
||||
if (menuAddDTO.getApiInfo() != null && !menuAddDTO.getApiInfo().isEmpty()) {
|
||||
sysMenu.setApiInfo(JSONObject.toJSONString(menuAddDTO.getApiInfo()));
|
||||
}
|
||||
return save(sysMenu);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -109,6 +114,9 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
throw new ApiNotPrintException("菜单不存在");
|
||||
}
|
||||
BeanUtil.copyProperties(menuEditDTO, menu);
|
||||
if (menuEditDTO.getApiInfo() != null && !menuEditDTO.getApiInfo().isEmpty()) {
|
||||
menu.setApiInfo(JSONObject.toJSONString(menuEditDTO.getApiInfo()));
|
||||
}
|
||||
return updateById(menu);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,12 @@ package com.czg.service.account.service.impl;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.czg.account.dto.PageDTO;
|
||||
import com.czg.account.dto.menu.MenuApiInfoItemDTO;
|
||||
import com.czg.account.dto.role.RoleAddDTO;
|
||||
import com.czg.account.dto.role.RoleEditDTO;
|
||||
import com.czg.account.dto.role.RolePermissionDTO;
|
||||
import com.czg.account.entity.SysMenu;
|
||||
import com.czg.account.entity.SysRole;
|
||||
import com.czg.account.entity.SysRolesMenus;
|
||||
@@ -24,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.mybatisflex.core.query.QueryMethods.column;
|
||||
|
||||
@@ -45,6 +49,48 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
|
||||
return mapper.selectByUserId(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将单个路径通配符转为 SQL LIKE 模式
|
||||
*/
|
||||
public static String toSqlLikePattern(String pathPattern) {
|
||||
if (pathPattern == null) return null;
|
||||
|
||||
// 转义 SQL 特殊字符
|
||||
String escaped = pathPattern
|
||||
.replace("\\", "\\\\")
|
||||
.replace("_", "\\_")
|
||||
.replace("%", "\\%");
|
||||
|
||||
// 替换 ** 为占位,避免被 * 干扰
|
||||
String placeholder = "@@DOUBLE_STAR@@";
|
||||
escaped = escaped.replace("**", placeholder);
|
||||
|
||||
// 替换 * 为单层路径匹配
|
||||
escaped = escaped.replace("*", "%");
|
||||
|
||||
// 替换 ? 为单字符匹配
|
||||
escaped = escaped.replace("?", "_");
|
||||
|
||||
// 替换回 **
|
||||
escaped = escaped.replace(placeholder, "%");
|
||||
|
||||
return escaped;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接多个路径通配符为 SQL OR 条件(适用于 MyBatis-Plus apply)
|
||||
*/
|
||||
public static String buildLikeSql(List<MenuApiInfoItemDTO> apiInfoItemDTOS, String columnName) {
|
||||
// 永不成立
|
||||
if (apiInfoItemDTOS == null || apiInfoItemDTOS.isEmpty()) {
|
||||
return "1 = 0";
|
||||
}
|
||||
|
||||
return apiInfoItemDTOS.stream()
|
||||
.map(pattern -> StrUtil.format("({} LIKE '{}' ESCAPE '\\\\' and method = '{}')" , columnName, toSqlLikePattern(pattern.getUrl()), pattern.getMethod()))
|
||||
.collect(Collectors.joining(" OR "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<SysRole> getList(Long shopId, PageDTO pageDTO, String key, String startTime, String endTime) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
@@ -70,27 +116,69 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getRoleMenu(long userId, Integer id) {
|
||||
public List<Long> getRoleMenu(long userId, Integer id, Integer type) {
|
||||
SysRole role = queryChain().eq(SysRole::getId, id).eq(SysRole::getCreateUserId, userId).one();
|
||||
if (role == null) {
|
||||
throw new ApiNotPrintException("角色不存在");
|
||||
}
|
||||
return sysRolesMenusService.queryChain().eq(SysRolesMenus::getRoleId, id).list().stream().map(SysRolesMenus::getMenuId).toList();
|
||||
return sysRolesMenusService.queryChain().eq(SysRolesMenus::getRoleId, id).eq(SysRolesMenus::getType, type).list().stream().map(SysRolesMenus::getMenuId).toList();
|
||||
}
|
||||
|
||||
public boolean addMenu(Long roleId, List<Long> menuIds) {
|
||||
long count = sysMenuService.queryChain().in(SysMenu::getMenuId, menuIds).count();
|
||||
if (count != menuIds.size()) {
|
||||
public boolean addMenu(Long roleId, List<Long> menuIds, boolean isAdmin) {
|
||||
List<SysMenu> sysMenuList = sysMenuService.queryChain().in(SysMenu::getMenuId, menuIds).list();
|
||||
if (sysMenuList.size() != menuIds.size()) {
|
||||
throw new ApiNotPrintException("菜单id包含错误id");
|
||||
}
|
||||
|
||||
ArrayList<MenuApiInfoItemDTO> apiPathList = new ArrayList<>();
|
||||
sysMenuList.forEach(item -> {
|
||||
if (StrUtil.isNotBlank(item.getApiInfo())) {
|
||||
List<MenuApiInfoItemDTO> itemDTOS = JSONArray.parseArray(item.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class);
|
||||
if (!itemDTOS.isEmpty()) {
|
||||
apiPathList.addAll(itemDTOS);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!apiPathList.isEmpty()) {
|
||||
String string = buildLikeSql(apiPathList, "url");
|
||||
QueryWrapper wrapper = new QueryWrapper();
|
||||
wrapper.where(string);
|
||||
List<SysMenu> sysMenus = sysMenuService.list(wrapper);
|
||||
if (!sysMenus.isEmpty()) {
|
||||
sysMenuList.addAll(sysMenus);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<SysRolesMenus> rolesMenus = new ArrayList<>();
|
||||
for (Long id : menuIds) {
|
||||
rolesMenus.add(new SysRolesMenus(id, roleId));
|
||||
for (SysMenu sysMenu : sysMenuList) {
|
||||
long count = sysRolesMenusService.count(new QueryWrapper().eq(SysRolesMenus::getMenuId, sysMenu.getMenuId())
|
||||
.eq(SysRolesMenus::getRoleId, roleId)
|
||||
.eq(SysRolesMenus::getType, isAdmin ? 0 : 1));
|
||||
if (count == 0) {
|
||||
rolesMenus.add(new SysRolesMenus(sysMenu.getMenuId(), roleId, isAdmin ? 0 : 1));
|
||||
}
|
||||
}
|
||||
return sysRolesMenusService.saveBatch(rolesMenus);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean editPermission(long userId, RolePermissionDTO rolePermissionDTO) {
|
||||
long count = count(new QueryWrapper().eq(SysRole::getId, rolePermissionDTO.getRoleId()).eq(SysRole::getCreateUserId, userId));
|
||||
if (count == 0) {
|
||||
throw new ApiNotPrintException("角色不存在");
|
||||
}
|
||||
|
||||
if (rolePermissionDTO.getAdminMenuIdList() != null && !rolePermissionDTO.getAdminMenuIdList().isEmpty()) {
|
||||
addMenu(rolePermissionDTO.getRoleId(), rolePermissionDTO.getAdminMenuIdList(), true);
|
||||
}
|
||||
|
||||
if (rolePermissionDTO.getCashMenuIdList() != null && !rolePermissionDTO.getCashMenuIdList().isEmpty()) {
|
||||
addMenu(rolePermissionDTO.getRoleId(), rolePermissionDTO.getCashMenuIdList(), false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean add(RoleAddDTO roleAddDTO) {
|
||||
@@ -110,7 +198,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
|
||||
sysRole.setCreateTime(DateUtil.date().toLocalDateTime());
|
||||
boolean save = save(sysRole);
|
||||
if (save) {
|
||||
return addMenu(sysRole.getId(), roleAddDTO.menuIdList());
|
||||
return addMenu(sysRole.getId(), roleAddDTO.menuIdList(), true);
|
||||
}
|
||||
|
||||
throw new ApiNotPrintException("保存失败");
|
||||
@@ -134,7 +222,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
|
||||
boolean b = updateById(role);
|
||||
if (b) {
|
||||
sysRolesMenusService.updateChain().eq(SysRolesMenus::getRoleId, role.getId()).remove();
|
||||
return addMenu(role.getId(), roleEditDTO.getMenuIdList());
|
||||
return addMenu(role.getId(), roleEditDTO.getMenuIdList(), true);
|
||||
}
|
||||
throw new ApiNotPrintException("保存失败");
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
|
||||
userInfo.setLastLoginTime(DateUtil.date().toLocalDateTime());
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
// StpKit.USER.login(userInfo.getId());
|
||||
StpKit.USER.login(userInfo.getId(), openId, null, null, MyStpLogic.LoginType.USER, false);
|
||||
StpKit.USER.login(userInfo.getId(), openId, null, null, MyStpLogic.LoginType.USER, false, "userMini");
|
||||
return new LoginTokenDTO(StpKit.USER.getTokenValue(), userInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
left join sys_menu as c on c.menu_id = b.menu_id
|
||||
where a.user_id = #{userId} and c.menu_id is not null
|
||||
<if test="type != null">
|
||||
and c.type=#{type}
|
||||
and b.type=#{type}
|
||||
</if>
|
||||
order by menu_sort desc
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user