From b7d9a2036defd214d4b368ddb2c12989a91c370e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Thu, 7 Nov 2024 16:51:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=92=E8=89=B2=E6=9D=83=E9=99=90?= =?UTF-8?q?=E5=88=86=E9=85=8D=E5=8A=9F=E8=83=BD=E5=85=A8=E9=83=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../harmony/controller/AuthController.java | 26 ++++++- .../harmony/domain/entity/SystemMenu.java | 18 ----- .../harmony/domain/entity/SystemRoleMenu.java | 67 +++++++++++++++++++ .../harmony/domain/vo/MenuVO.java | 22 ------ .../harmony/domain/vo/SystemRoleMenuVO.java | 19 ++++++ .../harmony/mapper/RoleMapper.java | 2 + .../harmony/mapper/SystemMapper.java | 17 ++++- .../harmony/service/AuthService.java | 7 ++ .../harmony/service/impl/AuthServiceImpl.java | 43 +++++++++++- .../service/impl/SystemServiceImpl.java | 6 +- .../harmony/service/impl/UserServiceImpl.java | 5 -- 11 files changed, 177 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/com/guaiguailang/harmony/domain/entity/SystemMenu.java create mode 100644 src/main/java/com/guaiguailang/harmony/domain/entity/SystemRoleMenu.java delete mode 100644 src/main/java/com/guaiguailang/harmony/domain/vo/MenuVO.java create mode 100644 src/main/java/com/guaiguailang/harmony/domain/vo/SystemRoleMenuVO.java diff --git a/src/main/java/com/guaiguailang/harmony/controller/AuthController.java b/src/main/java/com/guaiguailang/harmony/controller/AuthController.java index 43b4687..a88dcfd 100644 --- a/src/main/java/com/guaiguailang/harmony/controller/AuthController.java +++ b/src/main/java/com/guaiguailang/harmony/controller/AuthController.java @@ -5,7 +5,9 @@ import cn.dev33.satoken.util.SaResult; import com.guaiguailang.harmony.domain.dto.ParamLogin; import com.guaiguailang.harmony.domain.entity.MenuDataItem; import com.guaiguailang.harmony.domain.entity.SystemRole; +import com.guaiguailang.harmony.domain.entity.SystemRoleMenu; import com.guaiguailang.harmony.domain.vo.ResponseResult; +import com.guaiguailang.harmony.domain.vo.SystemRoleMenuVO; import com.guaiguailang.harmony.service.AuthService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -91,7 +93,7 @@ public class AuthController { } @Operation( summary = "角色列表(所有)", - description = "获取所有角色列表,仅限管理员操作的接口", + description = "获取所有角色列表", tags = {"权限相关接口"} ) @GetMapping("/role-all") @@ -124,4 +126,26 @@ public class AuthController { public ResponseEntity delRoleRealy(@RequestBody SystemRole systemRole) { return ResponseEntity.ok(authService.delRoleRealy(systemRole)); } + @GetMapping("/role-menu-list") + public ResponseEntity getMenuByRole(@RequestParam String roleId) { + return ResponseEntity.ok(authService.getMenuByRole(roleId)); + } + @Operation( + summary = "角色功能 删除", + description = "删除角色拥有的功能", + tags = {"权限相关接口"} + ) + @DeleteMapping("/role-menu-del") + public ResponseEntity delRoleMenu(@RequestBody SystemRoleMenu srm) { + return ResponseEntity.ok(authService.delRoleMenu(srm)); + } + @Operation( + summary = "角色功能新增", + description = "新增角色可以用的菜单", + tags = {"权限相关接口"} + ) + @PostMapping("/role-menu-add") + public ResponseEntity addRoleMenu(@RequestBody SystemRoleMenu srm) { + return ResponseEntity.ok(authService.addRoleMenu(srm)); + } } diff --git a/src/main/java/com/guaiguailang/harmony/domain/entity/SystemMenu.java b/src/main/java/com/guaiguailang/harmony/domain/entity/SystemMenu.java deleted file mode 100644 index 640f0d6..0000000 --- a/src/main/java/com/guaiguailang/harmony/domain/entity/SystemMenu.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.guaiguailang.harmony.domain.entity; - -import lombok.Data; - -@Data -public class SystemMenu { - - private Long id; - private String title; - private String key; - private String name; - private String component; - private String redirect; - private String icon; - private int status; - private String parent; - private int order; -} diff --git a/src/main/java/com/guaiguailang/harmony/domain/entity/SystemRoleMenu.java b/src/main/java/com/guaiguailang/harmony/domain/entity/SystemRoleMenu.java new file mode 100644 index 0000000..b27f521 --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/domain/entity/SystemRoleMenu.java @@ -0,0 +1,67 @@ +package com.guaiguailang.harmony.domain.entity; + + +public class SystemRoleMenu { + + private long id; + private String roleId; + private String menuId; + private String actionIds; + private String about; + private long status; + + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + + public String getRoleId() { + return roleId; + } + + public void setRoleId(String roleId) { + this.roleId = roleId; + } + + + public String getMenuId() { + return menuId; + } + + public void setMenuId(String menuId) { + this.menuId = menuId; + } + + + public String getActionIds() { + return actionIds; + } + + public void setActionIds(String actionIds) { + this.actionIds = actionIds; + } + + + public String getAbout() { + return about; + } + + public void setAbout(String about) { + this.about = about; + } + + + public long getStatus() { + return status; + } + + public void setStatus(long status) { + this.status = status; + } + +} diff --git a/src/main/java/com/guaiguailang/harmony/domain/vo/MenuVO.java b/src/main/java/com/guaiguailang/harmony/domain/vo/MenuVO.java deleted file mode 100644 index 746e4ee..0000000 --- a/src/main/java/com/guaiguailang/harmony/domain/vo/MenuVO.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.guaiguailang.harmony.domain.vo; - -import lombok.Data; - -import java.util.List; - -@Data -public class MenuVO { - //暂时作废 - private Long id; - private String title; - private String key; - private String name; - private String component; - private String redirect; - private String icon; - private int status; - private String parent; - private int order; - - private List children; -} diff --git a/src/main/java/com/guaiguailang/harmony/domain/vo/SystemRoleMenuVO.java b/src/main/java/com/guaiguailang/harmony/domain/vo/SystemRoleMenuVO.java new file mode 100644 index 0000000..8503ecb --- /dev/null +++ b/src/main/java/com/guaiguailang/harmony/domain/vo/SystemRoleMenuVO.java @@ -0,0 +1,19 @@ +package com.guaiguailang.harmony.domain.vo; + +import lombok.Data; + +import java.util.List; + +@Data +public class SystemRoleMenuVO { + private long id; + private String roleId; + private String menuId; + private String actionIds; + private String about; + private long status; + + private String title; + + +} diff --git a/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java index 5374e63..b4f4705 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/RoleMapper.java @@ -11,4 +11,6 @@ import java.util.List; public interface RoleMapper extends BaseMapper { @Select("SELECT * FROM system_role") List getRoleAll(); + @Select("SELECT * FROM system_role where level>system_role.level") + List getRole(int level); } diff --git a/src/main/java/com/guaiguailang/harmony/mapper/SystemMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/SystemMapper.java index 831bcad..da947dc 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/SystemMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/SystemMapper.java @@ -2,7 +2,12 @@ package com.guaiguailang.harmony.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.guaiguailang.harmony.domain.entity.MenuDataItem; -import com.guaiguailang.harmony.domain.entity.SystemMenu; +import com.guaiguailang.harmony.domain.entity.SystemRole; +import com.guaiguailang.harmony.domain.entity.SystemRoleMenu; +import com.guaiguailang.harmony.domain.vo.SystemRoleMenuVO; +import lombok.Data; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @@ -24,5 +29,13 @@ public interface SystemMapper extends BaseMapper { */ @Select("SELECT * from menu_data_item") List getMenusAll(); - + @Select("SELECT * from system_role_menu join menu_data_item on menu_id=menu_data_item.id where role_id=#{roleId}") + List getMneuByRoleId(String roleId); + @Delete("delete from system_role_menu where id=#{id}") + int deleteRoleMenuById(long id); + @Insert("INSERT INTO system_role_menu ( role_id, menu_id, action_ids, about, status) " + + "VALUES (#{roleId}, #{menuId}, #{actionIds}, #{about}, #{status})") + int addRoleMenu(SystemRoleMenu srm); + @Select("SELECT * from system_role_menu where role_id=#{roleId} and menu_id=#{menuId}") + List roleMenuIsin(String roleId, String menuId); } diff --git a/src/main/java/com/guaiguailang/harmony/service/AuthService.java b/src/main/java/com/guaiguailang/harmony/service/AuthService.java index 7d5fd38..7ceacd9 100644 --- a/src/main/java/com/guaiguailang/harmony/service/AuthService.java +++ b/src/main/java/com/guaiguailang/harmony/service/AuthService.java @@ -2,6 +2,7 @@ package com.guaiguailang.harmony.service; import com.guaiguailang.harmony.domain.dto.ParamLogin; import com.guaiguailang.harmony.domain.entity.SystemRole; +import com.guaiguailang.harmony.domain.entity.SystemRoleMenu; import com.guaiguailang.harmony.domain.vo.ResponseResult; import org.springframework.http.ResponseEntity; @@ -19,4 +20,10 @@ public interface AuthService { ResponseResult delRole(SystemRole systemRole); ResponseResult delRoleRealy(SystemRole systemRole); + + ResponseResult getMenuByRole(String roleId); + + ResponseResult delRoleMenu(SystemRoleMenu srm); + + ResponseResult addRoleMenu(SystemRoleMenu srm); } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java index cda15f5..8ac7dea 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/AuthServiceImpl.java @@ -5,11 +5,13 @@ import cn.dev33.satoken.util.SaResult; import com.github.yitter.idgen.YitIdHelper; import com.guaiguailang.harmony.domain.dto.ParamLogin; import com.guaiguailang.harmony.domain.entity.SystemRole; +import com.guaiguailang.harmony.domain.entity.SystemRoleMenu; import com.guaiguailang.harmony.domain.entity.UserInfo; import com.guaiguailang.harmony.domain.vo.ResponseResult; import com.guaiguailang.harmony.domain.vo.UserInfoLogin; import com.guaiguailang.harmony.mapper.AuthMapper; import com.guaiguailang.harmony.mapper.RoleMapper; +import com.guaiguailang.harmony.mapper.SystemMapper; import com.guaiguailang.harmony.mapper.UserMapper; import com.guaiguailang.harmony.service.AuthService; import com.guaiguailang.harmony.utils.PasswordEncryptor; @@ -34,12 +36,15 @@ public class AuthServiceImpl implements AuthService { private final AuthMapper authMapper; @Autowired private final UserMapper userMapper; + @Autowired + private final SystemMapper systemMapper; private final RoleMapper roleMapper; - public AuthServiceImpl(AuthMapper authMapper, UserMapper userMapper, RoleMapper roleMapper) { + public AuthServiceImpl(AuthMapper authMapper, UserMapper userMapper, SystemMapper systemMapper, RoleMapper roleMapper) { this.authMapper = authMapper; this.userMapper = userMapper; + this.systemMapper = systemMapper; this.roleMapper = roleMapper; } @@ -83,7 +88,17 @@ public class AuthServiceImpl implements AuthService { @Override public ResponseResult getRoleAll() { - List result = roleMapper.getRoleAll(); + // 判断是不是管理员,是管理员就加载所有,不是管理员就加载有权限的 + Long id = Long.parseLong(StpUtil.getLoginId().toString());// 获取用户id + // 获取level + SystemRole systemRoleCreator = userMapper.getRoleLevelByUserId(id); + List result; + if(systemRoleCreator.getLevel() ==1) { + // root 加载所有权限 + result = roleMapper.getRoleAll(); + }else{ + result = roleMapper.getRole(systemRoleCreator.getLevel()); + } return ResponseResult.success(result); } @@ -133,5 +148,29 @@ public class AuthServiceImpl implements AuthService { return ResponseResult.success(roleMapper.deleteById(systemRole.getId())); } + @Override + public ResponseResult getMenuByRole(String roleId) { + return ResponseResult.success(systemMapper.getMneuByRoleId(roleId)); + } + + @Override + public ResponseResult delRoleMenu(SystemRoleMenu srm) { + int result = systemMapper.deleteRoleMenuById(srm.getId()); + return ResponseResult.info(result,result==1?"成功":"失败"); + } + + @Override + public ResponseResult addRoleMenu(SystemRoleMenu srm) { + // 查询是否已被添加 + List systemRoleMenuList = systemMapper.roleMenuIsin(srm.getRoleId(),srm.getMenuId()); + if(systemRoleMenuList.size()>0){ + return ResponseResult.error("该功能已拥有,无需再次添加"); + } + srm.setStatus(1); + srm.setAbout("操作人:"+StpUtil.getLoginId().toString()); + int result = systemMapper.addRoleMenu(srm); + return ResponseResult.info(result,result==1?"成功":"失败"); + } + } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/SystemServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/SystemServiceImpl.java index 4a11187..4f96706 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/SystemServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/SystemServiceImpl.java @@ -2,20 +2,16 @@ package com.guaiguailang.harmony.service.impl; import cn.dev33.satoken.stp.StpUtil; import com.guaiguailang.harmony.domain.entity.MenuDataItem; -import com.guaiguailang.harmony.domain.entity.SystemMenu; import com.guaiguailang.harmony.domain.entity.UserInfo; -import com.guaiguailang.harmony.domain.vo.MenuVO; import com.guaiguailang.harmony.domain.vo.ResponseResult; import com.guaiguailang.harmony.mapper.SystemMapper; import com.guaiguailang.harmony.mapper.UserMapper; import com.guaiguailang.harmony.service.SystemService; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; -import java.util.stream.Collectors; + @Slf4j @Service diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java index c489e07..2621e6c 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java @@ -177,11 +177,6 @@ public class UserServiceImpl implements UserService { userAddParam.setTelphone(userAddParam.getTelphone()==null?"":userAddParam.getTelphone()); userAddParam.setAvatar(userAddParam.getAvatar()==null?"":userAddParam.getAvatar()); userAddParam.setCode(userAddParam.getCode()==null?"":userAddParam.getCode()); - - System.out.println("===========用户信息"); - System.out.println(userAddParam.toString()); - System.out.println("==================="); - int result = userMapper.addUser(userAddParam); if (result == 1) { return ResponseResult.success(userAddParam);