refactor: 修改之前hardcode的管理员判断

This commit is contained in:
valarchie
2023-02-27 21:44:13 +08:00
parent 9c105a7630
commit 0c860592b7
9 changed files with 27 additions and 91 deletions

View File

@@ -109,8 +109,8 @@ public class LoginController {
*/
@GetMapping("/getRouters")
public ResponseDTO<List<RouterDTO>> getRouters() {
Long userId = AuthenticationUtils.getUserId();
List<RouterDTO> routerTree = menuApplicationService.getRouterTree(userId);
LoginUser loginUser = AuthenticationUtils.getLoginUser();
List<RouterDTO> routerTree = menuApplicationService.getRouterTree(loginUser);
return ResponseDTO.ok(routerTree);
}

View File

@@ -121,12 +121,12 @@ public class MenuApplicationService {
}
public List<Tree<Long>> buildMenuEntityTree(Long userId) {
public List<Tree<Long>> buildMenuEntityTree(LoginUser loginUser) {
List<SysMenuEntity> allMenus;
if (LoginUser.isAdmin(userId)) {
if (loginUser.isAdmin()) {
allMenus = menuService.list();
} else {
allMenus = menuService.getMenuListByUserId(userId);
allMenus = menuService.getMenuListByUserId(loginUser.getUserId());
}
List<SysMenuEntity> noButtonMenus = allMenus.stream()
@@ -184,8 +184,8 @@ public class MenuApplicationService {
}
public List<RouterDTO> getRouterTree(Long userId) {
List<Tree<Long>> trees = buildMenuEntityTree(userId);
public List<RouterDTO> getRouterTree(LoginUser loginUser) {
List<Tree<Long>> trees = buildMenuEntityTree(loginUser);
return buildRouterTree(trees);
}

View File

@@ -114,7 +114,7 @@ public class UserModel extends SysUserEntity {
public void checkCanBeDelete(LoginUser loginUser) {
if (Objects.equals(getUserId(), loginUser.getUserId())
|| LoginUser.isAdmin(getUserId())) {
|| this.getIsAdmin()) {
throw new ApiException(ErrorCode.Business.USER_CURRENT_USER_CAN_NOT_BE_DELETE);
}
}
@@ -137,7 +137,7 @@ public class UserModel extends SysUserEntity {
@Override
public boolean updateById() {
if (LoginUser.isAdmin(this.getUserId()) && AgileBootConfig.isDemoEnabled()) {
if (this.getIsAdmin() && AgileBootConfig.isDemoEnabled()) {
throw new ApiException(Business.USER_ADMIN_CAN_NOT_BE_MODIFY);
}

View File

@@ -34,14 +34,17 @@ public class LoginUser implements UserDetails {
*/
private Long expireTime;
private boolean isAdmin;
/**
* 登录信息
*/
private LoginInfo loginInfo = new LoginInfo();
public LoginUser(Long userId) {
public LoginUser(Long userId, Boolean isAdmin) {
this.userId = userId;
this.isAdmin = isAdmin;
}
public RoleInfo getRoleInfo() {
@@ -118,17 +121,5 @@ public class LoginUser implements UserDetails {
return null;
}
/**
* 是否为管理员
* @return 结果
*/
public boolean isAdmin() {
return isAdmin(getUserId());
}
// TODO 多租户需要做改动
public static boolean isAdmin(Long userId) {
return userId != null && 1L == userId;
}
}

View File

@@ -11,15 +11,12 @@ import com.agileboot.orm.common.enums.UserStatusEnum;
import com.agileboot.orm.common.util.BasicEnumUtil;
import com.agileboot.orm.system.entity.SysMenuEntity;
import com.agileboot.orm.system.entity.SysRoleEntity;
import com.agileboot.orm.system.entity.SysRoleMenuEntity;
import com.agileboot.orm.system.entity.SysUserEntity;
import com.agileboot.orm.system.service.ISysMenuService;
import com.agileboot.orm.system.service.ISysRoleMenuService;
import com.agileboot.orm.system.service.ISysRoleService;
import com.agileboot.orm.system.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@@ -48,9 +45,6 @@ public class UserDetailsServiceImpl implements UserDetailsService {
@NonNull
private ISysUserService userService;
@NonNull
private ISysRoleMenuService roleMenuService;
@NonNull
private ISysMenuService menuService;
@@ -70,7 +64,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
throw new ApiException(ErrorCode.Business.USER_IS_DISABLE, username);
}
return new LoginUser(userEntity.getUserId());
return new LoginUser(userEntity.getUserId(), userEntity.getIsAdmin());
}
public RoleInfo getRoleInfo(Long roleId) {
@@ -113,60 +107,5 @@ public class UserDetailsServiceImpl implements UserDetailsService {
}
/**
* 获取角色数据权限
* @param userId 用户信息
* @return 角色权限信息
*/
public String getRoleKey(Long userId) {
// 管理员拥有所有权限
if (LoginUser.isAdmin(userId)) {
return "admin";
}
SysRoleEntity roleEntity = userService.getRoleOfUser(userId);
return roleEntity == null ? "" : roleEntity.getRoleKey();
}
/**
* 获取菜单数据权限
*
* @param userId 用户信息
* @return 菜单权限信息
*/
public Set<String> getMenuPermissions(Long userId) {
Set<String> perms = new HashSet<>();
// 管理员拥有所有权限
if (LoginUser.isAdmin(userId)) {
perms.add("*:*:*");
} else {
perms.addAll(userService.getMenuPermissionsForUser(userId));
}
return perms;
}
/**
* 获取菜单数据权限
*
* @param userId 用户信息
* @return 菜单权限信息
*/
public Set<Long> getMenuIds(Long userId, Long roleId) {
// 管理员拥有所有菜单
if (LoginUser.isAdmin(userId)) {
LambdaQueryWrapper<SysMenuEntity> menuQuery = Wrappers.lambdaQuery();
menuQuery.select(SysMenuEntity::getMenuId);
List<SysMenuEntity> menuList = menuService.list(menuQuery);
return menuList.stream().map(SysMenuEntity::getMenuId).collect(Collectors.toSet());
} else {
LambdaQueryWrapper<SysRoleMenuEntity> menuQuery = Wrappers.lambdaQuery();
menuQuery.select(SysRoleMenuEntity::getMenuId).eq(SysRoleMenuEntity::getRoleId, roleId);
List<SysRoleMenuEntity> menuList = roleMenuService.list(menuQuery);
return menuList.stream().map(SysRoleMenuEntity::getMenuId).collect(Collectors.toSet());
}
}
}

View File

@@ -217,6 +217,6 @@ INSERT INTO `sys_role_menu` VALUES ('3', '1');
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('1', '1', '1', '4', 'admin', 'valarchie1', '0', 'agileboot@163.com', '15888888889', '0', '', '$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy', '1', '127.0.0.1', '2022-10-06 17:00:06', null, '2022-05-21 08:30:54', '1', '2022-10-06 17:00:06', '管理员', '0');
INSERT INTO `sys_user` VALUES ('2', '2', '2', '5', 'ag1', 'valarchie2', '0', 'agileboot1@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', null, '2022-05-21 08:30:54', null, null, '测试员1', '0');
INSERT INTO `sys_user` VALUES ('3', '2', '0', '5', 'ag2', 'valarchie3', '0', 'agileboot2@qq.com', '15666666667', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', null, '2022-05-21 08:30:54', null, null, '测试员2', '0');
INSERT INTO `sys_user` VALUES ('1', '1', '1', '4', 'admin', 'valarchie1', '0', 'agileboot@163.com', '15888888889', '0', '', '$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy', '1', '127.0.0.1', '2022-10-06 17:00:06', 1, null, '2022-05-21 08:30:54', '1', '2022-10-06 17:00:06', '管理员', '0');
INSERT INTO `sys_user` VALUES ('2', '2', '2', '5', 'ag1', 'valarchie2', '0', 'agileboot1@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员1', '0');
INSERT INTO `sys_user` VALUES ('3', '2', '0', '5', 'ag2', 'valarchie3', '0', 'agileboot2@qq.com', '15666666667', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员2', '0');

View File

@@ -175,6 +175,7 @@ create table sys_user
status smallint default 0 not null comment '帐号状态1正常 2停用 3冻结',
login_ip varchar(128) default '' null comment '最后登录IP',
login_date datetime null comment '最后登录时间',
is_admin tinyint default 0 not null comment '超级管理员标志1是0否',
creator_id bigint null comment '更新者ID',
create_time datetime null comment '创建时间',
updater_id bigint null comment '更新者ID',

View File

@@ -18,7 +18,7 @@ import lombok.Setter;
* </p>
*
* @author valarchie
* @since 2022-10-02
* @since 2023-02-27
*/
@Getter
@Setter
@@ -88,6 +88,10 @@ public class SysUserEntity extends BaseEntity<SysUserEntity> {
@TableField("login_date")
private Date loginDate;
@ApiModelProperty("超级管理员标志1是0否")
@TableField("is_admin")
private Boolean isAdmin;
@ApiModelProperty("备注")
@TableField("remark")
private String remark;

View File

@@ -410,6 +410,7 @@ CREATE TABLE `sys_user` (
`status` smallint NOT NULL DEFAULT '0' COMMENT '帐号状态1正常 2停用 3冻结',
`login_ip` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '最后登录IP',
`login_date` datetime DEFAULT NULL COMMENT '最后登录时间',
`is_admin` tinyint(1) NOT NULL DEFAULT '0' COMMENT '超级管理员标志1是0否',
`creator_id` bigint DEFAULT NULL COMMENT '更新者ID',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`updater_id` bigint DEFAULT NULL COMMENT '更新者ID',
@@ -422,6 +423,6 @@ CREATE TABLE `sys_user` (
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('1', '1', '1', '4', 'admin', 'valarchie1', '0', 'agileboot@163.com', '15888888889', '0', '', '$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy', '1', '127.0.0.1', '2022-10-06 17:00:06', null, '2022-05-21 08:30:54', '1', '2022-10-06 17:00:06', '管理员', '0');
INSERT INTO `sys_user` VALUES ('2', '2', '2', '5', 'ag1', 'valarchie2', '0', 'agileboot1@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', null, '2022-05-21 08:30:54', null, null, '测试员1', '0');
INSERT INTO `sys_user` VALUES ('3', '2', '0', '5', 'ag2', 'valarchie3', '0', 'agileboot2@qq.com', '15666666667', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', null, '2022-05-21 08:30:54', null, null, '测试员2', '0');
INSERT INTO `sys_user` VALUES ('1', '1', '1', '4', 'admin', 'valarchie1', '0', 'agileboot@163.com', '15888888889', '0', '', '$2a$10$rb1wRoEIkLbIknREEN1LH.FGs4g0oOS5t6l5LQ793nRaFO.SPHDHy', '1', '127.0.0.1', '2022-10-06 17:00:06', 1, null, '2022-05-21 08:30:54', '1', '2022-10-06 17:00:06', '管理员', '0');
INSERT INTO `sys_user` VALUES ('2', '2', '2', '5', 'ag1', 'valarchie2', '0', 'agileboot1@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员1', '0');
INSERT INTO `sys_user` VALUES ('3', '2', '0', '5', 'ag2', 'valarchie3', '0', 'agileboot2@qq.com', '15666666667', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '1', '127.0.0.1', '2022-05-21 08:30:54', 0, null, '2022-05-21 08:30:54', null, null, '测试员2', '0');