添加对于用户的组织、角色分配
This commit is contained in:
@@ -5,6 +5,8 @@ import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 新增或修改角色。
|
||||
*/
|
||||
@@ -25,4 +27,7 @@ public class RoleSaveRequest {
|
||||
|
||||
/** 1启用 0停用 */
|
||||
private Integer status;
|
||||
|
||||
/** 顶部导航编码。空列表表示该角色看不到任何顶部栏目。 */
|
||||
private List<String> menuCodes;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 角色。内置角色不可删除,编码供登录令牌使用。
|
||||
@@ -38,6 +40,13 @@ public class SysRole implements Serializable {
|
||||
@Schema(description = "1内置 0自建")
|
||||
private Integer builtin;
|
||||
|
||||
@Schema(description = "1表示顶部导航已初始化")
|
||||
private Integer menuInited;
|
||||
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "可访问的顶部导航编码")
|
||||
private List<String> menuCodes = new ArrayList<>();
|
||||
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.banben.modules.admin.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 角色与顶部导航的对应关系。
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_role_menu")
|
||||
public class SysRoleMenu implements Serializable {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private Long roleId;
|
||||
|
||||
private String menuCode;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.banben.modules.admin.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 角色导航权限。
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> {
|
||||
}
|
||||
@@ -5,15 +5,22 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.banben.common.BusinessException;
|
||||
import com.banben.modules.admin.dto.RoleSaveRequest;
|
||||
import com.banben.modules.admin.entity.SysRole;
|
||||
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||
import com.banben.modules.admin.mapper.SysRoleMapper;
|
||||
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||
import com.banben.modules.admin.service.RoleService;
|
||||
import com.banben.modules.auth.entity.SysUser;
|
||||
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||
import com.banben.modules.auth.nav.NavMenus;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色增删改。ADMIN、USER 为内置角色。
|
||||
@@ -23,13 +30,28 @@ import java.util.List;
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
|
||||
private final SysRoleMapper roleMapper;
|
||||
private final SysRoleMenuMapper roleMenuMapper;
|
||||
private final SysUserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public List<SysRole> listAll() {
|
||||
return roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
|
||||
List<SysRole> roles = roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
|
||||
.orderByDesc(SysRole::getBuiltin)
|
||||
.orderByAsc(SysRole::getId));
|
||||
Map<Long, List<String>> menus = roleMenuMapper.selectList(new LambdaQueryWrapper<>()).stream()
|
||||
.collect(Collectors.groupingBy(SysRoleMenu::getRoleId,
|
||||
Collectors.mapping(SysRoleMenu::getMenuCode, Collectors.toList())));
|
||||
for (SysRole role : roles) {
|
||||
if (isAdminCode(role)) {
|
||||
role.setMenuCodes(new ArrayList<>(NavMenus.CODES));
|
||||
continue;
|
||||
}
|
||||
role.setMenuCodes(menus.getOrDefault(role.getId(), List.of()).stream()
|
||||
.filter(NavMenus::contains)
|
||||
.distinct()
|
||||
.toList());
|
||||
}
|
||||
return roles;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,6 +60,8 @@ public class RoleServiceImpl implements RoleService {
|
||||
role.setBuiltin(0);
|
||||
fill(role, request, null);
|
||||
roleMapper.insert(role);
|
||||
replaceMenus(role.getId(), request.getMenuCodes());
|
||||
markMenusInited(role.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,6 +83,13 @@ public class RoleServiceImpl implements RoleService {
|
||||
.set(SysUser::getRole, role.getCode())
|
||||
.eq(SysUser::getRoleId, id));
|
||||
}
|
||||
if (isAdminCode(role)) {
|
||||
replaceMenus(id, NavMenus.CODES);
|
||||
markMenusInited(id);
|
||||
} else if (request.getMenuCodes() != null) {
|
||||
replaceMenus(id, request.getMenuCodes());
|
||||
markMenusInited(id);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,6 +103,7 @@ public class RoleServiceImpl implements RoleService {
|
||||
throw new BusinessException("该角色下还有用户,不能删除");
|
||||
}
|
||||
roleMapper.deleteById(id);
|
||||
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,8 +113,12 @@ public class RoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
private void ensure(String code, String name, String remark) {
|
||||
Long count = roleMapper.selectCount(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, code));
|
||||
if (count != null && count > 0) {
|
||||
SysRole existing = roleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getCode, code));
|
||||
if (existing != null) {
|
||||
if ("ADMIN".equalsIgnoreCase(code)) {
|
||||
replaceMenus(existing.getId(), NavMenus.CODES);
|
||||
markMenusInited(existing.getId());
|
||||
}
|
||||
return;
|
||||
}
|
||||
SysRole role = new SysRole();
|
||||
@@ -92,6 +128,8 @@ public class RoleServiceImpl implements RoleService {
|
||||
role.setStatus(1);
|
||||
role.setBuiltin(1);
|
||||
roleMapper.insert(role);
|
||||
replaceMenus(role.getId(), NavMenus.CODES);
|
||||
markMenusInited(role.getId());
|
||||
}
|
||||
|
||||
private void fill(SysRole role, RoleSaveRequest request, Long selfId) {
|
||||
@@ -121,4 +159,35 @@ public class RoleServiceImpl implements RoleService {
|
||||
private boolean isBuiltin(SysRole role) {
|
||||
return role.getBuiltin() != null && role.getBuiltin() == 1;
|
||||
}
|
||||
|
||||
private boolean isAdminCode(SysRole role) {
|
||||
return role.getCode() != null && "ADMIN".equalsIgnoreCase(role.getCode());
|
||||
}
|
||||
|
||||
private void replaceMenus(Long roleId, List<String> menuCodes) {
|
||||
List<String> codes = menuCodes == null ? new ArrayList<>(NavMenus.CODES) : normalize(menuCodes);
|
||||
roleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
|
||||
for (String code : codes) {
|
||||
SysRoleMenu row = new SysRoleMenu();
|
||||
row.setRoleId(roleId);
|
||||
row.setMenuCode(code);
|
||||
roleMenuMapper.insert(row);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> normalize(List<String> menuCodes) {
|
||||
LinkedHashSet<String> codes = new LinkedHashSet<>();
|
||||
for (String code : menuCodes) {
|
||||
if (NavMenus.contains(code)) {
|
||||
codes.add(code);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(codes);
|
||||
}
|
||||
|
||||
private void markMenusInited(Long roleId) {
|
||||
roleMapper.update(null, new LambdaUpdateWrapper<SysRole>()
|
||||
.eq(SysRole::getId, roleId)
|
||||
.set(SysRole::getMenuInited, 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,59 @@
|
||||
package com.banben.modules.admin.support;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.banben.common.BusinessException;
|
||||
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||
import com.banben.modules.auth.context.LoginUser;
|
||||
import com.banben.modules.auth.context.UserContext;
|
||||
import com.banben.modules.auth.entity.SysUser;
|
||||
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 组织、角色、用户管理只允许管理员调用。
|
||||
* 后台管理接口:角色勾选了「后台管理」栏目才能调用。
|
||||
* 权限表暂时不可用时,仍放行角色编码为 ADMIN 的账号,避免把管理员锁在外面。
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AdminAccess {
|
||||
|
||||
private final SysUserMapper userMapper;
|
||||
private final SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
public void require() {
|
||||
LoginUser user = UserContext.get();
|
||||
if (user == null || user.role() == null || !"ADMIN".equalsIgnoreCase(user.role())) {
|
||||
throw new BusinessException(403, "需要管理员权限");
|
||||
LoginUser login = UserContext.get();
|
||||
if (login == null || login.id() == null || !allowed(login.id(), login.role())) {
|
||||
throw new BusinessException(403, "需要后台管理权限");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean allowed(Long userId, String roleCode) {
|
||||
if (isAdminCode(roleCode)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
SysUser user = userMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
if (isAdminCode(user.getRole())) {
|
||||
return true;
|
||||
}
|
||||
if (user.getRoleId() == null) {
|
||||
return false;
|
||||
}
|
||||
Long count = roleMenuMapper.selectCount(new LambdaQueryWrapper<SysRoleMenu>()
|
||||
.eq(SysRoleMenu::getRoleId, user.getRoleId())
|
||||
.eq(SysRoleMenu::getMenuCode, "admin"));
|
||||
return count != null && count > 0;
|
||||
} catch (Exception ignored) {
|
||||
return isAdminCode(roleCode);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAdminCode(String roleCode) {
|
||||
return roleCode != null && "ADMIN".equalsIgnoreCase(roleCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 认证接口。
|
||||
*/
|
||||
@@ -34,6 +36,12 @@ public class AuthController {
|
||||
return Result.success(authService.currentUser());
|
||||
}
|
||||
|
||||
@Operation(summary = "当前用户可见的顶部导航")
|
||||
@GetMapping("/menus")
|
||||
public Result<List<String>> menus() {
|
||||
return Result.success(authService.currentMenus());
|
||||
}
|
||||
|
||||
@Operation(summary = "登出")
|
||||
@PostMapping("/logout")
|
||||
public Result<Void> logout() {
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.banben.modules.auth.dto;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 登录响应。
|
||||
*/
|
||||
@@ -27,4 +29,7 @@ public class LoginResponse {
|
||||
|
||||
@Schema(description = "所属单位")
|
||||
private String orgName;
|
||||
|
||||
@Schema(description = "可见的顶部导航编码")
|
||||
private List<String> menus;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.banben.modules.auth.nav;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 顶部导航。编码与前端路由一致,角色按编码授权。
|
||||
*/
|
||||
public final class NavMenus {
|
||||
|
||||
public static final List<String> CODES = List.of(
|
||||
"dashboard",
|
||||
"search",
|
||||
"hotwords",
|
||||
"special",
|
||||
"deposit",
|
||||
"analysis",
|
||||
"resource",
|
||||
"collect",
|
||||
"governance",
|
||||
"admin"
|
||||
);
|
||||
|
||||
private NavMenus() {
|
||||
}
|
||||
|
||||
public static boolean contains(String code) {
|
||||
return code != null && CODES.contains(code);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ import com.banben.modules.auth.dto.LoginRequest;
|
||||
import com.banben.modules.auth.dto.LoginResponse;
|
||||
import com.banben.modules.auth.entity.SysUser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 认证服务。
|
||||
*/
|
||||
@@ -18,4 +20,9 @@ public interface AuthService {
|
||||
* 查询当前登录用户完整信息。
|
||||
*/
|
||||
SysUser currentUser();
|
||||
|
||||
/**
|
||||
* 当前用户可见的顶部导航编码。
|
||||
*/
|
||||
List<String> currentMenus();
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@ import com.banben.modules.auth.context.UserContext;
|
||||
import com.banben.modules.auth.dto.LoginRequest;
|
||||
import com.banben.modules.auth.dto.LoginResponse;
|
||||
import com.banben.modules.admin.entity.SysRole;
|
||||
import com.banben.modules.admin.entity.SysRoleMenu;
|
||||
import com.banben.modules.admin.mapper.SysRoleMapper;
|
||||
import com.banben.modules.admin.mapper.SysRoleMenuMapper;
|
||||
import com.banben.modules.auth.entity.SysUser;
|
||||
import com.banben.modules.auth.mapper.SysUserMapper;
|
||||
import com.banben.modules.auth.nav.NavMenus;
|
||||
import com.banben.modules.auth.service.AuthService;
|
||||
import com.banben.modules.auth.util.JwtUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -17,6 +20,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 认证服务实现。
|
||||
@@ -28,6 +33,7 @@ public class AuthServiceImpl implements AuthService {
|
||||
|
||||
private final SysUserMapper sysUserMapper;
|
||||
private final SysRoleMapper sysRoleMapper;
|
||||
private final SysRoleMenuMapper roleMenuMapper;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtUtil jwtUtil;
|
||||
|
||||
@@ -63,9 +69,23 @@ public class AuthServiceImpl implements AuthService {
|
||||
response.setRealName(user.getRealName());
|
||||
response.setRole(user.getRole());
|
||||
response.setOrgName(user.getOrgName());
|
||||
response.setMenus(menusOf(user));
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> currentMenus() {
|
||||
Long userId = UserContext.currentUserId();
|
||||
if (userId == null) {
|
||||
throw new BusinessException("未登录");
|
||||
}
|
||||
SysUser user = sysUserMapper.selectById(userId);
|
||||
if (user == null) {
|
||||
throw new BusinessException("未登录");
|
||||
}
|
||||
return menusOf(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser currentUser() {
|
||||
Long userId = UserContext.currentUserId();
|
||||
@@ -78,4 +98,35 @@ public class AuthServiceImpl implements AuthService {
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private List<String> menusOf(SysUser user) {
|
||||
if (isAdmin(user)) {
|
||||
return new ArrayList<>(NavMenus.CODES);
|
||||
}
|
||||
if (user.getRoleId() == null) {
|
||||
return new ArrayList<>(NavMenus.CODES);
|
||||
}
|
||||
try {
|
||||
return roleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, user.getRoleId()))
|
||||
.stream()
|
||||
.map(SysRoleMenu::getMenuCode)
|
||||
.filter(NavMenus::contains)
|
||||
.distinct()
|
||||
.toList();
|
||||
} catch (Exception e) {
|
||||
log.warn("读取导航权限失败,暂时开放全部栏目:{}", e.getMessage());
|
||||
return new ArrayList<>(NavMenus.CODES);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAdmin(SysUser user) {
|
||||
if (user.getRole() != null && "ADMIN".equalsIgnoreCase(user.getRole())) {
|
||||
return true;
|
||||
}
|
||||
if (user.getRoleId() == null) {
|
||||
return false;
|
||||
}
|
||||
SysRole role = sysRoleMapper.selectById(user.getRoleId());
|
||||
return role != null && "ADMIN".equalsIgnoreCase(role.getCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
-- 角色可见的顶部导航。已有角色默认拥有全部栏目,之后可在角色管理里取消勾选。
|
||||
-- menu_inited=1 后再次执行不会把已清空的栏目补回来。
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_role_menu
|
||||
(
|
||||
id BIGSERIAL NOT NULL PRIMARY KEY,
|
||||
role_id BIGINT NOT NULL,
|
||||
menu_code VARCHAR(32) NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uk_sys_role_menu ON sys_role_menu (role_id, menu_code);
|
||||
|
||||
COMMENT ON TABLE sys_role_menu IS '角色可访问的顶部导航编码';
|
||||
COMMENT ON COLUMN sys_role_menu.menu_code IS '导航编码,如 dashboard、search';
|
||||
|
||||
ALTER TABLE sys_role ADD COLUMN IF NOT EXISTS menu_inited SMALLINT DEFAULT 0;
|
||||
COMMENT ON COLUMN sys_role.menu_inited IS '1表示顶部导航已按角色保存过,重复执行不再补全';
|
||||
|
||||
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||
SELECT r.id, m.code
|
||||
FROM sys_role r
|
||||
CROSS JOIN (
|
||||
VALUES ('dashboard'), ('search'), ('hotwords'), ('special'),
|
||||
('deposit'), ('analysis'), ('resource'), ('collect'), ('governance')
|
||||
) AS m(code)
|
||||
WHERE r.deleted = 0
|
||||
AND COALESCE(r.menu_inited, 0) = 0
|
||||
AND NOT EXISTS (SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id);
|
||||
|
||||
UPDATE sys_role
|
||||
SET menu_inited = 1
|
||||
WHERE deleted = 0
|
||||
AND COALESCE(menu_inited, 0) = 0;
|
||||
@@ -0,0 +1,10 @@
|
||||
-- 后台管理成为顶部导航的一项。已有的系统管理员默认拥有该栏目,其它角色需在角色管理里勾选。
|
||||
|
||||
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||
SELECT r.id, 'admin'
|
||||
FROM sys_role r
|
||||
WHERE r.deleted = 0
|
||||
AND upper(r.code) = 'ADMIN'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id AND x.menu_code = 'admin'
|
||||
);
|
||||
@@ -0,0 +1,15 @@
|
||||
-- 系统管理员补齐全部顶部栏目。可重复执行,已有的不会重复插入。
|
||||
|
||||
INSERT INTO sys_role_menu (role_id, menu_code)
|
||||
SELECT r.id, m.code
|
||||
FROM sys_role r
|
||||
CROSS JOIN (
|
||||
VALUES ('dashboard'), ('search'), ('hotwords'), ('special'),
|
||||
('deposit'), ('analysis'), ('resource'), ('collect'),
|
||||
('governance'), ('admin')
|
||||
) AS m(code)
|
||||
WHERE r.deleted = 0
|
||||
AND upper(r.code) = 'ADMIN'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM sys_role_menu x WHERE x.role_id = r.id AND x.menu_code = m.code
|
||||
);
|
||||
Reference in New Issue
Block a user